Home / JSON Error Fixes / Unexpected Token in JSON

Unexpected Token in JSON: How to Fix It

This error usually means you are parsing non-JSON content as JSON, or your payload is malformed before parse time. Use this workflow to isolate the source and fix quickly.

Fast Diagnosis Checklist

  • Inspect raw response body before calling JSON.parse or response.json().
  • Confirm content-type is application/json.
  • Run payload through JSON Validator.
  • Reformat payload in JSON Formatter to locate breakpoints.

Typical Root Causes

  • Server returned HTML error page (token < at position 0).
  • Trailing commas or missing quotes in JSON body.
  • Double-encoded escaped JSON string not decoded first.
  • Unexpected BOM or invisible characters at payload start.

Minimal Repro Example

const raw = "<html>Not JSON</html>";
JSON.parse(raw); // Unexpected token < in JSON at position 0

Fix Workflow

  1. Log the raw response string (do not parse yet).
  2. Validate in JSON Validator.
  3. If it is JSON but escaped, decode with String to JSON.
  4. Compare expected vs actual payloads using JSON Diff.

Language Snippets

JavaScript

const text = await response.text();
console.log(text.slice(0, 200));
const data = JSON.parse(text);

Python

import json
raw = response.text
json.loads(raw)

FAQ

Why does this happen only in production?

Production responses often include error pages, CDNs, or auth redirects that do not exist in staging.

Should I auto-retry parsing?

No. Inspect the raw response first. Retrying without fixing the source usually hides the real issue.

About the Author

Formatterjson.org Editorial Team

We build and maintain formatterjson.org, a privacy-first suite of JSON, XML, YAML, and conversion tools used by developers and data teams. Our guides are based on real debugging workflows and tool usage patterns.

Last updated: March 16, 2026

Explore More SEO Hubs