How to Pretty Print JSON (Browser, Command Line, Python, JavaScript)
APIs and log files usually send JSON minified: no spaces and no line breaks, all on one line. That saves bytes, but it is almost impossible for a person to read. "Pretty printing" (also called formatting or beautifying) adds indentation and line breaks back. Here is how to do it everywhere you might need it.
Before and after
{"user":{"id":7,"name":"Ann","roles":["admin","dev"]},"active":true}{
"user": {
"id": 7,
"name": "Ann",
"roles": ["admin", "dev"]
},
"active": true
}Formatting only changes whitespace. The data stays the same.
Online (no install)
- Paste the JSON into the JSON Formatter, or open a file.
- Choose 2 spaces, 4 spaces or tabs.
- Click Format. Click Minify to go back to one line.
If the JSON is invalid, the formatter shows the exact line and column of the problem instead. The formatting runs in a background thread, so even very large files don't freeze the page, and nothing is uploaded.
JavaScript / Node.js
const pretty = JSON.stringify(data, null, 2) // 2 spaces
const tabs = JSON.stringify(data, null, '\t') // tabs
const mini = JSON.stringify(data) // minified
// Format a JSON string you received
const formatted = JSON.stringify(JSON.parse(text), null, 2)From the terminal with Node.js:
node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync(0,'utf8')),null,2))" < data.jsonPython
import json
print(json.dumps(data, indent=2, ensure_ascii=False))ensure_ascii=False keeps characters like é or 你 readable instead of turning them into é. From the terminal, Python has a built-in formatter:
python -m json.tool data.json
python -m json.tool --indent 2 --sort-keys data.jsonjq (command line)
jq . data.json # pretty print with colors
jq -c . data.json # minify (compact)
jq --tab . data.json # indent with tabs
curl -s https://api.example.com/users | jq .jq is also great for pulling out parts of the data. See the JSONPath tutorial for a similar query language you can use in many tools.
VS Code
Open the file and run Format Document:
- Windows:
Shift + Alt + F - Mac:
Shift + Option + F - Linux:
Ctrl + Shift + I
If you pasted JSON into a new tab, set the language to JSON first (bottom-right corner), or VS Code won't know how to format it.
In the browser
Firefox formats JSON responses automatically when you open an API URL. In Chrome and Edge, newer versions show a Pretty-print checkbox on raw JSON pages, and the Network tab in developer tools has a formatted Preview.
Formatting fails? Your JSON is invalid
Every formatter has to parse the JSON first. If it fails, look at the error position, or read 9 common JSON syntax errors. For "almost JSON" with trailing commas, single quotes or comments, JSON Repair fixes and formats it in one step.