JSON Syntax & Validation: The Complete Guide
JSON (JavaScript Object Notation) is a strict, minimal data-interchange format defined by its own specification (RFC 8259) — it looks like a JavaScript object literal but follows stricter, narrower rules, and that gap is where almost every "why won't this parse" problem comes from. This guide covers the actual grammar, the specific mistakes that break it, and how to read a validator's error message instead of scanning the whole file by eye.
The full JSON grammar, in practice
JSON has exactly six data types, and that's the entire vocabulary — nothing else is valid inside a JSON document:
- Object —
{ "key": value, ... }. Keys must be double-quoted strings; unquoted keys are invalid. - Array —
[ value, value, ... ]. Elements can be any mix of the six types. - String — double-quoted only. Single quotes are never valid string delimiters.
- Number — no leading zeros (
007is invalid), no trailing decimal point without digits, noNaNorInfinity— all three are valid in JavaScript but not in JSON. - Boolean — lowercase
true/falseonly. - Null — lowercase
nullonly.
| Type | Valid syntax | Common mistake |
|---|---|---|
| Object | {"key": value} | Unquoted key |
| Array | [value, value] | Trailing comma |
| String | Double-quoted only | Single quotes |
| Number | 42, 3.14 | Leading zero, NaN/Infinity |
| Boolean | true / false | Capitalized True |
| Null | null | Capitalized or undefined |
That's it. No dates, no functions, no undefined, no comments, no trailing commas — everything else a JavaScript object literal allows that isn't on this list is a JSON syntax error.
Why JSON only looks like JavaScript
JSON's syntax was deliberately modeled on JavaScript object/array literals for familiarity, but it was standardized as its own independent format (RFC 8259, and mirrored in ECMA-404) — not a JavaScript feature. JavaScript itself has since relaxed its own object literal syntax in ways JSON never adopted: modern JS allows trailing commas, unquoted identifier keys, single-quoted strings, comments, and values like undefined and NaN. None of that is valid JSON, which is exactly why copy-pasting a JS object literal into a .json file is the single most common source of "valid-looking" JSON that actually fails to parse.
The four mistakes that cause almost every parse error
- Trailing commas.
{"a": 1, "b": 2,}— that comma after2is invalid. Easy to leave behind when reordering or deleting the last item in an object or array. - Single-quoted strings.
{'key': 'value'}— JSON requires double quotes for every string and every key, with no exceptions. - Unquoted keys.
{key: "value"}— valid as a JS identifier key, invalid in JSON; the key needs quotes too:{"key": "value"}. - Comments. Neither
// linenor/* block */comments exist in the JSON spec at all. A stray comment left in from editing will break the entire parse, not just get ignored.
A correct version of a commonly broken example:
{
"name": "Frost Rank",
"tools": 13,
"live": true
}
versus the broken JS-literal version that looks almost identical:
{
name: 'Frost Rank',
tools: 13,
live: true, // trailing comma
}
Reading a "line X, column Y" error correctly
A parser reports the position where it could no longer continue matching valid JSON grammar — which is usually at or extremely close to the actual mistake, but occasionally one token later, because the parser only realizes something's wrong once it hits the next unexpected character. A missing closing brace, for instance, often gets reported at the very end of the file (where the parser ran out of input) rather than at the actual line where the brace should have been. When the reported line looks unremarkable, check the line immediately before it — that's the second most likely location. This is exactly the gap Frost Rank's JSON Formatter & Validator was built to close: it uses the same grammar JSON.parse does, with a dedicated error locator that points at the precise line and column instead of a vague browser console message.
Beautify vs. minify — and when to use each
Beautifying adds indentation and line breaks purely for human readability — useful when debugging a response body or reviewing a config file by eye. Minifying strips every unnecessary whitespace character to shrink the file size — useful for anything transmitted over a network, like an API payload or an embedded config blob, where every byte affects transfer time. The data is identical either way; only the formatting changes.
Minified JSON also hides inside other formats. The payload of a JSON Web Token is a minified JSON object encoded as Base64URL, so the same grammar rules apply to every claim in it, and a JWT decoder can unpack and beautify it in one step when you need to read one.
Frequently asked questions
Because JSON has its own strict specification (RFC 8259) that never allowed them; JavaScript added trailing-comma support later as a convenience. See how JSON and JavaScript differ.
No — the JSON specification has no comment syntax at all, neither // nor /* */. This is deliberate: JSON was designed purely as a data format, not a config language. If you need comments, either use a superset format like JSON5 or JSONC (not standard JSON, and not universally supported by parsers) or a different file format like YAML for anything meant to be hand-edited with explanatory notes.
It means the parser hit a character it couldn't fit into any valid JSON grammar rule at that exact position — most often a stray comma, a missing closing brace/bracket, an unescaped quote inside a string, or a bare (unquoted) object key. The position given (line and column) is the parser's best attempt to say exactly where it gave up, which is usually very close to, though not always exactly at, the actual mistake.
The JSON specification only allows double quotes for strings and keys; single quotes are a JavaScript feature JSON never adopted. See the four most common parse errors.
They're related but not identical. A formatter (or beautifier) reindents and re-spaces already-valid JSON to make it readable, or does the reverse (minifies) to make it compact — it assumes valid input. A validator specifically checks whether the JSON is syntactically correct in the first place and reports where it fails if not. A good tool, like Frost Rank's JSON Formatter & Validator, does both: it validates first and only formats if the JSON actually parses.
Per the JSON specification, no — an object is an unordered collection of key/value pairs, and two JSON documents with the same keys/values in different orders are considered equivalent data. In practice, though, most real-world parsers (including JavaScript's own) preserve insertion order when they build an in-memory object, so order often survives round-trips even though the spec doesn't require it — don't rely on that behavior across every language and library, since it isn't guaranteed.
Written directly from building Frost Rank's own JSON Formatter & Validator — the exact grammar and error-position behavior described above match the parser actually shipped on that tool, not a general description copied from the spec.
Was this guide helpful?
Rate it and leave a comment — takes 10 seconds, and genuinely helps decide what to fix or build next.