How to Format and Validate JSON Online

Unreadable single-line JSON and cryptic parse errors are two of the most common developer time sinks. Both are quick to fix once you know what to look for.

4 min read

Quick answer

Pretty-printing adds indentation and line breaks so a human can read the structure. Minifying strips every unnecessary character so a machine can transfer it faster. The data is identical either way — only whitespace changes.

Pretty-printing versus minifying

Pretty-printing adds indentation and line breaks so a human can read the structure. Minifying strips every unnecessary character so a machine can transfer it faster. The data is identical either way — only whitespace changes.

Use pretty-printed JSON in config files, fixtures and documentation. Use minified JSON in network responses and anywhere payload size matters.

The five mistakes that cause almost every parse error

JSON is stricter than JavaScript object literals, which is where most errors come from.

  • Trailing commas after the last item in an object or array
  • Single quotes instead of double quotes around keys and strings
  • Unquoted keys, which are legal in JavaScript but not in JSON
  • Comments — JSON has no comment syntax at all
  • Unescaped line breaks or quotes inside string values

Reading the error message

Parser errors usually report a position or line number. That location is where the parser gave up, which is often just after the real mistake — a missing comma on the previous line commonly reports an error on the next one. Check the line above the reported position first.

Keeping payloads private

API responses frequently contain customer names, email addresses and tokens. A formatter that posts your JSON to a server to format it is a data-handling risk. A browser-based formatter parses and re-serialises with the native JSON methods on your own device, so nothing is transmitted.

Advertisement

Tools mentioned in this guide

FAQ

Does formatting change my data?

No. Pretty-printing and minifying only alter whitespace. Keys, values, types and order are preserved.

Can JSON contain comments?

No. The JSON specification has no comment syntax. Formats like JSON5 and JSONC add comments, but standard parsers will reject them.

More guides