JSON (JavaScript Object Notation) is the format nearly every API, config file, and log line is written in today. Its rules are simple, but they're strict — a single misplaced comma is enough to make an entire payload unparseable. This guide covers the syntax rules, the errors developers hit most often, and how to recover from them.
The core syntax rules
Valid JSON is built from exactly six value types, nested inside two container types:
- Objects —
{ "key": value }, with keys always wrapped in double quotes. - Arrays —
[value, value], comma-separated, no trailing comma after the last item. - Strings — always double-quoted, never single-quoted; special characters are escaped with
\. - Numbers, booleans (
true/false), andnull.
Two things people expect JSON to support but that aren't part of the spec: comments (// or /* */) and trailing commas. Both will fail strict parsing in JSON.parse.
The most common JSON errors
- Trailing comma —
[1, 2, 3,]or a comma after the last property in an object. - Single-quoted strings —
{'name': 'Ada'}instead of double quotes. - Unquoted keys —
{name: "Ada"}instead of{"name": "Ada"}. - Missing comma between two properties or array items.
- Unescaped quotes inside a string — a stray
"that terminates the string early. - Non-JSON values —
undefined,NaN, or functions are valid JavaScript object values but are not valid JSON. - Duplicate keys — technically parseable (the last one usually wins), but a common source of silent bugs.
How to fix invalid JSON
A good JSON validator does two things: it tells you the exact line and column where parsing broke, and it can attempt to auto-repair the most common mistakes above — closing unbalanced brackets, stripping trailing commas, converting single quotes to double, and quoting bare keys. When repair isn't possible automatically, jumping straight to the reported line/column beats scanning the whole payload by eye.
JSON vs. JSON5, JSONC, and YAML
If you control both ends of a system, relaxed variants exist for a reason: JSONC (JSON with Comments, used by tools like VS Code's settings.json) and JSON5 allow comments, trailing commas, and unquoted keys. YAML goes further with a whitespace-based syntax and no required quoting at all. None of these are interchangeable with strict JSON — always convert back to standard JSON before sending data to an API or storing it as a payload.