Fix Unexpected Token JSON Errors

Published July 16, 2026By Samson PG

“Unexpected token” usually means the parser hit something that is not valid JSON — a comma, a quote, or an HTML login page pretending to be data.

SyntaxError: Unexpected token ... in JSON (or similar) means JSON.parse (or your language’s decoder) hit a character that valid JSON does not allow at that position. The message names a symptom; the fix is to find the bad byte and the producer that emitted it.

Usual culprits

Symptom / token Likely cause
Unexpected token , Trailing comma after last property/element
Unexpected token ' Single-quoted strings (JSON needs double quotes)
Unexpected token < HTML error/login page returned instead of JSON
Unexpected token u (undefined) Parsing undefined / empty body as JSON
BOM / weird first char File saved with UTF-8 BOM or concatenated junk

Comments (//, /* */) and bare NaN/Infinity are also invalid in strict JSON.

Debug steps that work

  1. Do not guess — paste the raw body into a formatter/validator and jump to the reported position.
  2. Confirm you are parsing the response body, not a JS object you already have.
  3. If the first non-space character is <, you got HTML — check status code, auth, and CDN error pages.
  4. Strip a leading BOM if a file editor added one.
  5. After it validates, pretty-print for humans or minify for transport — both need valid input first.

Minimal examples

Invalid (trailing comma):

{ "ok": true, }

Valid:

{ "ok": true }

Invalid (single quotes):

{ 'ok': true }

Use TryDevSnip JSON Formatter

TryDevSnip JSON Formatter pretty-prints, minifies, and validates in your browser so you can locate the bad token quickly. Handy mid-debug when the payload might include secrets — see also format JSON without pasting to the cloud.

FAQ

Is the line number in the error always right?

It is usually close. Minified one-line JSON reports a huge column — pretty-print first, then re-parse.

Why does it work in the console but fail in my app?

Different strings: escaped quotes, truncated logs, or middleware that wraps the body.

Can minify cause Unexpected token?

Minify of valid JSON stays valid. Minifying invalid input just fails earlier at parse time.

What about JSON5 / JSONC?

Those allow comments and other extensions. Strict JSON.parse will still throw — convert or use a matching parser.

← More from the blog