How to Format JSON: Syntax Rules, Common Errors and Fixes
Learn the JSON syntax rules, fix the errors that break parsers (trailing commas, single quotes, comments), and know when to pretty-print or minify JSON.
By Awais, Founder and editor-in-chiefPublished
To format JSON, paste it into a JSON formatter, which checks that it is valid and re-indents it with one key per line, usually with 2 or 4 spaces per level. If the formatter reports an error, the cause is almost always one of a handful of mistakes: a trailing comma, single quotes, a missing comma, unquoted keys or a comment.
This guide covers the rules that make JSON valid, the errors you will meet most often and how to fix each one, and when to pretty-print or minify.
The JSON syntax rules
JSON is defined by RFC 8259, an Internet standard from December 2017, and by the matching ECMA-404 standard. The whole format fits on one page:
| Rule | Valid | Invalid |
|---|---|---|
| Strings use double quotes | "name" |
'name' |
| Object keys are strings | {"id": 1} |
{id: 1} |
| Items are separated by commas, with none after the last item | [1, 2, 3] |
[1, 2, 3,] |
| Only six value types | string, number, object, array, true/false, null |
undefined, functions, dates |
true, false and null are lowercase |
true |
True, NULL |
Numbers have no leading zeros, no +, no NaN or Infinity |
7, -0.5, 1e6 |
07, +7, NaN |
| Some characters in strings must be escaped | "say \"hi\"" |
"say "hi"" |
| No comments | (none) | // note, /* note */ |
A few details from the standard are worth knowing:
- Whitespace is free. Spaces, tabs, line feeds and carriage returns are allowed around the structural characters (brackets, braces, colons and commas). That is why the same data can be written on one line or on a hundred.
- Escapes. Inside a string you must escape the double quote, the backslash and control characters such as a raw line break or tab. Write a line break as
\nand a tab as\t. - Duplicate keys. The standard says names within an object “SHOULD be unique”. Duplicates are not forbidden, but parsers treat them differently: most keep the last value, some keep the first. Avoid them.
- Encoding. JSON exchanged between systems must be UTF-8, and a sender must not add a byte order mark (BOM) at the start.
Here is a small, valid JSON document, pretty-printed with 2 spaces:
{
"id": 1042,
"name": "Ayesha Khan",
"email": "ayesha@example.com",
"active": true,
"roles": ["editor", "reviewer"],
"address": {
"city": "Lahore",
"postcode": "54000"
},
"lastLogin": null
}
Note that "54000" is a string, not a number. Postcodes, phone numbers and ID numbers with leading zeros should stay strings, or the zeros will be lost.
How to format JSON online
- Open the JSON Formatter.
- Paste your JSON into the box.
- If there is an error, the tool shows the line and column. Fix the problem and check again.
- Choose Format with 2 spaces, 4 spaces or tabs, or Minify to strip the whitespace.
- Copy the result or download it as a file.
The formatter runs in your browser, so the data you paste is not sent to a server. That matters because JSON often contains API keys, tokens, customer records and internal settings that should never be pasted into a random website.
Common JSON errors and how to fix them
1. Trailing comma
{ "a": 1, "b": 2, }
JavaScript allows a comma after the last item; JSON does not. Delete it. Watch for this after you remove the last line of a list.
2. Single quotes
{ 'name': 'Sam' }
Replace every single quote around keys and strings with double quotes. This often happens when a Python dictionary is printed with print() instead of json.dumps().
3. Unquoted keys
{ name: "Sam" }
This is valid JavaScript but not JSON. Put every key in double quotes: "name".
4. Missing comma
{ "a": 1 "b": 2 }
The error usually points at the start of the next item (“unexpected string at line 1”). Add the comma between the items.
5. Comments
JSON has no comments. Remove // and /* */ lines, or move notes into a field such as "_comment": "...". Some tools accept “JSON with comments” in their config files, but a standard JSON parser will reject it.
6. Unescaped characters in strings
A raw line break or an unescaped double quote inside a string ends the string early. Write \n for a line break, \" for a quote and \\ for a backslash. Windows paths are a classic trap: "C:\new" contains \n, which is read as a line break; write "C:\\new".
7. Wrong values
undefined, NaN, Infinity, True, None and dates like 2026-10-21 written without quotes are all invalid. Use null for missing values, put dates in quotes (ISO 8601 such as "2026-10-21T09:30:00Z" is the usual choice), and use lowercase true and false.
8. Invisible characters
If JSON looks perfect but still fails, it may contain a byte order mark at the start, a non-breaking space, or curly quotes (“ ”) copied from a document or chat app. Retype the quotes or paste the text into a plain-text editor first.
Pretty print vs minify
Pretty-printing adds line breaks and indentation so people can read the data. Minifying removes all optional whitespace so machines can move it faster. The data is identical either way.
| Pretty-printed | Minified | |
|---|---|---|
| Best for | Reading, debugging, config files, code review | API responses, storage, embedding in pages |
Sample record above, as output by JSON.stringify |
226 bytes (2-space indent), 260 bytes (4-space) | 169 bytes |
| 1,000 similar records (our test) | 256,892 bytes | 168,891 bytes |
| Same 1,000 records after gzip | 3,303 bytes | 3,259 bytes |
In our test, minifying 1,000 records saved about a third of the raw size. But once the data was gzip-compressed, as web servers normally do, the difference shrank to about 1%. Our sample records were very repetitive, so real data compresses less, but the pattern holds: on a server with compression, minifying JSON helps a little, and pretty-printing costs little. For config files that people edit, always keep JSON pretty-printed, because it makes changes easy to review with a tool such as Text Compare.
Formatting JSON from the command line
If you work in a terminal, these commands pretty-print a file:
python -m json.tool data.json
jq . data.json
In JavaScript, JSON.stringify(value, null, 2) pretty-prints with 2 spaces, and JSON.stringify(value) minifies. Python’s json.dumps(value, indent=2) does the same, and json.dumps(value, separators=(",", ":")) gives the most compact output.
Tips for working with JSON
- Validate before you send. A single stray comma can make an API return an error or a config file fail to load.
- Keep keys consistent. Pick one style, such as
camelCaseorsnake_case, and stick to it. - Don’t hand-encode binary data. Files and images inside JSON are usually Base64 strings. Use a Base64 encoder and decoder to check them.
- Keep secrets out. Never commit API keys or passwords in JSON config files. If a key leaks, replace it. Our guide on how to create a strong password covers how to manage secrets safely.
FAQ
Is JSON the same as a JavaScript object?
No. JSON looks like JavaScript object syntax but is stricter: keys need double quotes, strings can’t use single quotes, and trailing commas, comments, functions and undefined are not allowed.
Can JSON have comments?
Not in standard JSON. Some formats based on JSON, such as JSONC and JSON5, allow comments, but a normal JSON parser will reject them.
Should I indent JSON with 2 or 4 spaces?
Either is valid. Two spaces is the most common choice for web projects and keeps deeply nested data readable on narrow screens.
Why does my JSON parse in JavaScript but fail in a validator?
You may be looking at a JavaScript object literal, which allows unquoted keys, single quotes and trailing commas. Convert it with JSON.stringify() to get real JSON.