Developer guide
How to Format JSON Correctly (and Fix the Errors That Break It)
What valid JSON looks like, how to pretty-print and minify it, and how to fix the eight errors that cause most “Unexpected token” messages.
By M2Toolkit Editorial TeamPublished 8 min read
Quick answer
Valid JSON uses double quotes around every key and string, no trailing commas, no comments, and only true, false, null, numbers, strings, arrays and objects as values. Paste it into the JSON Formatter to indent it neatly — or see the exact line and column of any error.
JSON looks forgiving because it resembles JavaScript, but it's much stricter. One stray comma makes an entire API response or config file unreadable. Here's what correct JSON looks like, how to pretty-print and minify it, and how to fix the errors behind most “Unexpected token” messages.
The rules of valid JSON
{
"name": "Ada Lovelace",
"born": 1815,
"languages": ["English", "French"],
"active": false,
"address": null
}- The top level is usually an object
{}or an array[]. - Keys are always strings in double quotes.
- Strings use double quotes; special characters are escaped (
\",\n,\\). - Numbers have no leading zeros or plus signs;
NaNandInfinityaren't allowed. - Items are separated by commas, with no comma after the last one.
The errors that break JSON most often
| Error | Wrong | Right |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single quotes | {'a': 'b'} | {"a": "b"} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comments | {"a": 1 // note} | {"a": 1} |
| Python booleans | {"ok": True} | {"ok": true} |
| undefined / NaN | {"x": undefined} | {"x": null} |
| Unescaped quote | {"q": "say "hi""} | {"q": "say \"hi\""} |
| Missing comma | {"a": 1 "b": 2} | {"a": 1, "b": 2} |
When a parser says “Unexpected token at position 214”, the real mistake is usually just before that position — often a comma or quote on the previous line.
Try the free JSON ValidatorShows the line, column and a plain-English reason for the first error.Pretty-printing vs minifying
Pretty-printing adds indentation and line breaks so humans can read the structure. Use it for config files, debugging and code reviews. Two-space indentation is the most common convention.
Minifying removes all optional whitespace. Use it for API responses and storage where size matters; it typically cuts 10–30% from indented JSON.
In code, both are one line: JSON.stringify(data, null, 2) pretty-prints and JSON.stringify(data) minifies in JavaScript; json.dumps(data, indent=2) does the same in Python.
JSON vs YAML vs CSV
YAML is a superset of JSON that's easier to hand-edit (no quotes or braces needed), which is why it's popular for configuration. CSV is flat rows and columns, ideal for spreadsheets. Converting between them is common:
Frequently asked questions
Can JSON have comments?
Standard JSON can't. Some tools accept JSONC or JSON5 (which allow comments), but a strict parser will reject them. Remove comments, or keep documentation in a separate field.
Does key order matter in JSON?
No. JSON objects are unordered, so parsers may reorder keys. If order matters, use an array.
What's the maximum size of a JSON file?
The format has no limit; practical limits come from memory. Browsers handle tens of megabytes comfortably.