{FormJSON}
Developer Troubleshooting Guide JSON Debugging

How to Fix "Unexpected token in JSON at position X"

A step-by-step diagnostic breakdown for resolving JavaScript JSON.parse() errors and malformed API payloads.

Need to fix your malformed JSON right now?
Use our deterministic AST repair tool to patch syntax errors in one click.
Launch Safe Repair →

01. Unexpected token < in JSON at position 0

This is the single most common error encountered by frontend developers using fetch() or axios.

The Root Cause:

Your client code called response.json(), but the backend returned an HTML error page (starting with <!DOCTYPE html>) instead of JSON data. This happens when the endpoint returns a 404 Not Found, 500 Internal Server Error, or a reverse proxy gateway timeout (Cloudflare / Nginx error page).

The Fix:
// Safe fetch pattern: Check response.ok before parsing JSON
const res = await fetch('/api/user');
if (!res.ok) {
  const errorHtml = await res.text();
  throw new Error(`Server returned status ${res.status}: ${errorHtml.slice(0, 100)}`);
}
const data = await res.json();

02. Trailing Commas in Objects and Arrays

In JavaScript, Python, and TypeScript, trailing commas after the final element in an array or dictionary are valid and encouraged. However, RFC 8259 strictly forbids trailing commas in standard JSON.

✕ Invalid JSON (Causes Parser Crash)
{
  "name": "Alex",
  "role": "Admin",  <-- Trailing comma!
}
✓ Valid JSON (RFC 8259 Compliant)
{
  "name": "Alex",
  "role": "Admin"
}

03. Single-Quoted Strings and Unquoted Keys

Python str(dict), JavaScript object literal copies, and LLM completions often wrap strings in single quotes ('key': 'val') or omit quotes around keys ({key: "val"}).

Standard JSON parsers require all object property keys and string values to be wrapped in double quotes ("key": "value"). Use FormJson's Safe Repair Tool to instantly normalize them.

FAQ

Why do I get 'SyntaxError: Unexpected token < in JSON at position 0'?
This almost always means your server returned an HTML error page (like a 404 Not Found or 500 Internal Server Error starting with '<!DOCTYPE html>') instead of the expected JSON payload.
Why does JSON reject trailing commas?
The official JSON specifications (RFC 8259 and ECMA-404) explicitly disallow trailing commas in objects {'a': 1,} and arrays [1, 2,]. FormJson's Safe Repair engine can automatically clean these up.
Are single quotes valid in JSON strings?
No. JSON strictly requires double quotes (") around both string values and object property keys.
How can I automatically fix malformed JSON?
You can paste your invalid payload into FormJson's JSON Repair tool (/json-repair) to automatically fix trailing commas, unquoted keys, Python constants, and single quotes with an instant diff preview.

Explore Related Tools & Converters

100% Client-Side