Back to guides

How to Format (Pretty-Print) JSON

How to turn minified or messy JSON into readable, indented text, when to minify it back down, and how to do both from code or the command line.

Before and after

Formatting (also called pretty-printing or beautifying) only adds indentation and line breaks — it never changes the data itself.

Minified, hard to scan:

{"service":"api","port":8080,"env":["staging","production"]}

Formatted with 2-space indentation:

{
  "service": "api",
  "port": 8080,
  "env": ["staging", "production"]
}

Formatting online

Paste JSON into the JSON Formatter and choose Beautify with 2 spaces, 4 spaces, or a tab. Large documents are formatted in a background worker instead of on the main thread, so the page stays responsive.

Formatting from code

Every mainstream language's standard library can do this without a dependency:

// JavaScript
JSON.stringify(data, null, 2)

# Python
import json
json.dumps(data, indent=2)

From a terminal, without writing any code:

python -m json.tool file.json
jq . file.json

When to minify instead

Formatting is for humans reading the file. Once it's headed over the network — an API response, a config bundled into a build — the extra whitespace is pure overhead. Run it through the JSON Minifier to strip it back down before shipping it.

If the formatter is rejecting your input outright, the JSON is probably invalid before it's even a formatting question — see common JSON errors and how to fix them.

Frequently Asked Questions

Does formatting change the data?

No. Formatting only adds or removes whitespace (indentation, line breaks, spacing) — the keys, values, and structure are unchanged. It's purely cosmetic.

What's a sensible default indent width?

2 spaces is the most common default and what most formatters (including this one) start with. 4 spaces reads a little more distinctly at the cost of more horizontal space; tabs let each viewer's editor pick its own width.

Can I format very large JSON files without the tab freezing?

Yes — large documents are formatted in a background worker instead of on the main thread, so the tab stays responsive while it runs.

How do I format JSON from a terminal instead?

Most systems already have a formatter available: python -m json.tool file.json, or jq . file.json if you have jq installed. Both pretty-print with 2- or 4-space indentation depending on flags.