How to Fix Invalid JSON
β± 2 min readJSON is strict by design β one stray comma and the whole document refuses to parse. The good news: almost every 'invalid JSON' error is one of a handful of causes, and the error message tells you where to look.
First, read the error properly
Parsers report a position, e.g. 'Unexpected token } in JSON at position 74' or a line/column pair. That position is where the parser gave up β the actual mistake is usually at or just before it. Paste the JSON into a formatter, jump to that spot, and the culprit is normally staring at you.
The usual suspects
In practice, these eight cause nearly every failure:
- Trailing comma β {"a": 1,} is invalid; JSON forbids a comma after the last item.
- Single quotes β strings and keys must use double quotes: 'name' fails, "name" works.
- Unquoted keys β {name: "x"} is a JavaScript object literal, not JSON.
- Comments β // and /* */ are not part of JSON at all.
- undefined and NaN β not valid JSON values; use null or a number.
- Smart quotes β copying from Word or chat apps swaps " for curly quotes the parser rejects.
- Truncated data β a cut-off response loses its closing brackets; the error points at the very end.
- Invisible characters β a BOM or non-breaking space at the start fails with an error at position 0.
Why JSON is stricter than JavaScript
JSON looks like a JavaScript object literal but is a separate, deliberately tiny specification. The strictness is the feature: any parser in any language accepts exactly the same documents, with no dialect drift. When you want the relaxed version for config files, that's a different format (JSON5 or JSONC) β and standard parsers will still reject it.
The fast fix workflow
Paste the JSON into the formatter below, read the error position, fix that one spot, and re-validate β repeat until it pretty-prints. For machine-generated JSON that fails repeatedly, fix the generator (usually string concatenation that should be a serializer call).
Frequently asked questions
Are comments allowed in JSON?
No. The JSON spec has no comment syntax. Tooling-specific dialects like JSONC (used by VS Code config) allow them, but standard parsers β including JSON.parse β reject them.
Why does a trailing comma break JSON when JavaScript allows it?
JavaScript's grammar tolerates trailing commas; the JSON spec, frozen and minimal on purpose, does not. Every compliant parser must reject it.
What does 'Unexpected token' actually mean?
The parser met a character that can't legally appear at that point β often the visible symptom of an earlier mistake, like a missing quote or comma a few characters before the reported position.