How to Pretty Print JSON from the Command Line

By Sheng Pang · Published · 3 min read

A JSON response squashed onto one line is unreadable. Every operating system has at least one tool already installed that will indent it for you, and one small install gives you a full query language. Here are the commands, shortest first.

Python, already installed almost everywhere

python3 -m json.tool data.json
cat data.json | python3 -m json.tool
python3 -m json.tool --indent 2 data.json      # Python 3.9 and later
python3 -m json.tool --sort-keys data.json

macOS and nearly every Linux distribution ship Python, so this works with no setup. It validates too: invalid input prints the error with a line and column.

jq, the tool worth installing

brew install jq          # macOS
sudo apt install jq      # Debian and Ubuntu
winget install jqlang.jq # Windows

Pretty print:

jq . data.json
curl -s https://api.example.com/users | jq .

Compact to one line:

jq -c . data.json

Pull out a field:

jq '.name' data.json
jq '.users[0].email' data.json
jq '.users[].email' data.json          # every email
jq -r '.users[].email' data.json       # raw strings without quotes

Filter an array:

jq '.users[] | select(.active == true)' data.json
jq '[.users[] | select(.age > 30) | .name]' data.json

Reshape:

jq '{count: (.users | length), names: [.users[].name]}' data.json

Sort keys for a stable diff:

jq -S . a.json > a.sorted.json
jq -S . b.json > b.sorted.json
diff a.sorted.json b.sorted.json

jq output is coloured when writing to a terminal and plain when piped, which is exactly what you want.

Node

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

Longer, but useful in a Node project with no other tools. Most people alias it.

PowerShell on Windows

Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10
Invoke-RestMethod https://api.example.com/users | ConvertTo-Json -Depth 10

The -Depth flag matters. The default of 2 silently flattens anything nested deeper.

From the clipboard

Copy some JSON, then:

pbpaste | jq .                    # macOS
xclip -o | jq .                   # Linux with xclip
Get-Clipboard | ConvertFrom-Json | ConvertTo-Json -Depth 10   # PowerShell

Add | pbcopy at the end on macOS to put the formatted result back on the clipboard.

Handy aliases

# add to ~/.zshrc or ~/.bashrc
alias jsonpp='python3 -m json.tool'
alias jsonmin='jq -c .'

Inside your editor

VS Code formats a JSON file with Shift Alt F on Windows and Linux or Shift Option F on macOS. For a snippet in an untitled tab, set the language mode to JSON first. Vim users can run :%!jq . to filter the buffer through jq in place.

When you are not at a terminal

On a machine without your tools, or when the JSON is already in the browser, paste it into our online JSON formatter. It formats, minifies, sorts keys and reports syntax errors with line numbers, and nothing you paste leaves your browser.

← Back to all articles