How to Fix SyntaxError: Unexpected Token in JSON (Common Causes & Solutions)
Resolve JavaScript JSON.parse 'SyntaxError: Unexpected token' errors. Learn how trailing commas, single quotes, unescaped newlines, and BOM markers break parsing.
SyntaxError: Unexpected token occurs when JSON.parse() receives input that violates RFC 8259. Common culprits include single quotes instead of double quotes, trailing commas after the last array/object item, raw unescaped newlines within strings, or hidden UTF-8 BOM characters.
Solve this problem now with our free JSON Formatter & Validator
Try it instantly in your browser — zero tracking, 100% client-side privacy.
Specification & Architecture Reference
| Syntax Violation | Invalid Snippet | RFC 8259 Compliant Fix |
|---|---|---|
| Single Quotes | { 'key': 'value' } | { "key": "value" } |
| Trailing Comma | { "id": 1, "name": "Micky", } | { "id": 1, "name": "Micky" } |
| Unescaped Newline | { "text": "Line 1 Line 2" } | { "text": "Line 1\nLine 2" } |
| Leading UTF-8 BOM | \uFEFF{"status":200} | {"status":200} |
| Unquoted Key | { status: 200 } | { "status": 200 } |
Why JSON.parse Throws Unexpected Token
JavaScript's built-in `JSON.parse()` method enforces strict compliance with **RFC 8259**. Unlike standard JavaScript object literals, JSON does not tolerate loose syntax shortcuts. Whenever the parser encounters a character that deviates from the formal specification grammar, execution immediately aborts with a `SyntaxError: Unexpected token` message indicating the character position.
1. Single Quotes Instead of Double Quotes RFC 8259 explicitly mandates that strings and dictionary keys must be delimited by standard ASCII double quotes (`"...`, ASCII 34). Single quotes (`'...`) or backticks will immediately fail:
// ❌ Throws SyntaxError: Unexpected token ' in JSON// ✅ Valid RFC 8259 JSON JSON.parse('{ "service": "auth" }'); ```
2. Trailing Commas in Objects or Arrays JavaScript arrays and objects permit trailing commas, but standard JSON forbids them. A trailing comma before a closing brace `}` or bracket `]` triggers an unexpected token error:
// ❌ Trailing comma violation
{
"host": "localhost",
"port": 5432,
}3. Hidden UTF-8 Byte Order Marks (BOM) When reading JSON files exported from Windows editors or legacy CSV converters, a hidden zero-width Byte Order Mark (`\uFEFF`) is often prepended to index 0 of the string. Because `JSON.parse()` does not automatically strip BOM headers, it throws `SyntaxError: Unexpected token <U+FEFF> at position 0`.
To sanitize strings before parsing: ```javascript const cleanJson = rawInput.replace(/^\uFEFF/, ""); const data = JSON.parse(cleanJson); ```
How to Diagnose and Repair Instantly Manually hunting down mismatched quotation marks in nested payloads spanning thousands of lines is error-prone. Use our browser-based **JSON Formatter & Validator** to highlight the exact row and column causing the syntax collision, clean up formatting, and format your JSON payload instantly.
Solve this problem now with our free JSON Formatter & Validator
Execute this workflow immediately without saving data on remote servers.
Frequently Asked Questions
Can I use single quotes in standard JSON?
No. RFC 8259 strictly requires ASCII double quotes (") for both keys and string values. Single quotes trigger an immediate parser error in all standard JSON engines.
How do I ignore comments and trailing commas in JSON?
Standard JSON does not allow comments or trailing commas. If your application requires them, use JSON5 or parse with a relaxed parser like yaml/hjson before serializing back to standard JSON.