Common JSON Syntax Errors and How to Fix Them
Trailing commas, single quotes, unquoted keys, comments — a field guide to the JSON mistakes that break parsers, with the fix for each one.
JSON looks simple — and it is, until a stray character turns a five-hundred-line payload into “Unexpected token.” The format has no comments, no trailing commas, no single quotes, and no mercy. Every parser enforces the same spec (RFC 8259), so the same six mistakes cause the same error in every language.
This page walks through each one with the exact text a parser prints, what the problem actually is, and how to fix it. If you have a broken document in the clipboard right now, paste it into the JSON formatter — it will point at the line, the column and the character at fault.
1. Trailing commas
JavaScript, Python and most config formats accept a comma after the last item. JSON does not. This is the single most common cause of “Unexpected token” errors, because developers copy objects straight from source code.
{
"name": "Alice",
"role": "admin", ← trailing comma
}Fix: remove the comma before the closing } or ]. If you are generating JSON from code, most serializers already omit it — the comma usually sneaks in when someone edits the file by hand.
{
"name": "Alice",
"role": "admin"
}2. Single quotes instead of double quotes
JSON requires double quotes around every string and every key. Single quotes are valid JavaScript but invalid JSON, and the error usually reads “Expected property name enclosed in double quotes.”
{'name': 'Alice'}Fix: replace every ' with ". If the value itself contains a double quote, escape it as \".
{"name": "Alice"}3. Unquoted property names
JavaScript lets you write { name: 1 } without quoting the key. JSON never does — every key must be a double-quoted string. The parser will say “Expected property name” or “Unexpected token n.”
{name: "Alice", age: 30}Fix: wrap every key in double quotes.
{"name": "Alice", "age": 30}4. Comments
JSON has no comment syntax. Neither // nor /* */ is legal. This catches people who work with JSON5 or JSONC (the dialect VS Code uses for settings.json) and assume standard parsers will accept it too.
{
// database connection
"host": "localhost",
"port": 5432
}Fix: remove the comment entirely, or move the information into a key like "_comment" if you need it in the file.
{
"_comment": "database connection",
"host": "localhost",
"port": 5432
}5. undefined, NaN and Infinity
These are JavaScript values, not JSON values. The only legal primitives are strings, numbers (finite, no leading zeros except for 0.x), true, false and null. A parser will reject undefined with “Unexpected token u.”
{"score": NaN, "retries": undefined}Fix: use null where no value exists. For NaN and Infinity, either use null or encode them as strings ("NaN") if the consuming code knows to parse them back.
{"score": null, "retries": null}6. Missing or extra brackets, braces and colons
A mismatched { without its }, a missing : between key and value, or an accidental double comma (,, ) will trigger “Unexpected end of JSON input” or “Expected ':' after property name.” These errors are hardest to find in large files because the parser only knows something is wrong when it reaches the point of failure, which may be hundreds of lines after the actual mistake.
Fix: use a formatter that reports position, not just the error type. The JSON formatter shows the line, the column and a caret under the character, which is enough to jump straight to the problem even in a 10,000-line document.
How to catch these errors before they ship
Most of these mistakes happen during manual editing. Three habits prevent nearly all of them:
- Format before committing. Paste your JSON into a formatter and let it parse the document. If it renders, it is valid. If it does not, you get the exact position of the problem.
- Never hand-edit JSON you could serialize. If the data comes from code, write it with
JSON.stringifyor your language’s equivalent. The serializer handles quoting, escaping and comma placement automatically. - Use a schema. A JSON Schema validator catches structural problems that a syntax check misses — a field with the wrong type, a required key that is absent, or a value outside the allowed range.
Quick reference: what JSON allows
| Element | Allowed | Not allowed |
|---|---|---|
| Strings | Double quotes only | Single quotes, backticks |
| Keys | Double-quoted strings | Unquoted, single-quoted |
| Numbers | 42, -3.14, 1e10 | NaN, Infinity, 0x1F, leading zeros |
| Booleans | true, false | True, FALSE, yes |
| Null | null | None, nil, undefined |
| Trailing commas | Never | {"a":1,} |
| Comments | Never | //, /* */ |
Got broken JSON right now?
Paste it into the JSON formatter — it will name the error, show the line and column, and point a caret at the character that broke it. Once it is fixed, use the JSON checker to validate it, or the schema validator to verify its structure.