JSON Unexpected Token Error Fix

Unexpected token errors mean the parser found a character that is not valid at that position. Often the input is not JSON at all.

Symptoms

  • JSON.parse throws Unexpected token.
  • The token is <, which often means HTML was returned.
  • API client fails while browser shows an error page.

Likely causes

  • Server returned HTML instead of JSON.
  • JSON contains comments, trailing commas or single quotes.
  • Content-Type or endpoint URL is wrong.

Fix steps

  1. Inspect the raw response body before parsing.
  2. Check status code and Content-Type.
  3. Validate the JSON payload or response sample.

Verify the fix

  • Reproduce with curl -i.
  • Confirm response starts with { or [ for JSON APIs.
  • Handle non-2xx responses before JSON.parse.

FAQ

What does Unexpected token < mean?

It often means HTML was parsed as JSON, such as a login page or server error page.

Can formatting fix every parse error?

No. First confirm the input is actually JSON.

Related tools and guides

Last updated: May 18, 2026