JSON Formatting Explained — Syntax, Common Errors & Best Practices

JSON (JavaScript Object Notation) is a lightweight text format for storing and transporting data. It is easy for humans to read when formatted, and easy for machines to parse. Understanding its syntax rules prevents the errors that every developer encounters eventually.

JSON Data Types

JSON supports six value types: strings (in double quotes), numbers (integer or decimal), booleans (true or false), null, arrays (ordered lists in square brackets), and objects (key-value pairs in curly braces).

All keys must be strings and must use double quotes. Values can be any of the six types, including nested objects and arrays.

Common Syntax Errors

Single quotes around keys or values (JSON requires double quotes). Trailing commas after the last element in an array or object. Comments — JSON does not support // or /* */ comments. Unquoted keys. Numbers with leading zeros (e.g., 007 is invalid). Control characters in strings that are not properly escaped.

Formatted vs Minified JSON

Formatted JSON (also called beautified or pretty-printed) uses indentation and line breaks to make the structure readable. It is useful during development, debugging, and in version-controlled config files.

Minified JSON removes all unnecessary whitespace to reduce file size. Minified JSON is typically used in production API responses and stored data where every byte matters.

When to Validate

Always validate JSON before using it in production. A single missing comma or unclosed bracket causes the entire parse to fail. Most languages throw a parse error with a position hint — paste the JSON into a formatter to pinpoint the exact location.

Examples

Valid JSON object

{ "name": "Alice", "age": 30, "active": true, "scores": [95, 87, 92], "address": null }

Common error: trailing comma

{ "name": "Alice", "age": 30, } ← The comma after 30 is invalid. Remove it.

Practical notes

  • •JSON is a strict subset of JavaScript object notation but has stricter rules — a valid JS object literal is often not valid JSON.
  • •Many tools accept JSON5 (a superset that allows comments and single quotes), but standard parsers do not.

Try the tools

Frequently asked questions

Does JSON support comments?

No. Standard JSON does not support comments. If you need annotated config files, consider YAML (which allows # comments) or JSON5.

What is NDJSON?

NDJSON (Newline-Delimited JSON) is a format where each line is a valid JSON value, typically used for streaming large datasets and log files.