JSON formatting and validation: the rules and the common errors
Published . Last reviewed .
In short:JSON has a small, strict grammar defined in RFC 8259: double-quoted strings and keys, no trailing commas, no comments and only three literal words. Most broken JSON fails on one of a handful of mistakes. Formatting changes only the layout, minifying removes it, and validating checks the grammar.
What counts as JSON
JSON is a text format for data. The current standard is RFC 8259, published by the IETF. Its grammar is short enough to hold in your head:
- A JSON text is one value.
- A value is an object, an array, a string, a number, or one of three literal words:
true,falseandnull, in lower case. The RFC says plainly that “no other literal names are allowed”. - An object is a set of name and value pairs in curly braces:
{"name": "Asha"}. Each name is a string. - An array is a list of values in square brackets:
[1, 2, 3]. - A string starts and ends with double quotes. Inside, a double quote, a backslash and control characters must be escaped, for example
\",\\and\n. - A number is written in decimal, with an optional minus sign, fraction and exponent:
-12.5e3. - Spaces, tabs, line feeds and carriage returns may appear around the structural characters. Nothing else counts as whitespace.
When JSON is sent between systems, the RFC says it must be encoded as UTF-8.
The errors that break most files
Almost every “invalid JSON” message comes from one of these. Each example below is invalid, followed by the fix.
Trailing commas
{"roles": ["admin", "editor",]}
JavaScript allows a comma after the last item. JSON does not. Remove it: ["admin", "editor"].
Single quotes
{'name': 'Asha'}
Strings and keys must use double quotes: {"name": "Asha"}. This often happens when someone copies a Python dictionary or a JavaScript object and expects it to be JSON.
Keys without quotes
{name: "Asha"}
Unlike JavaScript object literals, JSON keys are always quoted strings: {"name": "Asha"}.
Comments
{
// timeout in seconds
"timeout": 30
}
The JSON grammar has no comment syntax. Some tools accept comments in their own config files, and the RFC allows a parser to accept extensions, but anything that produces JSON for others must stick to the strict grammar. If you need a note, add it as data: "timeoutNote": "seconds".
NaN, Infinity and other special numbers
{"ratio": NaN, "max": Infinity}
The RFC says numeric values such as Infinity and NaN are not permitted. Use null, or a string if the meaning matters.
Leading zeros and other number slips
007, +5, .5 and 5. are all invalid. Write 7, 5, 0.5 and 5. Leading zeros are not allowed except for a single zero before the decimal point.
Curly quotes from word processors and chat apps
JSON pasted from a document or a chat message may arrive with typographic quotes: “name” instead of "name". They look almost the same, but a parser rejects them. Retype the quotes or paste from a plain-text source.
Duplicate keys
{"id": 1, "id": 2}
This one is not a syntax error. The RFC says names within an object “SHOULD be unique” and warns that parsers behave differently when they are not: some keep the last value, some report an error. Many keep only the last, so id silently becomes 2. Treat duplicates as a bug.
Formatting, minifying and validating
These three jobs are often mixed up.
| Job | What it changes | When to use it |
|---|---|---|
| Format | Adds line breaks and indentation | Reading, reviewing, comparing |
| Minify | Removes all optional whitespace | Sending or storing compactly |
| Validate | Nothing; it checks the grammar | Finding why a file is rejected |
Formatting and minifying only touch the whitespace between tokens. The data is the same either way, and a program reading the file sees no difference. A good formatter refuses invalid input rather than guessing what you meant, so in practice formatting also tells you whether the JSON is valid.
A worked example
Here is a settings file that an application refuses to load:
{
'appName': "Ledger",
"retries": 03,
"features": ["export", "print",],
"timeout": NaN
}
Going through it line by line:
'appName'uses single quotes. Change to"appName".03has a leading zero. Change to3."print",]has a trailing comma. Remove the comma.NaNis not a JSON value. Use a number such as30, ornull.
A validator stops at the first problem, so you would see four separate errors as you fix them one at a time, each with its line and column. The corrected file:
{
"appName": "Ledger",
"retries": 3,
"features": ["export", "print"],
"timeout": 30
}
Minified, the same data is one line of 75 characters:
{"appName":"Ledger","retries":3,"features":["export","print"],"timeout":30}
The formatted version above is 93 characters with two-space indentation. On a file this small the saving does not matter; on a large API response or a file shipped to millions of users it does.
Big numbers
JSON itself puts no limit on the size of a number, but the program reading it may. JavaScript, for example, stores numbers as 64-bit floating point, so it cannot hold whole numbers above 9,007,199,254,740,991 exactly. A 20-digit ID such as 12345678901234567890 can come back as 12345678901234567000. The RFC notes this interoperability problem. If your IDs are that long, send them as strings.
Syntax is not the same as correct data
A validator that follows RFC 8259 tells you the text is well-formed JSON. It does not tell you whether "retries" should be a number, whether a required field is missing, or whether a date is in the right format. Those checks need a schema agreed between the systems, such as JSON Schema, or checks in the program itself.
Tools for each job
- To find the line and column of a syntax error, paste into the JSON validator.
- To read or tidy a file, use the JSON formatter, which can also sort keys so two files line up for comparison.
- To shrink it to one line, use the JSON minifier.
- To open the data in a spreadsheet, convert an array of objects with JSON to CSV.
All four run in your browser, so configuration files with keys or customer data stay on your device.
Tools for this
Sources
- RFC 8259, The JavaScript Object Notation (JSON) Data Interchange Format (IETF, December 2017) (checked 23 November 2026)
Rules and rates change. Check the current notice or official page before you rely on a figure here. This guide is general information, not legal, tax or financial advice. Spotted a mistake? See our editorial and corrections policy.