Troubleshooting Knowledge Base

How to Fix JSON SyntaxError: Unexpected Token

Fix the "SyntaxError: Unexpected token" JSON parse error. Every cause explained: single quotes, unquoted keys, trailing commas, and HTML in the response.

"SyntaxError: Unexpected token" in JSON is caused by invalid syntax such as single quotes instead of double quotes, unquoted object keys, trailing commas, JavaScript comments, or an HTML error page being parsed as JSON. Paste the text into our JSON Validator to pinpoint the exact line.

What Causes "Unexpected Token" in JSON?

The SyntaxError: Unexpected token error occurs when JSON.parse() or any JSON parser encounters a character that violates RFC 8259 syntax. It is the most common JSON error developers face.

Top Causes (in order of frequency)

  1. Single quotes instead of double quotes: {'name': 'Alice'} → invalid. Use {"name": "Alice"}.
  2. Unquoted object keys: {name: "Alice"} → invalid. Keys must be double-quoted strings.
  3. Trailing comma: {"a": 1, "b": 2,} → invalid. Remove the comma after the last item.
  4. JavaScript comments: // comment or /* */ → invalid. JSON forbids comments.
  5. HTML in the response: An API returning an HTML error page (404/500) that your code tries to JSON.parse.
  6. Undefined or NaN values: {"value": undefined} or {"num": NaN} → invalid JSON primitives.
  7. BOM character: A Byte Order Mark () at the start of a UTF-8 file can cause an unexpected token at position 0.

Diagnosing the Error

Paste your JSON into our JSON Validator to get the exact line number and character position of the unexpected token.

json
// Invalid JSON — All will throw "Unexpected token"
{
  'name': 'Alice',       // ❌ Single quotes
  age: 30,               // ❌ Unquoted key
  "active": true,        // ❌ Trailing comma
  // This is a comment   // ❌ JavaScript comment
  "value": undefined     // ❌ undefined is not JSON
}

// Valid JSON — RFC 8259 compliant
{
  "name": "Alice",
  "age": 30,
  "active": true,
  "value": null
}

Try the free JSON Validator

Runs entirely in your browser — no upload, no signup, and your data never leaves your device.

Open JSON Validator

Frequently Asked Questions

What does "Unexpected token < in JSON at position 0" mean?

This means your code received an HTML page (starting with <) instead of JSON. Check that your API returns Content-Type: application/json and handle HTTP errors before calling JSON.parse.

How do I find which line has the unexpected token?

Use our JSON Validator tool which reports the exact line number and character position of every syntax error.

Can I use single quotes in JSON?

No. RFC 8259 requires all strings (both keys and values) to be wrapped in double quotes only.

Are comments allowed in JSON?

No. Comments are not part of the JSON standard. If you need a JSON variant with comments, consider JSONC or JSON5, but standard parsers reject them.

Related JSON Topics & Reference Articles