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
Each example below shows the broken version, the error Frost Rank's JSON Formatter reports for it, and the fix.
1. Trailing commas
A comma after the last item in an object or array. Easy to leave behind when you delete or reorder the final line.
// Broken
{
"name": "Frost Rank",
"free": true,
}
// Line 4, column 1: Trailing comma is not allowed
// Fixed
{
"name": "Frost Rank",
"free": true
}
Notice the error points at line 4, the closing brace, not line 3 where the comma sits: the parser only knows the comma was wrong once it finds nothing after it.
2. Unquoted keys
Valid in a JavaScript object literal, invalid in JSON: every key needs double quotes.
// Broken
{
name: "Frost Rank",
"free": true
}
// Line 2, column 3: Expected a string key
// Fixed
{
"name": "Frost Rank",
"free": true
}
3. Single-quoted strings
JSON allows only double quotes, for keys and string values alike, with no exceptions.
// Broken
{
"name": 'Frost Rank',
"free": true
}
// Line 2, column 11: Unexpected token "'"
// Fixed
{
"name": "Frost Rank",
"free": true
}
If a string itself contains a double quote, escape it as \" rather than switching to single quotes.
4. Comments
Neither // line nor /* block */ comments exist in the JSON spec. A note left in from editing breaks the entire parse rather than being ignored, so delete it, or move it into a real field such as "_note": "..." if it has to travel with the data. (The // Broken and // Fixed labels above are annotations for this guide, not part of the JSON.)
All four usually arrive together when a JavaScript object literal is pasted into a .json file, which is why the section above matters. To find the exact line in your own file, paste it into the JSON validator, which reports these same messages.
Unescaped newlines inside strings
A fifth error turns up when text is pasted into a string by hand: a raw line break. RFC 8259 requires every control character in a string (U+0000 to U+001F) to be escaped, and the line feed is one of them, so a JSON string can't simply carry on to the next line.
// Broken
{
"note": "line one
line two"
}
// Line 2, column 20: Bad control character in string literal
// Fixed
{
"note": "line one\nline two"
}
The error points at column 20, the line break itself. Its wording comes from the browser's own JSON parser (this is Chrome's), so other browsers phrase it differently. Tabs follow the same rule and are written as \t. To check a multi-line value of your own, run it through the JSON formatter, which stops at the first raw break and gives its line and column.
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. Under RFC 8259 a trailing comma is invalid in both a JSON array and a JSON object: a comma may only sit between two values or two name/value pairs, so a comma right before ] or } is a syntax error. See how JSON and JavaScript differ.
RFC 8259, the JSON standard, keeps all three strict. An object is wrapped in curly braces and holds zero or more name/value pairs separated by commas, each name a double-quoted string followed by a colon (names should be unique). An array is wrapped in square brackets and holds zero or more comma-separated values of any JSON type, including nested objects and arrays. A string begins and ends with double quotation marks, and any quotation mark, backslash or control character inside it, a newline included, must be escaped. See the full grammar with examples.
Yes. JSON strings must use double quotes, and RFC 8259 defines every object key as a string, so keys need them too: {"name": "Frost Rank"} is valid, while {name: "Frost Rank"} and {'name': 'Frost Rank'} are not. To put a double quote inside a string, escape it as \" rather than switching quote styles. See the single-quote error and its fix.
RFC 8259 forbids control characters inside a string unless they are escaped, and a line break (U+000A) is one of them. That is why JSON strings cannot contain unescaped newlines: a string that runs onto a second line is a syntax error. Write the break as the two characters \n instead, and the parser turns it back into a real newline when it reads the value. See a before-and-after example.
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.
JSON5 is a relaxed superset of JSON meant for hand-written config files: it allows comments, trailing commas, single quotes, unquoted keys, hex numbers and Infinity or NaN. Every valid JSON file is valid JSON5, but not the other way round, and JSON5 needs its own parser. APIs, browsers and most tools expect standard JSON, so use JSON5 only where your tooling explicitly supports it.
Because valid syntax isn't the whole contract. The usual causes: a missing or wrong Content-Type header (it should be application/json), the body being sent as a string inside a string (double-encoded), an invisible byte-order mark or smart quotes from copy-pasting, a number too large for the API to store exactly, or the right syntax with the wrong shape: a missing required field, a string where the API expects a number. Read the API's error response; it usually names the field.
Paste the JSON into a validator that reports a line and column, then look at that spot and the line just before it, since a parser often notices a mistake one token late; a missing closing brace is usually reported at the very end of the file. Formatting the JSON first helps too: on one minified line, every error is "line 1". See how to read a line and column error.
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.
Available for SaaS & AI automation projects → Hire me · Portfolio · Services
Was this guide helpful?
Rate it and leave a comment — takes 10 seconds, and genuinely helps decide what to fix or build next.