How to Fix JSON Syntax Errors (Unexpected token, Trailing comma)
SyntaxError: Unexpected token. Unexpected end of JSON input. Bad control character in string literal. If your code just threw one of these, the data you handed to JSON.parseis not valid JSON — and the parser stopped at the first thing it could not make sense of.
The fastest way to find the culprit is to validate the whole blob and let a parser point at the break. Here is how, plus the handful of mistakes that cause almost every JSON error.
How to find and fix the syntax error
QuickWand's free JSON Formatter validates as it formats, entirely in your browser — your data never touches a server.
- Open the JSON Formatter and paste the JSON that failed to parse.
- Click Format. If the JSON is valid, you get clean indented output. If it is not, the tool reports the parse error so you can zero in on the broken spot.
- Fix the issue (see the common causes below), then format again to confirm it now parses cleanly.
The usual suspects
- Trailing comma. A comma after the last item, like
[1, 2, 3,]or{"a": 1,}, is legal in a JavaScript literal but illegal in JSON. Delete the comma before the closing]or}. - Single quotes. JSON strings and keys must use double quotes.
{'name': 'sam'}is invalid;{"name": "sam"}is correct. - Unquoted keys.
{name: "sam"}fails — every key needs double quotes:{"name": "sam"}. - Comments.
// like thisor/* this */are not allowed in standard JSON and will throw. - Non-JSON values.
undefined,NaN,Infinity, and trailing function calls are not JSON. Only strings, numbers,true,false,null, objects, and arrays are valid. - Unescaped characters in strings. A literal newline or an unescaped
"inside a string breaks it. Use\nfor newlines and\"for embedded quotes.
The most misunderstood error: “Unexpected token < at position 0”
This one rarely means your JSON has a typo. A < at position 0means the very first character was an angle bracket — which almost always means the server returned HTML, not JSON. You requested an endpoint, got back a 404 or 500 error page, and your code tried to JSON.parse the <!DOCTYPE html> that came with it.
When you see this, stop debugging the JSON and look at the raw response body and the HTTP status code instead. The fix is on the request side — a wrong URL, a missing auth header, or an actual server error — not in the JSON syntax.
A note on encoded payloads
Sometimes the “broken” JSON is actually a perfectly valid string that contains encoded data. If a field looks like gibberish but parses fine, it may be Base64 or percent-encoded text. Pull the value out and decode it with the Base64 decoder or the URL decoder to read it. Once your JSON parses cleanly, the rest of your code can finally get to work.