How to format, validate and fix JSON
Read and debug JSON quickly — pretty-print it, find the exact line of a syntax error, understand the most common mistakes and minify it for production.
· 5 min read
JSON is the lingua franca of APIs and configuration files, and it is deliberately strict: there are no comments, no trailing commas and no single quotes. One stray character makes the whole document invalid, and the error message points at where the parser gave up rather than where you made the mistake.
The quickest route here: paste the document into the JSON formatter for indented output, or into the JSON validator if it is already rejected somewhere and you need the exact position of the problem. Both parse in your browser, so an API response with production data is not sent anywhere.
The rules people trip on
| Mistake | Invalid | Valid |
|---|---|---|
| Trailing comma | {"a": 1,} |
{"a": 1} |
| Single quotes | {'a': 1} |
{"a": 1} |
| Unquoted key | {a: 1} |
{"a": 1} |
| Comment | {"a": 1 // note} |
not allowed at all |
| Special numbers | NaN, Infinity |
null or a string |
undefined |
{"a": undefined} |
omit the key, or use null |
| Leading zero | {"a": 01} |
{"a": 1} |
| Hexadecimal | {"a": 0x1F} |
{"a": 31} |
| Curly quotes | {“a”: 1} |
{"a": 1} |
| Text after the value | {"a": 1}extra |
one value per document |
Two more that are easy to miss: JSON text must be UTF-8, and a string cannot contain a raw newline — it has to be written as \n.
Curly quotes deserve their own warning. They appear whenever JSON has been through a word processor, a chat window or a documentation page with typographic quotes enabled, and they look almost identical to straight quotes in most fonts.
Why JavaScript objects are not JSON
JavaScript object literals accept every invalid form in that table, which is why copying an object out of code into a .json file so often fails. The reverse is true too: JSON.stringify silently drops keys whose value is undefined or a function, and turns undefined inside an array into null.
Other languages are lenient in their own ways. Python's json module accepts NaN and Infinity by default, as a documented extension — so a file that loads perfectly in a Python script can be rejected by a browser, a Go service or this validator. If a document has to travel between languages, keep to the strict grammar.
How to read a parser error
Error messages give you a position, which is a character offset from the start of the document, plus a line and column. The JSON validator shows the line, the column and a caret under the offending character, and reports key count and nesting depth for valid documents.
The wording tells you what the parser expected next:
| Message | What it usually means |
|---|---|
Expected double-quoted property name in JSON at position 8 |
A trailing comma, or an unquoted or single-quoted key |
Expected ',' or '}' after property value in JSON at position 8 |
A missing comma between two pairs, or a comment |
Expected ',' or ']' after array element |
The document was truncated, or a comma is missing |
Unexpected token 'N' |
A bare NaN, undefined or other non-JSON literal |
Unexpected non-whitespace character after JSON |
Two documents concatenated, or stray text at the end |
The key habit: the reported position is where parsing became impossible, not where the fault is. A missing comma on line 40 is often reported on line 41, and a missing closing brace is usually reported at the very end of the file. Look just before the position, not at it.
For truncated payloads, check the total length first. A response cut off by a proxy or a log line clipped at a fixed width will always fail near the end, and no amount of editing around the reported position will help.
Pretty-print, sort, compare
Formatting re-serialises the parsed data with your chosen indentation, so the output is guaranteed valid and numbers, strings and escapes are preserved exactly. Sorting object keys alphabetically is recursive, which turns "why do these two API responses differ?" into a readable comparison: sort both, then paste them into Text compare so that only real differences show up instead of key-order noise.
Minify for production, but know what it buys
Minifying removes every space, tab and line break outside strings. On real files the raw saving is substantial: an indented 769,377-byte report becomes 561,925 bytes, 27% smaller, and a small 849-byte config drops to 595 bytes, nearly 30%.
Most of that disappears once transport compression is applied, because the whitespace being removed is exactly what gzip and Brotli compress best. That report compresses with gzip to 92,899 bytes indented and 87,880 bytes minified — a real saving of only 5.4%. Minify JSON because it is being embedded in an environment variable, a URL or an HTML attribute, and let compression handle the network. The JSON minifier shows the byte count before and after.
What JSON cannot represent
There are no date, binary or comment types. The conventions are: dates as ISO 8601 strings ("2026-09-24T10:00:00Z"), binary data as Base64 — see Base64 explained — and "comments" as an ordinary key such as "_comment" that consumers ignore.
Numbers are the sharpest edge. JSON has one number type and no stated precision, and JavaScript reads every number as a 64-bit float: 9007199254740993 parsed in a browser comes back as 9007199254740992. Python parses the same document exactly. If your identifiers exceed 2^53 − 1, send them as strings.
Duplicate keys are not an error in the grammar, and most parsers keep the last one, so {"a":1,"a":2} becomes {"a":2} — silently.
Moving data to and from spreadsheets
Need to hand data to someone who does not read JSON? JSON to CSV flattens nested objects into columns such as address.city, and CSV to JSON goes the other way, converting numbers and booleans to proper JSON types. If the CSV is then opened in Excel, the guide How to open a CSV in Excel correctly covers the separator and encoding problems that follow.
Questions people ask
Does the validator check a schema? No. It checks syntax against RFC 8259. Schema validation — required fields, types, ranges — is a separate step with a tool such as a JSON Schema validator.
Is it safe to paste an API response? These tools parse in the browser and nothing is uploaded. As a habit, still remove tokens and personal data before sharing JSON with anyone.
Can I keep comments in a config file? Not in .json. Formats such as JSON5 or JSONC allow them, but they need a parser that supports them.
Next steps
- Make a payload readable: JSON formatter
- Locate a syntax error: JSON validator
- Compress for embedding: JSON minifier
Tools used in this guide
- JSON FormatterBeautify JSON with indentation and optional key sorting.
- JSON ValidatorCheck JSON syntax and pinpoint errors by line and column.
- JSON MinifierRemove whitespace from JSON and see the size saved.
- CSV to JSON ConverterConvert CSV rows into a JSON array of objects.
- JSON to CSV ConverterConvert JSON arrays into CSV for spreadsheets.
- Text Compare (Diff Checker)Compare two texts and highlight the differences.