JSON error and escape reference
Three tables: what each parse error usually means, which escapes are legal inside a string, and what happens to values JSON cannot represent.
Parse errors and what causes them
Browsers change the wording between releases, so match on the cause rather than the exact sentence. Every row below is a syntax problem: the document never reaches the formatting step.
| Input | Message you will see | Cause | Fix |
|---|---|---|---|
| {"a":1,} | Unexpected token } or Expected double-quoted property name | trailing comma after the last member | delete the comma |
| {'a':1} | Unexpected token ' | single quotes around a key | use double quotes |
| {a:1} | Unexpected token a | key without quotes | wrap the key in double quotes |
| // note | Unexpected token / | a comment at the start | remove the comment |
| {"a":1} extra | Unexpected non-whitespace character after JSON | text after the closing brace | keep one document only |
| {"a":"line break"} | Bad control character in string literal | a real newline inside a string | write backslash-n instead |
| {"a":01} | Unexpected number | leading zero in a number | write 1 |
| an empty or blank box | Please enter JSON content | nothing left after trimming | paste a document |
Positions in the message count characters from the start of the string, including whitespace. If the reported position is near the end, look at the last member; a missing closing bracket is usually reported one character past the end of the input.
Legal escape sequences
Only the escapes in this table are valid inside a JSON string. Anything else, including a backslash before an ordinary letter, is a syntax error rather than a literal backslash.
| Escape written | Character produced | When it is needed |
|---|---|---|
| backslash + quote | a double quote inside the string | any text containing a quote |
| backslash + backslash | one backslash | Windows paths and regular expressions |
| backslash + slash | a forward slash | optional, the slash needs no escape |
| \b | backspace, U+0008 | almost never |
| \f | form feed, U+000C | almost never |
| \n | line feed | multi-line text kept on one line |
| \r | carriage return | Windows line endings |
| \t | tab character | tab inside a value |
| \uXXXX | one UTF-16 code unit | characters you cannot type directly |
A pair of backslash-u escapes is needed for characters outside the basic plane, such as many emoji: the first unit is a high surrogate and the second a low surrogate.
What JSON.stringify does to each value
Valid JavaScript values do not always survive serialisation. The table shows the object and array cases, which behave differently for the same value.
| Value | Inside an object | Inside an array | Why |
|---|---|---|---|
| undefined | key omitted | null | JSON has no undefined |
| function | key omitted | null | functions cannot be serialised |
| Symbol | key omitted | null | symbols cannot be serialised |
| NaN | null | null | not a finite number |
| Infinity | null | null | not a finite number |
| Date | ISO 8601 string | ISO 8601 string | Date provides toJSON() |
| BigInt | throws TypeError | throws TypeError | convert it to a string first |
Omitting a key and writing null are different outcomes: the first removes the member, the second keeps it with an empty value. A parser on the other side may treat a missing field as a default and a null field as an explicit clear, so check which one your schema expects.
Reading a message without guessing
- Unexpected token usually points at the character the parser could not accept.
- Unexpected end of JSON input means the document stopped early, often a missing brace.
- Bad control character means a raw tab, newline or other unescaped control byte.
- Unexpected non-whitespace character means one value parsed correctly and text followed.
- A message about a property name means the parser expected a quoted key.
None of these messages mention duplicate keys, precision loss or overflow, because those documents are syntactically valid. Check them separately.