JSON Parse Error: Unexpected Token - How to Fix
The dreaded SyntaxError: Unexpected token in JSON. Here is exactly what causes it and how to fix it in every scenario.
What This Error Means
JSON.parse() encountered a character that is not valid at that position in the JSON string. The error message varies by browser:
// Chrome/Edge\nSyntaxError: Unexpected token X in JSON at position N\n\n// Firefox\nSyntaxError: JSON.parse: expected property name or '}'\n\n// Safari\nSyntaxError: JSON Parse error: Expected '}'Top 5 Causes and Fixes
1. Server Returned HTML Instead of JSON
The most common cause in API calls. Your server returned an HTML error page (404, 500) but your code tried to parse it as JSON. Fix: check response.ok and Content-Type header before parsing.
const res = await fetch('/api/data');\nif (!res.ok) throw new Error('HTTP ' + res.status);\nconst contentType = res.headers.get('content-type');\nif (!contentType?.includes('application/json')) {\n throw new Error('Expected JSON, got ' + contentType);\n}\nconst data = await res.json();2. Empty Response Body
JSON.parse('') always throws. Check for empty responses before parsing.
3. BOM Character at Position 0
If the error says "position 0" and the JSON looks correct, there is likely an invisible BOM (Byte Order Mark) character. Strip it: text.replace(/^\uFEFF/, '')
4. Invalid JSON Syntax
Trailing commas, single quotes, unquoted keys, or comments. Paste into our JSON Formatter to find the exact position.
5. Double-Encoded JSON
If you see \" everywhere, the JSON was stringified twice. Parse it twice: JSON.parse(JSON.parse(data))
Debug JSON Errors Instantly
Paste your JSON to see the exact error with line and column numbers.
Open JSON FormatterRelated: Fix Invalid JSON Guide, JSON Formatter, JSON Schema Validator