Common JSON syntax errors
JSON looks like JavaScript object literals, which is exactly why people write things that are valid JavaScript but not valid JSON. The grammar is defined by RFC 8259 and it is strict: no comments, no trailing commas, double quotes only.
The eight mistakes
-
Trailing comma — tolerated by JavaScript, rejected by JSON.
{"a": 1,} invalid {"a": 1} valid -
Comments — neither
//nor/* */is allowed. Put the note in a separate field, or in the documentation. -
Single quotes — strings must use double quotes.
{"a": "x"} valid {'a': 'x'} invalid -
Unquoted keys —
{"a": 1} valid {a: 1} invalid -
NaN,Infinity,undefined— none of these are JSON values. Usenullor a string. -
Leading zeros —
012is not a JSON number. Write12, or quote it if it is an identifier such as a phone number. -
Raw control characters — a literal tab or newline inside a
string must be escaped as
\tor\n. - A byte order mark or stray text before the opening brace. Some editors add a BOM; JSON parsers reject it.
Valid JSON that still surprises you
-
Big integers lose precision. A JSON number becomes an
IEEE-754 double in JavaScript, so
9007199254740993is parsed as9007199254740992. If the exact value matters — database IDs, snowflake IDs, account numbers — send it as a string. - Duplicate keys keep the last value. The specification leaves it undefined, and every common parser keeps the final one, silently discarding the earlier value.
- Key order is not guaranteed. Most parsers preserve insertion order in practice, but the specification does not promise it. Never depend on it.
-
An empty body is not
{}. A zero-length response fails to parse; a request with no fields is{}.
Finding the position
Modern engines report an offset, for example
Unexpected token } in JSON at position 12 or
Expected property name or '}' in JSON at position 1. The
message is engine-specific, so do not build logic on its text — but the
position is usually enough to find the offending character.
A quick checklist
- Does every brace and bracket have a partner?
- Is every key in double quotes?
- Is the last element in each object or array free of a comma?
- Are there comments anywhere?
- Are large identifiers quoted?
Paste the payload into the JSON formatter to see the reformatted output and the syntax errors. Formatting keeps comments and trailing commas; Minify removes them and outputs strict JSON. If the JSON is a request body, the cURL generator turns it into a command you can run.