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.
Related questions
All questions β- UTC Offsets vs Time Zones: What Is the Difference?Offsets are static numbers; time zones are living histories of rules. Mixing them up is the root of most date bugs.
- SHA-256 vs SHA-512: Which Hash Should I Use?Both are excellent and neither is "broken". Pick by ecosystem defaults: 256 for broad compatibility, 512 where longer digests suit the job.
- What Actually Makes a Password Strong?Unpredictability measured in bits of entropy. Length contributes far more than symbol swaps, and humans are bad at both unaided.
- HEX, RGB, or HSL: Which Color Notation When?All three describe the same colors. HEX wins for copying, RGB for code with alpha, HSL for thinking about relationships.
Related guides
All guides β- Formatting JSON the Safe WayPretty-printing is trivial; doing it without pasting production payloads into random websites is the actual skill.
- Planning Meetings Across Time Zones Without the GuessworkMost scheduling pain comes from three avoidable mistakes: trusting abbreviations, converting with the wrong date, and ignoring that the meeting lands on someone elseβs tomorrow.
- Daylight Saving and Time Conversion: The Mistakes That Break SchedulesOffsets are answers to "at what date?", not properties of a zone. Every classic DST bug β drift, gaps, duplicates, hemisphere flips β follows from forgetting that.
- A Markdown Workflow for Project DocumentationDocumentation dies when writing it hurts. Markdown plus a tight preview loop keeps the cost near zero.
Related reading
All articles β- Reading Regex Without FearRegex looks like line noise because it compresses a whole language into punctuation. Learn the eight moves and any pattern becomes parseable.
- Entropy, Explained for Normal PeopleForget "complexity rules". One number predicts whether a password survives attack, and it rewards length far more than exclamation marks.
- Why UUID v4 Collisions Almost Never HappenPeople hear "random" and expect collisions eventually. The numbers say otherwise: the universe ends first.
- Client-Side Tools Change the Privacy MathEvery uploaded file is a promise about storage and deletion. Files processed locally require no promises at all.