JSON.parse Error in JavaScript: Common Causes and Fixes

Last updated:

JSON.parse() throws SyntaxError for 7 specific input mistakes: trailing commas ({a:1,}), single-quoted strings ('text'), unquoted keys ({name:"x"}), JavaScript comments (// ...), undefined values, NaN/Infinity, and raw newlines inside strings. V8's error message includes the character position — SyntaxError: Unexpected token {'}'} in JSON at position 12 — but points to where the parser noticed the problem, which is often 1–2 characters after the actual mistake. Always wrap JSON.parse() in a try/catch: an uncaught SyntaxError crashes a Node.js request handler and silently breaks browser code. This guide covers all 7 error types with the exact error string each runtime produces, plus safe parse wrappers and a TypeScript unknown cast pattern.

Catch the error first

Wrap parsing in try/catch so you can inspect the bad payload and error message instead of crashing the app.

try {
  const data = JSON.parse(raw)
} catch (error) {
  console.error('Invalid JSON:', raw)
  console.error(error)
}

For the full safe parsing flow, see JSON.parse in JavaScript.

Trailing commas

// Invalid
{"name":"Alice",}

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

JavaScript object literals sometimes allow patterns that JSON does not. Trailing commas are a common example.

Single quotes instead of double quotes

// Invalid JSON
{'name':'Alice'}

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

JSON strings and object keys must use double quotes. If the source looks like a JavaScript object literal, it may need cleanup before parsing.

Unquoted keys

// Invalid
{name:"Alice"}

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

Comments and invalid escape sequences

// Invalid JSON with comments
{
  // user name
  "name": "Alice"
}

// Invalid escape
{"path":"C:
ew	est"}

// Valid escaped backslashes
{"path":"C:\new\test"}

If you need a JSON-safe string literal, the patterns in JSON.stringify tutorial are useful.

Debug the exact broken character

When the error reports a position, log the surrounding slice of the string so you can see what is actually there.

try {
  JSON.parse(raw)
} catch (error) {
  const start = Math.max(0, 40)
  console.log(raw.slice(start, start + 120))
}

Prevent JSON.parse errors upstream

  • Always generate JSON with JSON.stringify(), not by manual string concatenation.
  • Validate pasted or uploaded JSON before storing it.
  • Be careful when reading JSON from localStorage or query strings.
  • Normalize nested payloads before logging or reusing them.

If your payload is deeply nested, parse nested JSON in JavaScript covers safer access patterns after parsing succeeds.

Find invalid JSON fast

Paste the payload into Jsonic's JSON Formatter & Validator to surface syntax errors before they hit your code.

Validate JSON

Frequently asked questions

What causes "Unexpected token" in JSON.parse?

The parser found a character where the JSON grammar does not allow one. Common causes: single quotes, trailing commas, unquoted keys, JavaScript comments, or keywords likeundefined and NaN.

How do I fix a trailing comma JSON error?

Remove the comma that appears after the last item in an object or array, before the closing} or ]. Use JSON.stringify to generate JSON programmatically and avoid trailing commas entirely.

What is the most common JSON syntax error?

Single quotes instead of double quotes. JSON requires double quotes everywhere. The second most common is a trailing comma, which is allowed in JavaScript object literals but forbidden in JSON.

How do I debug a JSON.parse SyntaxError?

Log the raw string before parsing. Use the error position to slice the string around the broken character. Paste the full payload into a JSON validator for a precise line and column number.

Can JSON have single quotes?

No. The JSON spec (RFC 8259) requires double quotes for all strings and keys. Single quotes are not valid anywhere in a JSON document.

What is the difference between JSON and JavaScript object literals?

JSON is stricter. It requires double quotes, forbids trailing commas, has no comment syntax, and only allows string, number, object, array, boolean, and null values. JavaScript object literals allow single quotes, unquoted keys, trailing commas, and more.