How to pretty-print JSON: 7 ways that actually work
Pretty-print JSON in the browser, VS Code, jq, Python, JavaScript, and PowerShell — with the exact commands and the gotchas for each.
title: "How to pretty-print JSON: 7 ways that actually work" slug: "how-to-pretty-print-json" date: "2026-07-07" description: "Pretty-print JSON in the browser, VS Code, jq, Python, JavaScript, and PowerShell — with the exact commands and the gotchas for each." keywords:
- pretty print json
- json pretty print
- format json
- jq pretty print
- python pretty print json
Minified JSON is written for machines: one line, no spaces, no mercy. The moment a human needs to read it — debugging an API response, reviewing a config file, diffing two payloads — you need to pretty-print it. Here are the seven ways developers actually do that, from zero-install to fully scripted, with the sharp edges called out.
1. In your browser, without uploading anything
The fastest path when you have JSON on your clipboard: paste it into the JSON Formatter. It pretty-prints with 2- or 4-space indentation, can sort keys alphabetically, and — unlike most online formatters — runs entirely client-side, so an API response full of tokens or customer emails never leaves your machine.
Because it parses with the browser's native JSON.parse(), it also doubles as a syntax check: if your JSON is broken, you get the error position instead of silently mangled output.
2. JavaScript / Node.js: JSON.stringify with the third argument
The formatting logic every online formatter is built on ships in the language:
const pretty = JSON.stringify(obj, null, 2); // 2-space indent
const tabbed = JSON.stringify(obj, null, "\t"); // tab indent
The second argument (null here) is a replacer function — leave it null for a plain re-serialization. If what you have is a JSON string rather than an object, round-trip it:
const pretty = JSON.stringify(JSON.parse(raw), null, 2);
Gotcha: JSON.stringify silently drops undefined values, functions, and symbols. That's correct JSON behavior, but it surprises people pretty-printing objects that were never valid JSON to begin with.
3. Python: json.tool from the command line
Python ships a formatter you can pipe into with no code at all:
python -m json.tool response.json
curl -s https://api.example.com/data | python -m json.tool
In code, it's the indent parameter:
import json
pretty = json.dumps(data, indent=2, ensure_ascii=False)
ensure_ascii=False keeps non-ASCII text readable instead of escaping it to \uXXXX sequences. Add sort_keys=True for stable, diffable output.
4. jq: the shell workhorse
jq pretty-prints by default — the identity filter is all you need:
jq . response.json
curl -s https://api.example.com/data | jq .
Useful variations: jq -S . sorts keys, jq --indent 4 . changes the indent width, and jq -c . goes the other direction and minifies. If you don't have jq installed and just need JSON smaller for transport, the JSON Minifier does the same job in the browser.
The classic jq gotcha: it will fail on things that look like JSON but aren't — NDJSON logs, JSON with comments, trailing commas. For input that's almost JSON, run it through the JSON Repair tool first.
5. VS Code: Format Document
Open the file (or paste into a new editor with the language set to JSON) and press Shift+Alt+F (Shift+Option+F on macOS). VS Code respects your configured tab size, and its JSON language service also underlines syntax errors — a two-for-one.
For JSON embedded inside a string in another file, select just the JSON and use "Format Selection" from the command palette.
6. Browser DevTools
If the JSON is an HTTP response, you don't need to copy it anywhere: open the Network tab, click the request, and the Preview pane shows a collapsible pretty-printed tree. The Response tab shows the raw body, and the {} pretty-print button in Sources works on any fetched document.
One caution we've written about before in the JSON parse error debugging guide: the Preview pane interprets the response. If you're debugging a parse failure, look at the raw body, not the preview.
7. PowerShell (Windows)
PowerShell can round-trip JSON natively:
Get-Content response.json -Raw | ConvertFrom-Json | ConvertTo-Json -Depth 100
The -Depth flag matters: ConvertTo-Json defaults to a depth of 2 and silently truncates anything deeper, which has burned nearly everyone who has used it. Set it explicitly.
When you should not pretty-print
Pretty-printing is for humans. Whitespace can double or triple payload size, so keep JSON minified for network transport, storage, and anywhere bandwidth is billed — gzip closes most of the gap, but not all of it, and unzipped size still affects parse time. The rule of thumb: minified at rest and on the wire, pretty-printed the moment a person needs to look at it.
Quick reference
| Where you are | Command |
|---|---|
| Browser, ad hoc | JSON Formatter |
| JavaScript | JSON.stringify(obj, null, 2) |
| Python CLI | python -m json.tool file.json |
| Python code | json.dumps(data, indent=2) |
| Shell | jq . file.json |
| VS Code | Shift+Alt+F |
| PowerShell | ConvertTo-Json -Depth 100 |
However you format it, the output is the same JSON — only the whitespace changes. If the content is what's wrong, start with the JSON Validator to find out exactly where.