Skip to main content

Questions Β· Developer Tools Β· 2 min read

Why Is My JSON Invalid?

JSON’s grammar is stricter than JavaScript’s, so code-style habits break it: trailing commas, single-quoted strings, unquoted object keys, and word-processor smart quotes are the four most common faults. Paste the text into a formatter and the error position plus one of these four usually solves it in under a minute.

The four usual suspects

Trailing commas head the list: {"a": 1,} parses fine in modern JavaScript and fails in JSON, which allows commas only between items, never after the last one. Arrays hide the same trap on their final element.

Single quotes come from languages where they are interchangeable; JSON permits double quotes only. Unquoted keys look natural to JavaScript eyes ({name: "x"}) but every key must be a quoted string. Smart quotes arrive via copy-paste from documents and chat apps, where "curly" quotation marks silently replace straight ones and parsers reject them outright.

Reading error messages efficiently

Parsers report the first line and column where confusion began, which is often slightly after the actual mistake: a missing comma is reported at the start of the next unexpected token. Read the reported position, then check immediately before it.

A formatter that pretty-prints on success doubles as your validator: if it refuses, the message points at the fault; if it succeeds, your JSON is genuinely well-formed regardless of how suspicious it looks.

Structural failures beyond syntax

Valid JSON can still be wrong JSON: duplicated keys (last one wins silently), numbers as strings ("42"), and deeply nested arrays where objects were intended all parse cleanly and misbehave later. After syntax passes, eyeball the formatted structure; indentation makes shape mistakes obvious in seconds.

Worth knowing before you start

  • BOM characters at file starts (from Windows editors) break strict parsers invisibly; re-save without BOM.
  • Comments are invalid in JSON despite decades of requests; strip them before validation.
  • Very large files may exceed browser tab comfort; validate samples or use streaming tools for giant payloads.

Frequently asked questions

Does JSON allow comments?
No. The specification omits them deliberately. Use a "//": "note" key convention or keep documentation beside the data rather than inside it.
Why does my JSON work in JavaScript but fail elsewhere?
JavaScript object literal syntax is a superset of JSON: it forgives trailing commas, bare keys, and single quotes. Code that never leaves JS tolerates these; anything speaking real JSON does not.
All questions β†’
All guides β†’
All articles β†’