Learn JSON: syntax, validation and common errors
Everything you need to read, write and debug JSON with confidence, including what the different JSON specifications actually mean when you pick one in a validator.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based format for exchanging data. It was popularized by Douglas Crockford in the early 2000s and has become the default format for web APIs, configuration files and data storage. Despite the name, JSON is language-independent: practically every programming language can parse and produce it.
Here is a small but complete JSON document describing a mechanical keyboard. It uses five of the format's building blocks at once:
{
"board": "Redwood 65",
"keys": 68,
"switches": ["Holy Panda", "Boba U4"],
"hotswap": true,
"warranty": null
}
An object wrapped in { }, a string, a number, an array of strings, a boolean and a null value. That is essentially all of JSON. Paste it into the editor and change a value to see validation and formatting react live.
JSON data types explained
JSON has exactly six value types. Everything you build is a combination of these:
| Type | Example | Notes |
|---|---|---|
| String | "cold brew\ttall" | Always double quotes. Supports escapes like \n, \t, \", \\ and \uXXXX. |
| Number | -3.14, 2.5e10 | No leading zeros (01 is invalid), no NaN/Infinity, no hex. |
| Boolean | true, false | Lowercase only. |
| Null | null | Lowercase only. Represents “no value”. |
| Object | {"city": "Lisbon"} | Unordered key/value pairs. Keys must be double-quoted strings. |
| Array | [9, "amp", null] | Ordered list; elements may be of mixed types. |
Common JSON errors and how to fix them
These account for the vast majority of “invalid JSON” failures. PerfectJSON pinpoints each one with a line and column number, and its Auto-fix button repairs most of them in one click.
1. Trailing commas
{ "roast": "medium", "shots": 2, } ← invalid
{ "roast": "medium", "shots": 2 } ✓ valid
JavaScript tolerates a comma after the last item; JSON never does.
2. Single quotes
{ 'city': 'Lisbon' } ← invalid
{ "city": "Lisbon" } ✓ valid
3. Unquoted keys
{ city: "Lisbon" } ← invalid (valid JavaScript, not JSON)
{ "city": "Lisbon" } ✓ valid
4. Comments
{
"retries": 3 // give up after three attempts ← invalid
}
Standard JSON has no comment syntax. Variants like JSONC (used by VS Code settings) and JSON5 allow them, but strict parsers reject them.
5. Missing commas or brackets
A forgotten , between properties or an unclosed {/[ cascades into confusing errors further down the document. Validate early and often — you can check it in the editor as you type.
6. Special values from other languages
NaN, Infinity, undefined and Python's None/True/False are all invalid in JSON. Use null, true, false or strings.
JSON specifications: RFC 8259 vs RFC 7159 vs RFC 4627 vs ECMA-404
JSON has been standardized several times. The grammar barely changed. What changed is what a document is allowed to contain at the top level and how it should be encoded. This is exactly what the validation dropdown in PerfectJSON selects.
| Spec | Year | Key points |
|---|---|---|
| RFC 4627 | 2006 | The original IETF spec. The top-level value must be an object or an array. A bare "string" or 42 is not a valid JSON text. |
| ECMA-404 | 2013 | The ECMA standard. Pure grammar: any JSON value (including strings, numbers, booleans, null) is a valid document. Says nothing about encoding or interoperability. |
| RFC 7159 | 2014 | Relaxed RFC 4627: any value may appear at the top level. Recommends UTF-8 and warns about duplicate keys and number precision. |
| RFC 8259 | 2017 | The current standard. Same grammar as RFC 7159, but UTF-8 is required for JSON exchanged between systems, and a byte-order mark must not be added. |
Practical takeaway: validate against RFC 8259 unless you must interoperate with a legacy system that expects RFC 4627's object-or-array rule.
JSON numbers and precision limits
The JSON grammar puts no limit on number size, but most parsers (including JavaScript's JSON.parse) decode numbers as IEEE-754 doubles. Integers are only exact up to 2^53 − 1 = 9007199254740991. Beyond that, digits are silently rounded:
JSON.parse('{"ledgerId": 9007199254740993}').ledgerId
// → 9007199254740992 (the id changed by one!)
This bites hard with database IDs and ledger entries, where being off by one is not a rounding nicety but a wrong record. Fixes: transmit big IDs as strings, or use a lossless parser. PerfectJSON auto-detects long integers and switches to its lossless parser so the digits survive formatting and editing.
How JSON validation works
A JSON validator does two things: it tokenizes the text (splitting it into strings, numbers, punctuation) and then parses the token stream against the JSON grammar. When something violates the grammar, a good validator reports where (line and column) and what was expected. “Expected ',' between properties” is fixable in seconds, while a generic “Unexpected token” is not. PerfectJSON additionally keeps parsing after the first error so you can see and fix several problems in one pass, and highlights each error range directly in the text. You can watch this happen in the PerfectJSON editor.
Frequently asked questions
Are comments allowed in JSON?
No. Standard JSON has no comments. JSONC and JSON5 add them, but strict parsers reject them. Auto-fix in the editor strips // and /* */ comments for you.
Are trailing commas valid?
No. A comma after the last array element or object property is invalid, even though JavaScript allows it.
Can I use single quotes?
No. Strings and keys must use double quotes.
What's the difference between RFC 8259 and ECMA-404?
Same grammar; RFC 8259 adds interoperability rules (UTF-8 required, duplicate-key and precision warnings). Syntactically, a document valid under one is valid under the other.
Does JSON support big numbers?
The grammar allows them, but typical parsers lose precision above 253 − 1. Use strings or a lossless parser for big integers.
Is NaN or Infinity valid?
No. JSON numbers must be finite. Use null or a string convention.