How to format JSON (and fix the errors that break it)

Formatting JSON takes one click. Knowing why a document refuses to parse, and what a formatter can silently change, is the part worth learning.

By NoFrillzApps · 4 min read

What formatting actually does

A JSON formatter parses your text into data and prints it back out with consistent indentation, one key per line. Minifying is the same round trip with all optional whitespace removed. Neither changes the data, with one important exception covered below (very large numbers).

Because formatting starts with parsing, a formatter is also a validator: if the text isn't valid JSON, there is nothing to print and you get an error instead.

{"b":2,"a":[1,2]}

formatted with two-space indentation becomes:

{
  "b": 2,
  "a": [
    1,
    2
  ]
}

Key order is preserved. If you need stable output for code reviews or diffs, sort keys explicitly; the JSON Formatter has an option for it.

The rules that trip people up

JSON looks like a JavaScript object literal, but it is stricter. These are the errors that cause almost every parse failure:

InvalidValidRule
{'name': 'Ana'}{"name": "Ana"}Strings and keys use double quotes only.
{name: "Ana"}{"name": "Ana"}Keys must be quoted strings.
[1, 2, 3,][1, 2, 3]No trailing commas.
{"a": 1 // note}{"a": 1}No comments of any kind.
{"n": NaN}{"n": null}No NaN, Infinity or undefined.
{"n": 007}{"n": 7}No leading zeros on numbers.
"line one line two""line one\nline two"Control characters such as newlines must be escaped.

Configuration formats that look like JSON but allow comments and trailing commas (JSON5, JSONC as used by VS Code settings) are not JSON. A strict parser will reject them; that's expected, not a bug.

Reading a parse error

Parsers report the position where they gave up, which is often just after the real mistake. A missing comma between two properties is reported at the start of the second property; a missing closing brace is reported at the very end of the input.

  1. Go to the reported line and column.
  2. Look at the previous token: a missing comma, an unclosed string or a stray quote is usually there.
  3. If the error points at the last character, count your brackets: something opened was never closed.

Pasting into a validator that shows the line and column, rather than reading a stack trace, saves most of the time.

The silent problem: big numbers

JSON itself allows integers of any size, but JavaScript (and therefore every browser-based formatter, including this one) stores numbers as 64-bit floating point. Integers above 253 − 1 = 9,007,199,254,740,991 cannot all be represented exactly:

JSON.parse("9007199254740993")   // → 9007199254740992

The document is valid, so no error appears; the number simply changes. This matters for database IDs, tweet/snowflake IDs and some financial values. If you work with such numbers, have the API send them as strings ("id": "9007199254740993"), or use a tool that parses big integers natively.

Formatting JSON on the command line

For files on disk or in scripts:

# Python (installed almost everywhere)
python3 -m json.tool data.json

# jq: pretty-print, or minify with -c
jq . data.json
jq -c . data.json

# Node.js
node -e 'console.log(JSON.stringify(JSON.parse(require("fs").readFileSync(0, "utf8")), null, 2))' < data.json

All three also act as validators: they exit with an error on invalid input, which makes them useful in CI checks.

Privacy: where does the JSON go?

JSON often contains things you shouldn't paste into a random website: API responses with tokens, customer records, configuration with secrets. Many online formatters send your input to a server. The NoFrillzApps JSON Formatter parses and prints in your browser; the text never leaves your device. For anything sensitive, that (or a local command-line tool) is the safe default.

Common questions

Is JSON the same as a JavaScript object?

No. JSON is a text format with stricter rules: double-quoted keys and strings, no comments, no trailing commas, and no undefined, NaN or functions. A JavaScript object literal allows all of those.

Can JSON have comments?

No. The JSON standard (RFC 8259) has no comment syntax. Formats such as JSON5 and JSONC allow comments, but strict JSON parsers reject them.

Does formatting change my data?

Formatting only changes whitespace. The one silent change comes from the parser: integers larger than 9,007,199,254,740,991 lose precision in JavaScript-based tools, so send such IDs as strings.

What is the difference between formatting and minifying?

Formatting adds indentation and line breaks for people to read; minifying removes all optional whitespace to make the payload smaller. Both represent exactly the same data.

Try it: JSON Formatter & Validator — runs in your browser, nothing uploaded.

Related guides