The Unexpected token error is the single most common JSON error developers hit. It means the parser found a character that violates the JSON specification and stopped. The error message points at a position, but that position rarely tells you what is actually wrong. This guide covers all 7 common causes with instant fixes.

What the error actually means

JSON looks like JavaScript, but it is much stricter. When JSON.parse() reads your string character by character, it builds an expectation of what should come next. The moment it sees something that breaks the grammar, it throws:

SyntaxError: Unexpected token } in JSON at position 42
SyntaxError: Unexpected token ' in JSON at position 1
Unexpected token o in JSON at position 1

The "token" is the offending character. The position is the character index where parsing failed. Below are the causes, ordered by how often they occur.

Cause 1: Trailing commas

This is by far the most common cause. JavaScript allows a comma after the last item in an object or array. JSON does not.

// Invalid JSON — trailing comma after "true"
{
  "name": "test",
  "active": true,
}

Fix: Remove the comma after the last property in every object and the last element in every array.

Cause 2: Single quotes instead of double quotes

JSON requires double quotes for both keys and string values. Single quotes are never valid. This happens constantly when developers copy Python dictionaries or JavaScript object literals into a JSON context.

// Invalid — single quotes
{'name': 'Alice'}

// Valid — double quotes
{"name": "Alice"}

Fix: Replace all single quotes with double quotes. Be careful with apostrophes inside values.

Cause 3: You received HTML, not JSON

If the error is Unexpected token < in JSON at position 0, the < is the start of an HTML tag, usually <!DOCTYPE html>. Your API returned an HTML error page instead of JSON.

fetch('/api/data')
  .then(r => r.json())  // fails — body is "<html>..."

Fix: Check the response status code and the Content-Type header before parsing. Log response.text() to see what actually came back.

Cause 4: Unquoted keys

Every key in JSON must be wrapped in double quotes. JavaScript object literals do not require this.

// Invalid
{name: "Alice"}

// Valid
{"name": "Alice"}

Cause 5: Comments

Standard JSON does not support comments — neither // nor /* */. Remove all comments before parsing, or use YAML or JSON5 if you need them.

Cause 6: Invalid values (undefined, NaN)

JSON only supports strings, numbers, objects, arrays, true/false, and null. Values like undefined, NaN, and Infinity are not valid — use null or 0 instead.

Cause 7: A Byte Order Mark (BOM)

Files saved by some Windows editors include an invisible BOM at the start. The JSON looks perfect but fails at position 0.

const clean = text.replace(/^\uFEFF/, '');
JSON.parse(clean);

Find the error instantly — JSON Formatter & Validator

Paste your JSON and get the exact line and column of the error highlighted. Free, runs in your browser, no upload.

Open tool →

How the same error looks in other languages

  • Python: JSONDecodeError: Expecting property name enclosed in double quotes
  • Go: invalid character '}' looking for beginning of object key string
  • Java (Jackson): Unexpected character: was expecting double-quote to start field name

How to prevent it

  • Never hand-write JSON — generate it with JSON.stringify() or your serializer.
  • Validate JSON in CI before it ships.
  • Always check HTTP status and Content-Type before calling .json().