7 min read

Common JSON Errors and How to Fix Them

Ninety percent of "Unexpected token" errors come from the same seven mistakes. Here they are with before-and-after fixes you can pattern-match in seconds.

JSON's grammar is deliberately tiny - and strictly enforced. Most parse errors happen because JSON looks like JavaScript but forbids half of JavaScript's conveniences. Below are the seven classics, each with a broken and fixed version. When you just want the answer, paste your text into our JSON Formatter - it validates, points at the failing position, and pretty-prints or minifies, entirely in your browser.

1. Trailing Commas

JavaScript tolerates a comma after the last item; JSON never does. This is the single most common error when hand-editing config files.

// Before - invalid
{ "name": "Ada", "age": 36, }
// After - valid
{ "name": "Ada", "age": 36 }

2. Single Quotes

JSON strings use double quotes only - for keys and values alike. Apostrophes inside a double-quoted string are fine.

// Before - invalid
{ 'title': 'It\'s ready' }
// After - valid
{ "title": "It's ready" }

3. Unquoted Keys

Object keys must be quoted strings, even simple identifier-like ones.

// Before - invalid
{ status: "active", count: 3 }
// After - valid
{ "status": "active", "count": 3 }

4. Missing Brackets or Braces

Every { needs its } and every [ its ] - deep nesting makes the missing one hard to spot by eye, and the error often points at the end of the file.

// Before - invalid (array never closed)
{ "items": [1, 2, 3, "next": true }
// After - valid
{ "items": [1, 2, 3], "next": true }

A formatter is the fast fix here: pretty-printing re-indents by nesting depth, so the structure that never closed becomes visually obvious.

5. Comments

Standard JSON has no comments, by design. Some editors accept JSONC (JSON with comments) for their own config files, which trains bad habits for everywhere else.

// Before - invalid
{
  // retry twice
  "retries": 2
}
// After - valid
{
  "_comment": "retry twice",
  "retries": 2
}

6. NaN, undefined, and Friends

Valid JSON values are exactly: string, number, object, array, true, false, null. JavaScript's undefined, NaN, and Infinity are not on the list.

// Before - invalid
{ "score": NaN, "rank": undefined }
// After - valid
{ "score": null, "rank": null }

7. BOM and Invisible Characters

The sneakiest one: the file looks perfect but fails on the first character. Files saved as "UTF-8 with BOM" begin with three invisible bytes (EF BB BF) that strict parsers reject, and text pasted from Word or chat apps carries curly quotes (“ ”) and non-breaking spaces that are not the ASCII characters JSON requires. Fix: save as UTF-8 without BOM, and re-type any quotes that came from a document. A validator that reports "unexpected character at position 0" is almost always describing a BOM.

Find the Error in One Paste

Validate, pretty-print, or minify any JSON - errors located by line and column, nothing uploaded anywhere

Open the JSON Formatter →

JSON Is Not a JavaScript Object Literal

The root cause behind errors 1-6 is one misconception. JSON was derived from JavaScript syntax, but it is a stricter, language-independent data format: double quotes only, quoted keys, no trailing commas, no comments, no expressions, and only the seven value types. Every object literal your JavaScript accepts is not necessarily JSON - though every JSON document is (almost) valid JavaScript.

The practical rule: never build JSON by concatenating strings by hand. Construct a real object and let JSON.stringify() serialize it - the output is guaranteed valid - and parse with JSON.parse() rather than eval(), which would happily execute malicious input. Hand-editing is fine for config files; just run the result through a validator before deploying, because a single trailing comma can take down a build.

Frequently Asked Questions

What does "Unexpected token" in a JSON error actually mean?

The parser read your file character by character and hit something the grammar forbids at that position - the message names the character and usually a position like "line 3 column 18". The real mistake is often just before the reported spot: a missing comma on the previous line makes the next quote "unexpected". Start looking one token back from the position given.

Why is JSON valid in my code but rejected by an API?

Because JavaScript object literals accept many things JSON does not: single quotes, unquoted keys, trailing commas, comments, undefined, and NaN. If you built the payload by hand-writing a string, any of those slip through. Build payloads with JSON.stringify on a real object instead - it always emits valid JSON.

How do I put comments in a JSON file?

Standard JSON has no comment syntax - // or /* */ makes the file invalid. Workarounds: use a tolerant superset where the consumer allows it (JSONC in VS Code configs, JSON5), or add a conventional dummy key like "_comment": "text". For data exchanged between systems, leave comments out entirely.

Why does my JSON fail to parse when it looks completely fine?

Suspect invisible characters. Files saved as "UTF-8 with BOM" start with three hidden bytes that strict parsers reject, and text copied from chat apps or word processors often contains curly quotes or non-breaking spaces that look identical to the real thing. Re-type the suspicious quotes, save without BOM, or run the text through a validator that pinpoints the offending character.

Are numbers like NaN, Infinity, or 007 valid JSON?

No. JSON numbers must be plain decimals - no NaN or Infinity (use null or a string instead), no leading zeros (007 is invalid, 7 is fine), and no hex like 0xFF. Leading-plus signs are also out (+5 is invalid). Scientific notation like 1.5e10 is allowed.

Stop Debugging JSON by Eye

Paste, validate, format, or minify - errors pinpointed by line and column, free and entirely in your browser.

Try the JSON Formatter →