QuickWand
← All guides

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.

  1. Open the JSON Formatter and paste the JSON that failed to parse.
  2. 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.
  3. 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 this or /* 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 \n for 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.

Frequently asked questions

What does 'Unexpected token < in JSON at position 0' mean?
It means whatever you tried to parse as JSON did not start with valid JSON — it started with a '<'. Almost always this is an HTML error page (like a 404 or 500) being returned where you expected JSON. Check the actual response body and the HTTP status before debugging the JSON itself.
Why is a trailing comma invalid in JSON?
The JSON specification does not allow a comma after the last element of an array or the last property of an object. JavaScript object literals permit trailing commas, which trips people up — but strict JSON parsers reject them. Remove the comma before the closing ] or }.
How do I find which line my JSON error is on?
Paste the JSON into a validating formatter. QuickWand's JSON Formatter parses it in your browser and reports the parse error so you can jump to the problem area, instead of eyeballing thousands of characters by hand.
Can JSON have comments?
No. Standard JSON does not support // or /* */ comments, and a comment will cause a parse error. Some tools accept 'JSONC' (JSON with comments) for config files, but anything sent over a real JSON API must be comment-free.

Free tool

JSON Formatter

Beautify messy JSON, validate it with clear error messages, or minify it — all locally in your browser.

Try JSON Formatter— free →