How to Format and Validate JSON, and Fix the Most Common Errors
What valid JSON looks like, why parsers reject it, and how to fix trailing commas, single quotes, unquoted keys and other frequent mistakes.
What JSON allows
JSON is a small text format with strict rules. A document is a single value: an object in curly braces, an array in square brackets, a string, a number, true, false or null. Objects hold key and value pairs, and every key must be a string wrapped in double quotes.
That strictness is why JSON is easy for programs to read, and also why a single stray character makes a parser reject the whole document. JSON is not JavaScript. Several things that are fine in a JavaScript object literal are not valid JSON.
The mistakes that cause most parse errors
Most broken JSON comes from a short list of habits carried over from code or from hand editing.
- Trailing commas. A comma after the last item in an object or array is not allowed, so {"a": 1,} fails.
- Single quotes. Strings and keys must use double quotes, so {'a': 1} fails.
- Unquoted keys. {a: 1} works in JavaScript but not in JSON.
- Comments. JSON has no comment syntax, so // note and /* note */ both break parsing.
- Unescaped characters inside strings. A double quote, a backslash or a raw line break inside a string must be escaped as \", \\ or \n.
- Values that JSON does not have: undefined, NaN, Infinity and functions. Use null or a string instead.
- A byte order mark or stray text before the first brace, often added by a text editor or copied from a log line.
Reading an error message
Most parsers report a position or a line and column. The reported spot is where the parser gave up, which is usually at or just after the real mistake. If a message says unexpected token at the start of a key, look at the character before it for a missing comma or an extra one. If it says unexpected end of input, a closing brace or bracket is missing somewhere earlier.
Fix problems from the top of the document downward. One missing quote can make everything after it look wrong, so later errors often disappear once the first one is fixed.
Formatting and minifying
Formatting, also called pretty printing, adds line breaks and indentation so the structure is easy to scan. It does not change the data. Two or four spaces are the usual choices, and teams generally pick one and keep it so diffs stay small.
Minifying does the opposite and removes every space and line break that is not inside a string. The result is smaller, which helps when JSON is sent over a network or stored in a size limited field. Neither operation changes keys, values or their order.
A quick checking routine
When a payload is rejected, paste it into a formatter and read the first error. Fix it, run it again and repeat until the document formats cleanly. Then confirm the shape matches what the receiving system expects, because valid JSON can still have the wrong field names or types. A formatter that runs in your browser keeps the data on your machine, which matters when the payload contains tokens or customer records.
Worked example: fixing a broken payload
Here is a payload copied from a code editor and pasted into an API request body. It is not valid JSON:
{'name': "Asha", "age": 31, "tags": ["admin", "beta",], // main account "active": True}
A parser stops at the first problem, so the errors appear one at a time. Walking through them in order shows how the routine works.
- Step 1: the parser fails on the very first key. The key is wrapped in single quotes, so replace 'name' with "name".
- Step 2: the next failure is the comma after "beta" inside the array. Delete it, because the last item in an array cannot be followed by a comma.
- Step 3: the text starting with // is a comment. JSON has no comments, so remove it. If the note matters, store it as a field such as "note": "main account".
- Step 4: True with a capital T is a Python or other language literal. JSON only accepts lowercase true and false.
The fixed document is {"name":"Asha","age":31,"tags":["admin","beta"],"active":true}, shown here minified. Formatted with two spaces, each key sits on its own line and the tags array is easy to read. Now check the shape: if the receiving API expects userName instead of name, the JSON is valid but the request will still be rejected.
JSON values compared with JavaScript
Many parse errors come from assuming JSON behaves like the language you write it in. This table shows the differences that matter most.
| Item | JavaScript object literal | Valid JSON |
|---|---|---|
| Key quoting | Bare or quoted, single or double | Double quotes only |
| String quotes | Single, double or backtick | Double quotes only |
| Trailing comma | Allowed | Not allowed |
| Comments | Allowed | Not allowed |
| undefined | Allowed | Not allowed, use null |
| NaN and Infinity | Allowed | Not allowed |
| Leading zero such as 007 | Treated as octal in some cases | Not allowed |
| Hexadecimal such as 0xFF | Allowed | Not allowed, write 255 |
Problems that look valid but are not
Some errors are hard to see because the text looks fine on screen.
- Curly quotes. Text copied from a word processor or chat tool can turn straight quotes into typographic ones. The parser sees an unknown character, not a quote.
- Non-breaking spaces. These look like normal spaces but can trigger an unexpected token error. Retype the space or run the text through a formatter that reports the position.
- Numbers that lose precision. JSON itself allows large numbers, but many parsers read them as double precision floats, so an ID such as 12345678901234567890 may be changed silently. Send long IDs as strings.
- Duplicate keys. The format does not forbid them, but parsers disagree on which value wins, usually the last one. Remove duplicates rather than rely on that.
- Two documents joined together. {"a":1}{"b":2} is two values, not one, and a standard parser fails after the first closing brace. Log files with one object per line need to be read line by line.
Frequently asked questions
- Why does my JSON fail when it works in my JavaScript code? Your code is probably using an object literal, which is more relaxed than JSON. Strict JSON needs double quoted keys, no comments and no trailing commas.
- Can JSON have comments? No. The format has no comment syntax. Either remove them, add a field that holds the note, or use a format built for configuration, such as one that explicitly supports comments.
- Does formatting change my data? No. Formatting only adds or removes whitespace outside strings. Keys, values and their order stay the same.
- What is the difference between null and an empty string? null means no value, while "" is a string with zero characters. Receiving systems often treat them differently, so use the one the documentation asks for.
- Is it safe to paste private data into an online formatter? Only if the tool runs in your browser and does not upload the text. Check this before pasting tokens, keys or customer records.