Trailing commas, single quotes, comments — the five mistakes behind many "but it looks fine" JSON failures, and the one-character fix for each.
JSON looks simple — until your parser throws SyntaxError: Unexpected token at line 47 of a 200-line config file and you can't see what's wrong. The spec (RFC 8259) is sixteen pages, but small differences from JavaScript's looser object syntax catch out experienced developers all the time. The fix is usually one character.
Here are five errors behind a large share of "but it looks fine" failures — what the parser sees, why it complains, and how to spot the same shape next time — followed by a few documents that parse but still cause trouble. Every example was run through Python's json module and Node.js's JSON.parse; the error messages quoted are theirs. If you want to throw a blob into a working tool while you read, our JSON formatter will point at the offending line and column.
1. Trailing commas
JavaScript object literals tolerate them. JSON does not.
{"name": "ada",} ✗ trailing comma — invalid JSON
{"name": "ada"} ✓ valid
[1, 2, 3,] ✗ trailing comma — invalid
[1, 2, 3] ✓ valid
The grammar in RFC 8259 defines an object as members joined by a value separator (§4), and an array the same way (§5) — commas are separators between items, not terminators. Python's message is admirably direct: "Illegal trailing comma before end of object". Node says "Expected double-quoted property name", because after a comma it expects another member.
This usually bites when you're hand-editing config files: you delete the last entry, forget the comma on the previous line, save, and the parser stops cooperating. Habits from JavaScript, where formatters often add trailing commas to keep diffs clean, carry over. Some editors and tools use relaxed variants — JSON5, or "JSON with comments" — that allow trailing commas, but a file written that way will fail the moment it's read by a strict JSON parser.
2. Single vs double quotes
JSON requires double quotes around every string. Single quotes are rejected, even though JavaScript treats 'foo' and "foo" as equivalent.
{'name': 'ada'} ✗ single quotes — invalid
{"name": "ada"} ✓ valid
This goes for keys as well as values. Python rejects the first line with "Expecting property name enclosed in double quotes", which is a good clue to look at the quotes.
The common cause is generating JSON by printing a data structure instead of serialising it. Python's repr() of a dict and naive string templating both produce single quotes. Use json.dumps() in Python, JSON.stringify() in JavaScript, or the equivalent in your language.
3. Unquoted keys
JavaScript object literals let you write keys without quotes — {name: "ada"} — when the key is a valid identifier. JSON requires every key to be a quoted string, no exceptions.
{name: "ada"} ✗ unquoted key — invalid
{"name": "ada"} ✓ valid
{1: "first"} ✗ unquoted, numeric — invalid
{"1": "first"} ✓ valid
You'll hit this when copy-pasting from JS source into a .json file, or when someone used to YAML or TOML, both of which allow bare keys, writes JSON for the first time. The numeric case is the subtle one: JSON keys are always strings, even when they look like numbers, so code that reads them back may need to convert.
4. Comments
JSON has no comments. Not //, not /* */, not #. That was deliberate. Douglas Crockford, who originally specified the format, explained in 2012, as quoted in Wikipedia's JSON article:
"I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability."
{
// user record ✗ JSON has no comments
"name": "ada"
}
A strict parser rejects the whole document. Relaxed formats like JSON5 accept comments, but anything that reaches a strict parser downstream will fail.
A common workaround is a "_comment" field. Be clear about what that does: it isn't ignored. It's a normal key that every parser reads and every consumer receives, so code that validates against a schema, or copies every key into a database, will see it. Use it only where consumers are known to tolerate extra keys, and otherwise keep notes in a README or in the code that loads the file.
{
"_comment": "user record — see PR #423",
"name": "ada"
}
5. Special characters in strings
RFC 8259 §7 sets the rule:
"All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)."
— RFC 8259 §7 (IETF)
So: escape the double quote (\"), the backslash (\\), and control characters such as newline (\n) and tab (\t). Escaping a forward slash (\/) is allowed but optional.
{"path": "C:\Users\ada"} ✗ backslash not escaped
{"path": "C:\\Users\\ada"} ✓ valid
{"quote": "She said "hi""} ✗ unescaped quote
{"quote": "She said \"hi\""} ✓ valid
{"note": "line one
line two"} ✗ literal newline
{"note": "line one\nline two"} ✓ valid
The Windows path is a sneaky one: \U isn't a valid escape, so Python reports "Invalid \escape". The literal newline gives "Invalid control character". For arbitrary Unicode, JSON strings support \uXXXX escapes.
Documents that parse but still bite
Fixing syntax gets the parser to stop complaining. These next cases are worse, because some parsers accept them.
NaNandInfinity. Not valid JSON. Python'sjson.loadsaccepts{"x": NaN}by default; Node'sJSON.parserejects it. A file one service writes, another may refuse.- Duplicate keys. RFC 8259 says names within an object "SHOULD be unique", and that behaviour is unpredictable when they aren't. Both Python and Node silently kept the last value for
{"a": 1, "a": 2}. - Big integers. The RFC notes that integers in the range [−(2^53)+1, (2^53)−1] are interoperable. Outside it, values can change: Node parsed
9007199254740993as9007199254740992. Send large IDs as strings. - Leading zeros.
{"n": 007}is rejected — JSON numbers can't start with0unless the number is zero. - A byte-order mark. The RFC says implementations "MUST NOT add a byte order mark" to networked JSON and requires UTF-8 for exchange between systems that aren't part of a closed ecosystem. Python refused a BOM-prefixed string ("Unexpected UTF-8 BOM"), and so did Node.
What doesn't break: Windows line endings. CRLF between tokens is ordinary whitespace in the JSON grammar, and both parsers accepted it. If a file with CRLF fails, look for a literal line break inside a string instead.
Is JSON a subset of JavaScript?
Not always — it became one by design in ES2019. The TC39 JSON superset proposal explains that JSON strings could contain unescaped U+2028 and U+2029 characters while older JavaScript strings could not, and it extended JavaScript to allow them; the repository says it was scheduled for ES2019. The practical lesson is the opposite direction: plenty of valid JavaScript isn't JSON, which is what errors 1–4 are about.
The Toolhub workflow
Paste the failing document into the JSON formatter to pretty-print it, validate it, and get the line and column of the first syntax error. Fix that one, re-run, and repeat — parsers stop at the first problem, so a file with three errors reports them one at a time. Everything runs locally; your JSON never leaves the tab.
← All articles