How to Fix "Unexpected Token" Errors in JSON
If you work with APIs, you have met this error: SyntaxError: Unexpected token in JSON at position 4231. It means the JSON parser hit a character it was not allowed to see at that point. The message is famously unhelpful — a position number in a payload that is one giant line — but the underlying causes are a short, predictable list. Here is how to find and fix each one.
First: turn the position into a line number
Paste the payload into a JSON validator. A good validator converts the raw character position into a line and column, which immediately narrows a 50KB payload to one visible spot. Modern browsers now include line numbers in the error itself (at line 3 column 8), but server logs and older environments still give you only the position.
The six usual suspects
1. Trailing commas
{ "name": "Kawsu", "role": "founder", }
JavaScript object literals allow that final comma; JSON does not. This is the single most common error when JSON is written by hand or generated by naive string concatenation. Delete the comma before the closing brace or bracket.
2. Single quotes
{ 'valid': false }
JSON strings — including keys — must use double quotes. Python developers hit this constantly, because printing a Python dict produces single quotes. The fix in Python is to serialise properly with json.dumps(data) instead of str(data).
3. Unquoted keys
{ status: "ok" }
Legal JavaScript, illegal JSON. Every key needs double quotes: { "status": "ok" }.
4. Comments
JSON has no comment syntax. // or /* */ anywhere in the document is an immediate parse failure. If you need commented config, that is what JSONC (VS Code settings) or YAML are for — but anything consumed by JSON.parse must be comment-free.
5. Invalid values
undefined, NaN and Infinity are JavaScript values, not JSON values. The legal scalar set is strings, numbers, true, false and null. Serialisers usually convert these for you; hand-built strings do not.
6. The payload is not JSON at all
The sneakiest one: Unexpected token < in JSON at position 0. That < is the first character of an HTML page — usually an error page or a login redirect that your code received instead of the API response it expected. The bug is not in your JSON handling; it is that the request failed. Check the HTTP status code and the response body before parsing.
A debugging workflow that always works
- Log or copy the raw response text before any parsing.
- Paste it into a JSON formatter and validator to get the exact error line.
- Look at that line for the six patterns above.
- If position 0 fails, the response is not JSON — inspect the HTTP status and headers instead.
Preventing it in the first place
Never build JSON by string concatenation — always use your language's serialiser (JSON.stringify, json.dumps, json_encode). Serialisers cannot produce trailing commas, wrong quotes or unquoted keys. On the consuming side, wrap parsing in a try/catch that logs the raw body, so the next "unexpected token" takes you thirty seconds instead of thirty minutes.