SyntaxError: Unexpected token is one of the least helpful messages in software. It tells you something is wrong somewhere in the document, and the position is often a character after the actual mistake. The fix is usually simple; finding it is the work.
Read the position, not the prose
Modern V8 messages include a position:
Unexpected token } in JSON at position 42
or, in newer versions:
Expected ',' or '}' after property value in JSON at position 42 (line 3 column 5)
Position 42 is a zero-based offset into the string. Convert it to a line and column by counting newlines before it. The reported token is often the character where the parser gave up, which may be one character after the missing comma or quote.
Browser messages vary. Firefox and Safari phrase them differently and may omit the position. If the message is vague, paste the document into a validator that computes the line and column.
The usual suspects
Trailing commas
{
"a": 1,
"b": 2,
}
Valid in JavaScript object literals, invalid in JSON. Remove the comma after the last member. This is the most common error when copying code into a .json file.
Single quotes
{ 'a': 1 }
JSON requires double quotes for keys and strings. Single quotes are valid in JavaScript, Python and YAML, not here.
Comments
{
// explanation
"a": 1
}
Standard JSON has no comments. Some tools accept JSONC or JSON5, but a strict parser will reject the file. Remove the comment or use a configuration format that allows it.
Unquoted keys
{ a: 1 }
Keys must be strings. Quote them.
Missing commas
{
"a": 1
"b": 2
}
The parser reports an unexpected string or brace after the value. The actual fix is a comma at the end of the previous line.
A byte-order mark
A UTF-8 BOM before the opening { is invisible in most editors and rejected by many parsers. If the file starts with { visually but the parser says “unexpected token at position 0”, check for a BOM. Save as UTF-8 without BOM.
Control characters in strings
A literal newline or tab inside a string is invalid. It must be escaped as \n or \t. Copying a multi-line message into a JSON string produces this error.
Truncated output
A response cut off mid-stream produces “unexpected end of JSON input”. Check the HTTP status, the Content-Length, and whether the server timed out. This is a transport problem, not a syntax problem.
HTML instead of JSON
An API behind a proxy returns a login page or an error page with 200 OK. The parser sees < at position 0. Log the raw response body before parsing; the content type may say JSON while the body is HTML.
Duplicate keys
Syntactically valid, semantically ambiguous. JSON.parse keeps the last value. If a downstream system keeps the first, the two disagree. Some linters flag duplicates; a strict validator may reject them.
A systematic procedure
- Save the exact string that failed, not a re-typed version. Log it with
JSON.stringify(raw)so invisible characters are visible. - Check the first and last characters. A truncated document fails at the end; a BOM fails at the start.
- Paste into a validator that reports line and column.
- If the validator accepts it but your code fails, check whether the string was transformed before parsing: template literals,
replace, shell interpolation, or an editor's smart quotes. - If it is a response, log status, content type and the first 200 characters.
Smart quotes and invisible edits
“key” with curly quotes is not "key". Word processors, chat apps and some note tools substitute them automatically. The parser sees an invalid character and reports a token that looks correct in the editor. Turn off smart quotes or retype the quotes.
Escaping versus parsing
If you are building a JSON string by hand, stop. Use JSON.stringify on an object. Manual escaping misses backslashes, control characters and Unicode edge cases. If you need to embed a JSON string inside another JSON string, stringify twice deliberately and document it.
The JSON validator reports the first error with a line, column and snippet. The formatter can repair indentation once the syntax is valid. Neither can guess what a truncated payload was meant to contain; that requires the source.