JSON Parse Error: Fix "Unexpected Token" and Other Syntax Issues

8 min readJSON Debugging

JSON parse errors stop your code cold. The error message — SyntaxError: Unexpected token ',' in JSON at position 47 — tells you where the parser failed but not why. This guide covers the 6 most common causes with before/after fixes you can apply in seconds.

Fastest fix: Paste your JSON into our JSON Formatter & Validator — it highlights the exact error line and suggests fixes.

The 6 Most Common JSON Parse Errors

Error 1: Trailing Comma

Most common cause — JavaScript allows it; JSON does not

INVALID

{
  "name": "Alice",
  "age": 30,  ← trailing comma
}

VALID

{
  "name": "Alice",
  "age": 30
}

Quick fix: Find & Replace with regex: ,(\s*[}]])$1

Error 2: Single Quotes Instead of Double Quotes

JSON requires double quotes everywhere — strings and keys

INVALID

{
  'name': 'Alice',
  'age': 30
}

VALID

{
  "name": "Alice",
  "age": 30
}

Quick fix: If the JSON came from a Python dict repr(), use json.dumps() instead to get proper JSON.

Error 3: Unexpected Token '<' at Position 0

Your server returned HTML, not JSON — usually an error page

// Your API returned this (a 404 page):
<!DOCTYPE html><html>...

// But your code tried to parse it as JSON:
const data = await response.json(); // ← SyntaxError!
// Fix: check response.ok first
const response = await fetch('/api/data');
if (!response.ok) {
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();

Debug tip: Check the Network tab in DevTools — look at the actual response body and status code.

Error 4: Unquoted Keys

JavaScript object literals allow unquoted keys; JSON does not

INVALID (JS object literal)

{
  name: "Alice",
  age: 30
}

VALID JSON

{
  "name": "Alice",
  "age": 30
}

Error 5: Unexpected token 'u' at Position 0

You're trying to parse undefined

// Common cause: variable wasn't initialized
let savedData;  // undefined
JSON.parse(savedData);  // ← SyntaxError: Unexpected token 'u'

// Also happens with localStorage:
const data = localStorage.getItem('key'); // returns null if missing
JSON.parse(data); // ← SyntaxError
// Fix: guard before parsing
const raw = localStorage.getItem('key');
const data = raw ? JSON.parse(raw) : null;

Error 6: Comments in JSON

JSON does not support comments — not even // single-line or /* block */

INVALID

{
  // API key for production
  "apiKey": "abc123",
  "timeout": 5000 /* ms */
}

VALID

{
  "_comment": "API key for production",
  "apiKey": "abc123",
  "timeout": 5000
}

Alternative: Use JSON5 format (a superset of JSON that allows comments) or YAML for config files that humans write.

Quick Debug Checklist

  • Check the character at the reported position — the error is usually just before it
  • Look for trailing commas after the last item in objects and arrays
  • Confirm all string values and keys use double quotes
  • If the error is at position 0, your value is undefined, null, or an HTML string
  • If you see "<" at position 0, your API returned an error HTML page — check the status code
  • Wrap JSON.parse() in try/catch and log the raw string to see what you're actually parsing

FAQ

Why does JSON not allow trailing commas?

JSON's specification (RFC 8259) was designed for strict, minimal interoperability across all languages. Trailing commas were excluded to keep the grammar unambiguous. JavaScript added trailing comma support later, but JSON intentionally stayed strict. If you want trailing commas in a JSON-like format, use JSON5.

How do I handle JSON parse errors gracefully?

Always wrap JSON.parse() in a try/catch block in production code. Never let a parse error crash your application. Pattern: try { const data = JSON.parse(raw); } catch (e) { console.error('Failed to parse JSON:', e.message, '\nRaw:', raw); }

What is the difference between JSON and JSON5?

JSON5 is a superset of JSON that adds support for trailing commas, single-quoted strings, comments, unquoted keys, and multi-line strings. JSON5 files are not parseable by the standard JSON.parse() — you need the json5 npm package. JSON5 is mainly useful for config files written by humans.

Related Resources