JSONRepair

Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse

You pasted something that looks like perfectly good JSON, and the parser rejected it with a cryptic "Unexpected token" message. Nearly every failure traces back to one of these seven mistakes.

Here is the thing nobody tells you when you start working with JSON: it is not JavaScript. It looks like JavaScript, it was designed to be trivially parseable, and it borrows JavaScript's object notation, but the grammar is far stricter. Every relaxed habit you picked up writing object literals (trailing commas, single quotes, comments) is a landmine in JSON.

I have debugged hundreds of broken payloads over the years, and the distribution is comical. It is almost always one of seven things. Learn these and you will diagnose any parse error in under a minute.

1. Trailing commas (the number one offender)

A comma after the last item in an object or array is valid JavaScript but invalid JSON. It usually appears after someone deletes a line from the end of a list and leaves the comma behind.

{ "email": "ada@example.com", "age": 36, }
{ "email": "ada@example.com", "age": 36 }

The parser's error ("Unexpected token }" or "Expected property name") points at the closing brace, which is confusing, because the actual mistake is the comma just before it.

2. Single quotes instead of double quotes

JSON requires double quotes for all strings, including keys. No exceptions. This one bites anyone who copies Python dictionary syntax or hand-writes payloads in an editor.

{ 'name': 'Ada' }
{ "name": "Ada" }

Watch out for apostrophes in values: a global find-and-replace of single quotes will break "it's" into "it"s". That is a trap I have watched a repair script fall into.

3. Comments

Standard JSON has no comment syntax at all. Neither // nor /* */ is allowed. Config files that look like JSON, VS Code's settings.json, and tsconfig files actually use JSONC, which is a different format.

{ "port": 3000 // local dev port }
{ "port": 3000 }

Comments need to be stripped before parsing, and stripped carefully: a // inside a string value (like a URL) is not a comment. Use a string-aware parser, not a regex.

4. Unquoted keys

In JavaScript you can write { name: "Ada" }. In JSON every key must be a double-quoted string. This shows up when people copy an object literal out of their code and expect it to parse.

{ name: "Ada", role: "admin" }
{ "name": "Ada", "role": "admin" }

5. Smart quotes pasted from rich text

This is the sneakiest one. You copy a JSON snippet from a Word doc, a Google Doc, a Slack message, or an email, and the straight quotes have been "helpfully" replaced with curly typographic quotes. They look identical in most editors but are completely different characters, and the parser chokes on them.

{ “name”: “Ada” }
{ "name": "Ada" }

My rule: if JSON was copied out of any rich-text source and fails to parse, check for smart quotes first. It takes ten seconds and it is the cause more often than anyone expects.

6. Values JSON does not support

JSON has exactly six value types: string, number, boolean, null, array, and object. Anything else is invalid:

7. Unescaped control characters in strings

JSON strings cannot contain literal newlines, tabs, or other control characters. They must be escaped as \n and \t. This surfaces most often with multi-line text pasted directly into a string value, and with invisible characters like a stray byte-order mark at the start of a file.

{ "bio": "line one
line two" }
{ "bio": "line one\nline two" }

How to diagnose any parse error fast

My personal workflow, in order:

  1. Read the position in the error. "Unexpected token } at position 42" tells you where the parser gave up. The actual mistake is almost always just before that point.
  2. Check for the big seven above around that position, especially trailing commas and comments.
  3. Validate before you repair. If it is valid, something else is wrong (maybe the HTTP response was HTML, not JSON).
  4. Prevent, don't just fix. Generate JSON with JSON.stringify() instead of string concatenation and most of these errors never appear in the first place.

Paste your broken JSON and get valid JSON back instantly, with a plain-English list of every fix.

Fix My JSON Now

Frequently asked questions

Why does JSON not allow trailing commas?

JSON was designed for maximum simplicity and cross-language compatibility. Allowing trailing commas would add grammar complexity, so the spec forbids them. JavaScript object literals allow them, which is why this is the most common error when moving data between JS code and JSON.

Can I use comments in JSON?

Not in standard JSON. If you need comments, use JSONC (used by VS Code and TypeScript configs), JSON5, YAML, or TOML, and make sure your parser supports the format you choose.

What does "Unexpected token in JSON at position X" mean?

The parser hit a character that is illegal in that exact spot of the grammar. Count to that position in your text and look just before it: you will usually find a trailing comma, a stray quote, or a comment.

Related reading: JSON with Comments (JSONC): What It Is and How to Handle It