How to Fix Invalid JSON: The 7 Errors Behind Almost Every Failure

How to Fix Invalid JSON: The 7 Errors Behind Almost Every Failure

Why JSON breaks so easily

JSON has a deliberately tiny rulebook - that is its strength and its frustration. There is no room for the conveniences other formats allow, so one stray character anywhere makes the entire document unparseable. The good news: invalid JSON almost always traces to one of seven specific mistakes, and a formatter points you straight to the offending position.

The seven usual suspects

Run down this list against your broken JSON:

  • Trailing comma - a comma after the last item in an object or array. JSON forbids it; JavaScript allows it, which is why it sneaks in.
  • Single quotes - JSON requires double quotes. 'name' must be "name".
  • Unquoted keys - {name: "x"} is invalid; keys need quotes: {"name": "x"}.
  • Comments - JSON has no comments. // and /* */ both break it.
  • Missing or extra commas between items.
  • Unescaped characters - literal newlines, tabs or unescaped quotes inside strings.
  • Wrong brackets - closing an object with ] or an array with }.

Trailing commas: the number one cause

By far the most common error. You delete the last item from a list and leave its comma behind, or you add a comma in anticipation of another item that never comes. JSON treats the dangling comma as a promise of more data, then hits the closing bracket and fails.

A formatter highlights the exact line. Paste your JSON into our free JSON Formatter and it flags the position of the first error - usually a comma sitting just before a } or ].

Quotes and keys: JSON is stricter than JavaScript

People copy object literals out of JavaScript code and expect them to be valid JSON. They often are not. JavaScript happily accepts single quotes, unquoted keys and trailing commas; JSON accepts none of them. If your 'JSON' came from a .js file, assume all three problems and fix them before parsing.

Convert single to double quotes, wrap every key in double quotes, and remove trailing commas - that alone fixes the majority of pasted-from-code failures.

A reliable debugging workflow

When JSON will not parse: paste it into a formatter to get the error line, jump to that line and check it against the seven-item list, fix the one error, and re-validate (fixing one error often reveals the next). Formatting also pretty-prints the structure, which makes a misplaced bracket obvious at a glance. The whole loop runs in your browser, so API responses with sensitive data stay local.

Frequently asked questions

What is the most common JSON error?

A trailing comma - a comma after the final item in an array or object. JSON forbids it even though JavaScript allows it.

Why does my JSON work in JavaScript but not as JSON?

JavaScript accepts single quotes, unquoted keys and trailing commas; strict JSON rejects all three. Convert quotes to double, quote every key, and drop trailing commas.

Can JSON have comments?

No. The JSON standard has no comment syntax. Remove // and /* */ comments, or move them into a dedicated string field if you must keep notes.

How do I find which line of my JSON is broken?

Paste it into a JSON formatter - it reports the exact position of the first syntax error and pretty-prints the structure so misplaced brackets stand out.

Free tools mentioned in this guide