What Causes "Unexpected Token" in JSON?
The SyntaxError: Unexpected token error occurs when JSON.parse() or any JSON parser encounters a character that violates RFC 8259 syntax. It is the most common JSON error developers face.
Top Causes (in order of frequency)
- Single quotes instead of double quotes:
{'name': 'Alice'}→ invalid. Use{"name": "Alice"}. - Unquoted object keys:
{name: "Alice"}→ invalid. Keys must be double-quoted strings. - Trailing comma:
{"a": 1, "b": 2,}→ invalid. Remove the comma after the last item. - JavaScript comments:
// commentor/* */→ invalid. JSON forbids comments. - HTML in the response: An API returning an HTML error page (404/500) that your code tries to JSON.parse.
- Undefined or NaN values:
{"value": undefined}or{"num": NaN}→ invalid JSON primitives. - BOM character: A Byte Order Mark () at the start of a UTF-8 file can cause an unexpected token at position 0.
Diagnosing the Error
Paste your JSON into our JSON Validator to get the exact line number and character position of the unexpected token.
// Invalid JSON — All will throw "Unexpected token"
{
'name': 'Alice', // ❌ Single quotes
age: 30, // ❌ Unquoted key
"active": true, // ❌ Trailing comma
// This is a comment // ❌ JavaScript comment
"value": undefined // ❌ undefined is not JSON
}
// Valid JSON — RFC 8259 compliant
{
"name": "Alice",
"age": 30,
"active": true,
"value": null
}