How to format and validate JSON
Indent it to read it, minify it to ship it, and find out precisely where a broken document goes wrong.
JSON from an API arrives as one long line. Formatting it makes the structure visible; validating it tells you why a parser is rejecting it.
Step-by-step
- Paste your JSON.
- Format to indent it, or minify to strip whitespace.
- Read the error if it is invalid — with the position it failed at.
The errors you will actually hit
- Trailing comma. Allowed in JavaScript, forbidden in JSON. The single most common cause.
- Single quotes. JSON requires double quotes, for keys as well as values.
- Unquoted keys. Valid in a JavaScript object literal, invalid in JSON.
- Comments. JSON has none. A
//line will fail. - An unescaped newline inside a string. Must be written
\n.
The common thread is that JavaScript object syntax and JSON are similar and not the same. Most invalid JSON is valid JavaScript.
When to minify
Whitespace can be a large fraction of a formatted document, so minifying is worth it for anything transmitted repeatedly. It makes no difference to a config file a human maintains — keep that readable.
Validity is not correctness
Valid JSON is well-formed. It says nothing about whether the fields are the ones your program expects, or whether a number is in range. A response can be perfectly valid JSON and completely wrong for your purposes; validating rules out one class of problem only.
Frequently asked questions
Why is my JSON invalid when it looks fine?
Usually a trailing comma after the last item, or single quotes instead of double. Both are legal JavaScript and illegal JSON, which is why they are so easy to miss.
Can JSON contain comments?
No. The format has no comment syntax. Some parsers accept them as an extension, but a file containing them is not JSON and will fail elsewhere.
Is my data sent anywhere?
No. Parsing and formatting happen in your browser.
Open the developer tools →