HomeGuides › Format JSON

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

  1. Paste your JSON.
  2. Format to indent it, or minify to strip whitespace.
  3. Read the error if it is invalid — with the position it failed at.

The errors you will actually hit

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 →