LocalFirstTools.com — data tools that never leave your device
No upload — everything runs on your device

How to pretty-print JSON

Last updated

To pretty-print JSON, run it through any JSON parser and re-serialize it with indentation. The fastest way with data in your clipboard: paste it into the JSON Formatter and press Format — you get 2-space-indented output, validation with exact error positions, and nothing leaves your browser. The same job has one-liners in the terminal and in code, below.

What pretty-printing does

Minified JSON is a single line with no spaces — correct for machines, unreadable for people. Pretty-printing inserts newlines and indentation between tokens so the structure becomes visible. This one-line document:

{"user":{"id":42,"name":"Ada","roles":["admin","dev"]}}

becomes:

{
  "user": {
    "id": 42,
    "name": "Ada",
    "roles": [
      "admin",
      "dev"
    ]
  }
}

Since whitespace between tokens carries no meaning in JSON (RFC 8259, section 2), the two documents are identical to every parser. The cost is measurable but small at this scale: the minified version is exactly 55 bytes, the 2-space pretty version 100 bytes — an 82% size increase that gzip mostly claws back on the wire. Pretty-printing is lossless and reversible — the reverse operation is minifying.

In the browser (no install)

  1. Open the JSON Formatter & Validator.
  2. Paste the JSON, drop a .json file onto the input pane, or use the Upload button.
  3. Press Format (or Cmd/Ctrl + Enter). Invalid input shows the exact line and column of the first error instead of output.
  4. Copy the result or download it as a file.

Enable the Sort keys toggle if you also want every object's keys alphabetized — useful before diffing two documents.

In the terminal

jq is the standard: jq . data.json pretty-prints to stdout, and curl -s https://api.example.com/users | jq . formats an API response inline. Without jq, Python is preinstalled almost everywhere: python3 -m json.tool data.json, or Node: node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(0,'utf8')),null,2))".

In code

Every mainstream language does this natively. JavaScript: JSON.stringify(value, null, 2) — the third argument is the indent. Python: json.dumps(value, indent=2). Go: json.MarshalIndent(v, "", " "). The principle is identical everywhere: parse, then serialize with an indent parameter.

In the browser console

Debugging a live page? You already have a pretty-printer open. With an object in hand, copy(JSON.stringify(obj, null, 2)) in the dev-tools console puts formatted JSON straight on your clipboard. For a response you are about to fetch, await (await fetch(url)).json() renders as an expandable tree in the console — often better than text, because you can collapse the parts you don't care about. And the Network tab's Preview pane pretty-prints every JSON response automatically; the Response tab next to it shows the raw minified bytes.

JSON Lines is a different animal

Log files often contain JSON Lines (also called NDJSON): one complete JSON document per line, newline-separated. A .jsonl file as a whole is not valid JSON — paste one into a formatter and you get an error like Unexpected token { at line 2, because a second document begins where the parser expected the end of input. Pretty-print such files one line at a time, or with jq ., which processes a stream of documents natively. The mirror-image mistake — pretty-printing a document that a log shipper then expects on one line — is why log pipelines want minified JSON.

When you should not pretty-print

Keep JSON minified where machines consume it and bytes count: API responses, embedded data attributes, cache entries. A pretty-printed 10 KB payload is typically 25–35% larger than its minified form. The right habit is: pretty in the repo and the editor, minified on the wire — and a formatter in the middle when you need to cross between the two.

Do it now — locally

Pretty-print, minify and validate JSON in your browser. Nothing is uploaded.

Open the JSON Formatter

Frequently asked questions

Does pretty-printing change the JSON data?

No. JSON ignores whitespace between tokens (RFC 8259), so an indented document parses to exactly the same value as its minified form. Only readability changes.

What indentation should I use — 2 spaces, 4 spaces, tabs?

Two spaces is the dominant convention in web tooling and what jq, Prettier and most formatters emit by default. It matters only for humans; pick one and keep it consistent within a project.

How do I pretty-print a huge JSON file?

Browser tools handle files into the tens of megabytes. Beyond that, stream with a command-line tool: jq . big.json > pretty.json processes without loading your editor.

Can I pretty-print JSON with comments in it?

Not as-is — comments are not valid JSON and any compliant parser rejects them. Strip the comments first, or treat the file as JSONC/JSON5 in tooling that explicitly supports those supersets.

Why won't my log file pretty-print as a whole?

It is probably JSON Lines (NDJSON): one JSON document per line. The file as a whole is invalid JSON by design. Format individual lines, or use jq, which handles document streams.

Does pretty-printing preserve number precision?

Formatting itself adds no rounding, but parsing can: JavaScript stores all numbers as 64-bit floats, so an integer longer than 15–16 digits (like a snowflake ID 1234567890123456789) silently loses precision the moment it is parsed. Keep such IDs as strings.