drop the json

JSON syntax errors, explained

JSON parse errors are terse and the position they report is often one character past the real mistake. This guide maps the messages you actually see in browsers, Node, Python and Java to the handful of mistakes that cause them.

Want the computer to find it for you? Paste the document into the validator — it reports the line and column — or into the formatter, which repairs most of these automatically.

1. Trailing comma

{ "a": 1, "b": 2, }

Messages: Unexpected token }, Expected double-quoted property name, Python's Expecting property name enclosed in double quotes. JSON forbids a comma after the last element. JavaScript allows it, which is why it is the single most common error in hand-written JSON.

2. Single quotes

{ 'name': 'Ada' }

Messages: Unexpected token ', Expecting value. Strings and keys must use double quotes. Python dict reprs and JavaScript object literals both print single quotes, so this usually means someone pasted a language value instead of real JSON.

3. Unquoted keys

{ name: "Ada" }

Legal in JavaScript, illegal in JSON. Every key is a string and needs quotes.

4. Comments

{ "debug": true // remove before deploy }

Message: Unexpected token /. JSON has no comments. Config formats that look like JSON but allow them (tsconfig.json, VS Code settings) are JSONC, a superset. See JSON with comments and trailing commas.

5. Unexpected end of JSON input

The parser ran out of text while still inside an object, array or string. Causes: a missing closing bracket, a document truncated by a log line limit, or an empty string passed to the parser (a very common bug when an HTTP response body is empty).

6. Bad escape sequences

{ "path": "C:\Users\ada" }

Message: Bad escaped character, Invalid escape. A backslash must be followed by one of " \ / b f n r t or uXXXX. Windows paths and regular expressions need every backslash doubled.

7. Control characters inside strings

A literal newline or tab inside a string value is an error — it must be written as \n or \t. This bites when multi-line text is pasted into a string.

8. NaN, Infinity, undefined

None of these exist in JSON. JSON.stringify silently turns NaN into null and drops undefined properties; Python's json.dumps emits NaN unless you pass allow_nan=False, producing JSON that other parsers reject.

9. Leading zeros and plus signs

{ "zip": 02134, "delta": +5 }

Numbers cannot start with 0 unless they are exactly 0, and cannot have a leading +. Postal codes and IDs with leading zeros must be strings.

10. Multiple top-level values

{"a":1}
{"a":2}

Message: Unexpected non-whitespace character after JSON. A JSON document is one value. Several objects per file is a different format, JSON Lines (NDJSON), and must be parsed line by line.

11. Byte order mark and invisible characters

A file saved as "UTF-8 with BOM" starts with an invisible \uFEFF. Many parsers reject it with Unexpected token at position 0. Likewise, non-breaking spaces copied from a web page are not JSON whitespace. Re-save the file as plain UTF-8.

12. Duplicate keys

Not a syntax error, but a trap: the spec says behaviour is undefined, and nearly every parser silently keeps the last value. If a field seems to have the wrong value, search for the key twice.

Reading the error position

Browsers report position N as a character offset from the start of the text. Count line breaks to translate it, or use a tool that does it for you. The offset points at where the parser gave up, which is usually the token after the actual mistake — look one token back.