Complete Guide to JSON Formatting: Structure, Validation, and Best Practices
JSON (JavaScript Object Notation) has become the dominant format for data exchange on the web. REST APIs, configuration files, NoSQL databases, and inter-service communication all rely on JSON. Despite its apparent simplicity, poorly structured or invalid JSON is one of the most common causes of API integration bugs. This guide covers everything from the basics of JSON syntax to advanced formatting best practices.
JSON Syntax Rules
JSON is strict about syntax. Unlike JavaScript, JSON has no tolerance for deviations. Understanding these rules prevents common parsing errors:
- String keys required: All object keys must be strings in double quotes.
{name: 'Alice'} is invalid JavaScript-style shorthand; valid JSON is {"name": "Alice"}. - Double quotes only: JSON does not allow single quotes. All strings, whether keys or values, must use double quotes.
- No trailing commas: A trailing comma after the last property in an object or the last item in an array is a syntax error in JSON.
{"a": 1,} is invalid. - No comments: JSON has no comment syntax. If you need to document a JSON config file, use a JSON5 or JSONC (JSON with Comments) extension, or keep comments in a separate document.
- Limited value types: Valid JSON values are strings, numbers, booleans (
true/false), null, objects {}, and arrays []. JavaScript-specific values like undefined, NaN, Infinity, and Date objects are not valid JSON.
Pretty-Printing vs Minified JSON
Pretty-printed JSON uses indentation (typically 2 or 4 spaces) and newlines to make the structure human-readable. Use it for configuration files, documentation examples, and debugging. Minified JSON removes all whitespace to reduce file size. Use it for production API responses and data transfer where bandwidth matters. A typical JSON object is 20-40% larger when pretty-printed versus minified. Our JSON Formatter converts instantly between both formats.
Common JSON Formatting Mistakes
- Trailing commas: The most frequent error. When copying JSON objects or adding/removing properties, it's easy to leave a comma after the last item. Validate with a formatter to catch these instantly.
- Unescaped special characters in strings: Backslashes, double quotes, and control characters inside string values must be escaped. A newline inside a string should be written as
\n, not a literal newline. - Numeric strings:
"123" (a string) and 123 (a number) are different values. APIs often reject requests where a numeric field is sent as a string. - Null vs undefined: JSON supports
null but not undefined. When serializing JavaScript objects with JSON.stringify(), undefined properties are silently omitted, which can cause subtle bugs. - Unicode escaping: Non-ASCII characters can be included directly in JSON (UTF-8 is valid), or escaped as
\uXXXX. Mixing these styles in the same file is valid but inconsistent.
JSON Structure Best Practices
- Use camelCase for keys in API responses: JavaScript convention uses camelCase (
firstName), while databases often use snake_case (first_name). API responses should follow the client language convention — camelCase for JavaScript clients. - Be consistent with null vs omission: When a field has no value, decide whether to include it as
null or omit it entirely. Consistency helps API consumers handle responses predictably. - Use ISO 8601 for dates: Always represent dates as ISO 8601 strings:
"2025-03-15T14:30:00Z". Avoid Unix timestamps in JSON unless the consuming system requires them — timestamps are opaque to human readers. - Avoid deeply nested structures: JSON can represent any depth of nesting, but structures deeper than 3-4 levels become difficult to read and manipulate. Consider flattening or using references for deeply nested data.
- Use arrays for lists of homogeneous items: If you have multiple items of the same type, always use an array. Avoid using sequential numbered keys (
item1, item2) — that's an anti-pattern that breaks iteration.
JSON Schema: Validating Structure
JSON Schema is a vocabulary for describing the structure and constraints of JSON documents. It lets you define what fields are required, what types they must be, and what values are valid. This is invaluable for API contracts: both the server and client can validate against the same schema, catching mismatches before they cause runtime errors. Our JSON to Schema Generator automatically generates a draft JSON Schema from any sample JSON object.
Converting JSON to Other Formats
JSON is often just one representation of structured data. Depending on your use case, you may need to convert it:
- JSON to CSV: For spreadsheet analysis and database import. Use our JSON to CSV converter for flat arrays of objects.
- JSON to YAML: For Kubernetes configs, CI/CD pipelines, and configuration files where YAML's human-readable syntax is preferred. Use our JSON to YAML converter.
- JSON to XML: For legacy system integration and SOAP web services. Use our JSON to XML converter.
- JSON to TypeScript: For generating type-safe interfaces from API response samples. Use our JSON to TypeScript converter.
tip
When debugging API issues, always validate JSON first. Copy the request or response body into our
JSON Formatter — it will immediately highlight syntax errors with the exact line and character position. This saves hours of debugging compared to reading raw error messages.