• JSON
  • Debugging
  • JavaScript

Why your JSON won't parse: 8 errors and how to fix each one

Trailing commas, single quotes, comments, NaN and more. What each JSON.parse error message actually means, why it happens, and the quickest fix.

The utilyx team8 min read
On this page

JSON looks like JavaScript, so it's easy to write JavaScript and call it JSON. Then JSON.parse throws something unhelpful like:

text
SyntaxError: Expected double-quoted property name in JSON at position 8 (line 1 column 9)

JSON is deliberately strict. The whole format fits on a business card, and anything outside that card is an error. Below are the eight mistakes behind almost every parse failure, with the exact message Node.js 22 (V8) prints for each. Chrome uses the same engine, so its messages match. Firefox and Safari word them differently, but the cause is the same.

1. Trailing commas

json
{ "name": "utilyx", "tools": 43, }
text
Expected double-quoted property name in JSON at position 33 (line 1 column 34)

JavaScript allows a comma after the last item. JSON doesn't. The parser sees the comma, expects another "key", and finds } instead.

Fix: delete the comma before } or ]. This is the most common error by far, especially in hand-edited config files.

2. Single quotes

json
{ 'name': 'utilyx' }
text
Expected property name or '}' in JSON at position 2 (line 1 column 3)

JSON strings use double quotes only, for keys and values alike.

Fix: swap ' for ". Be careful with apostrophes inside text, like "it's": those stay as they are.

3. Unquoted keys

json
{ name: "utilyx" }
text
Expected property name or '}' in JSON at position 2 (line 1 column 3)

Same message as single quotes, because the parser is asking the same question: where's the double-quoted key? Every key must be a string in double quotes, even simple ones.

Fix: wrap each key in "…".

4. Comments

json
{
  "port": 3000 // dev only
}
text
Expected ',' or '}' after property value in JSON at position 17 (line 2 column 16)

JSON has no comments. Not //, not /* */, not #.

Fix: remove the comments. If you need them in a config file, you probably want a format that supports them: JSONC (used by VS Code's settings.json and tsconfig.json), JSON5, or YAML. Keep in mind that a file with comments parses fine in the tool that expects JSONC and fails everywhere else.

5. NaN, Infinity and undefined

json
{ "ratio": NaN, "limit": Infinity, "value": undefined }
text
Unexpected token 'N', ..." "ratio": NaN, "limi"... is not valid JSON

These are JavaScript values, not JSON values. JSON only knows strings, numbers, true, false, null, objects and arrays.

This often comes from code, not typing. JSON.stringify quietly turns NaN and Infinity into null and drops keys whose value is undefined. But a template, a logger or a Python script (json.dumps writes NaN by default) can still produce invalid output.

Fix: use null, or represent the special value as a string like "Infinity" that your code understands. In Python, pass allow_nan=False to catch it early.

6. Numbers JSON doesn't allow

json
{ "id": 007, "mask": 0x1F, "delta": +1, "half": .5 }
text
Unexpected number in JSON at position 9 (line 1 column 10)

JSON numbers are plain decimals. These are all invalid:

  • Leading zeros: 007. Use 7, or "007" if the zeros matter (like a postcode).
  • Hex: 0x1F. Use 31.
  • A leading +: +1. Use 1.
  • A bare decimal point: .5. Use 0.5.

Fix: write the plain decimal, or store the value as a string if its exact formatting matters.

7. Raw line breaks and tabs inside strings

json
{ "message": "line one
line two" }
text
Bad control character in string literal in JSON at position 22 (line 1 column 23)

A JSON string can't contain an actual line break or tab character. It has to be escaped.

Fix: use \n for a new line and \t for a tab. If you're building JSON by gluing strings together, stop: build an object and call JSON.stringify, which escapes everything correctly.

8. A hidden character at the start

text
Unexpected token '', "{"a":1}" is not valid JSON

The JSON looks perfect, yet the error points at position 0 and shows a "token" you can't see. That's a byte order mark (BOM), an invisible character some editors (notably on Windows) add to the start of a file. Zero-width spaces pasted from chat apps and web pages cause the same thing.

Fix: save the file as "UTF-8" rather than "UTF-8 with BOM". In code, strip it before parsing:

js
JSON.parse(text.replace(/^\uFEFF/, ""));

Two traps that don't throw

Some JSON parses without errors and is still wrong.

Big integers lose precision. JavaScript numbers are 64-bit floats, so integers above 2^53 (9,007,199,254,740,992) get rounded:

js
JSON.parse('{"id": 9007199254740993}').id;
// 9007199254740992 (silently off by one)

This bites with database IDs, Twitter/X IDs and Discord snowflakes. Fix: send large IDs as strings. That's why many APIs return both id and id_str.

Duplicate keys are accepted, and the last one wins:

js
JSON.parse('{"role": "user", "role": "admin"}').role; // "admin"

Other parsers may keep the first value or throw. Don't rely on either behaviour; fix the source.

A faster way to find the problem

Reading error positions by hand works, but it's slow in a 2,000-line payload. A good formatter will:

  • point at the exact line and column,
  • explain the error in plain words,
  • and offer to repair the common cases above (trailing commas, single quotes, unquoted keys, comments) in one click.

The utilyx JSON formatter does all three, and it runs in your browser. That matters when the JSON you're debugging is an API response full of customer data or tokens.

Quick reference

Error message (V8)Likely cause
Expected double-quoted property nameTrailing comma before }
Expected property name or '}'Single quotes or unquoted keys
Expected ',' or '}' after property valueComment, missing comma, or hex number
Unexpected token 'N' / 'u' / 'I'NaN, undefined, Infinity
Unexpected numberLeading zero
Bad control character in string literalRaw line break or tab in a string
Unexpected token at position 0BOM or invisible character
Try the JSON formatter
Free, no sign-up, and it runs in your browser.
Open the tool →