Home › Guides › "Unexpected token … is not valid JSON" — the token-by-token fix map

"Unexpected token … is not valid JSON" — the token-by-token fix map

Direct answer: the parser met a character that JSON grammar forbids at that spot. The token in the message tells you which family of problem you have: a quote problem, a comma problem, a JavaScript-ism (undefined, NaN, single quotes), or — when the token is "<" — an HTML page parsed as JSON by mistake. The tool below repairs the fixable families and lists every change.

Repair the JSON behind the error

Processed in your browser, so big pastes don't freeze the page. Nothing is uploaded — check your browser's network tab if you'd like to verify.

Read the token, find the cause

Token in the messageUsual causeFix
' (single quote)Strings or keys written JavaScript/Python styleConvert to double quotes — the repair tool does it and lists each one
A letter where a key is expectedUnquoted key ({name: "x"})Quote the key
} or ] right after a commaTrailing commaRemove the comma before the closer
/A // or /* */ comment (JSON has none)Remove comments, or use a JSON5/JSONC parser where comments are legal
u of undefined, N of NaNJavaScript values serialised naivelyMap to null — and fix the producer: JSON.stringify drops/keeps these differently per position
<An HTML page (error/redirect) parsed as JSONFix the request, not the "JSON" — check URL, status, auth
S of "Sure!" or similar proseAn LLM wrapped the JSON in conversationExtract the document (strip fences/prose) — the repair tool handles the classic shape

Why the position in the message matters

Parsers report where they noticed the problem, which can be a few characters after the actual mistake — a missing comma is typically reported at the next key, and an unclosed string can swallow half the document before anything looks wrong. When you repair with the tool above, the Repair Report lists each fix at its own location, so a cascade caused by one early error is visible as a cascade instead of five mysterious edits.

Fix it at the source

If the same payload shape keeps failing, the durable fix is upstream: serialise with a real JSON encoder rather than string templates; if you control the LLM prompt, ask for raw JSON only and validate before use; if you are consuming JavaScript-flavoured data regularly, you are in JSON5 territory and should say so explicitly rather than letting a strict parser discover it in production.

Frequently asked questions

What does "Unexpected token 'x', ... is not valid JSON" mean?

The parser hit a character it cannot accept at that position. The message names the offending character and usually its position. The common culprits: single quotes, unquoted keys, trailing commas, comments, an HTML error page or plain prose where JSON was expected, and JavaScript values like undefined or NaN.

The token looks fine to me. Why is it invalid?

JSON is stricter than JavaScript object syntax. In JSON, keys must be double-quoted strings, strings must use double quotes, there are no comments, no trailing commas, and the only literals are true, false and null. Code that would run happily as a JS object literal can fail as JSON in five different places.

The error says "Unexpected token '<'". What happened?

Almost always: the response was HTML, not JSON — an error page, a login redirect, or a "not found" from the server — and your code tried to parse it as JSON. Check the URL, the status code, and that you are reading the response body you think you are.