Pretty-Print JSON in the Terminal
Using jq, Python's json.tool, curl, and other command-line tools to format JSON output in your shell.
API responses from curl, log entries piped from a service, or JSON files you cat — they all come out as a single minified line that's nearly impossible to read. Fortunately, several tools can format JSON in your terminal without opening a browser or an editor. Here are the most useful options, from simplest to most powerful.
jq — the standard tool for JSON in the terminal
jq is a lightweight command-line JSON processor. It formats, filters, transforms, and validates JSON. For basic pretty-printing, the identity filter . is all you need:
# Pretty-print a JSON file
jq '.' data.json
# Pretty-print JSON from a string
echo '{"name":"Alice","age":30}' | jq '.'
# Pretty-print and pipe through less for large output
jq '.' large-data.json | less
Install jq
brew install jq # macOS
apt-get install jq # Ubuntu/Debian
yum install jq # CentOS/RHEL
scoop install jq # Windows (Scoop)
jq beyond formatting
jq is far more powerful than a simple formatter. A few useful patterns:
# Extract a specific field
jq '.name' data.json
# Extract nested value
jq '.user.address.city' data.json
# Get all values in an array
jq '.[] | .name' users.json
# Compact (minify) output
jq -c '.' data.json
# Sort keys alphabetically
jq -S '.' data.json
# Filter array by a condition
jq '[.[] | select(.active == true)]' users.json
curl + jq: the most common pattern
The most common use case: making an API call with curl and reading the JSON response. Pipe curl's output directly to jq:
# GET request — pretty-print the response
curl https://api.example.com/users | jq '.'
# POST request with a JSON body
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}' | jq '.'
# Add -s to silence curl's progress bar
curl -s https://api.example.com/data | jq '.'
# Extract just one field from the response
curl -s https://api.example.com/user/1 | jq '.email'
The -s flag on curl suppresses the transfer progress output so only the JSON goes to jq.
Python's json.tool — no installation needed
Python 3 ships with a built-in JSON formatter as part of the standard library. No package installation required — if Python is on your system, this works immediately:
# Format a JSON file
python3 -m json.tool data.json
# Pipe from curl
curl -s https://api.example.com/data | python3 -m json.tool
# Format a JSON string
echo '{"name":"Alice","age":30}' | python3 -m json.tool
# Sort keys alphabetically
python3 -m json.tool --sort-keys data.json
# Control indent size (Python 3.9+)
python3 -m json.tool --indent 4 data.json
Python's json.tool is slightly slower than jq for large files and has fewer filtering capabilities, but it's always available and produces clean output. It also validates the JSON in the process — if the input is invalid, it exits with an error message.
Node.js
If Node.js is available and jq isn't, a quick one-liner works:
# Format a JSON file
node -e "process.stdin.resume();let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>console.log(JSON.stringify(JSON.parse(d),null,2)));" < data.json
# Simpler with a script file (json-format.js)
# process.stdout.write(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2));
# Usage: node json-format.js < data.json
bat — syntax highlighting for JSON files
bat is a cat replacement with syntax highlighting. It doesn't format JSON, but it displays already-formatted JSON with colour-coded syntax — useful for reviewing JSON files without a GUI editor:
brew install bat # macOS
apt-get install bat # Ubuntu
# View a JSON file with syntax highlighting
bat data.json
# Combine with jq: format then syntax-highlight
jq '.' data.json | bat -l json
Using httpie instead of curl
httpie is an alternative to curl designed for human-friendly API interaction. It pretty-prints and syntax-highlights JSON responses by default, with no extra piping required:
pip install httpie
# GET — response is automatically pretty-printed
http GET https://api.example.com/users
# POST with JSON body
http POST https://api.example.com/users name=Alice age:=30
Validating JSON in a shell script
Both jq and Python's json.tool exit with a non-zero status code if the input is invalid JSON. This makes them useful for CI scripts and pre-commit hooks:
#!/bin/bash
# Validate all .json files in a directory
for f in *.json; do
if jq '.' "$f" > /dev/null 2>&1; then
echo "$f: valid"
else
echo "$f: INVALID" >&2
exit 1
fi
done
For browser-based validation during development, the JSON Validator gives you exact line and column numbers with no terminal setup.
Format JSON in your browser — no terminal needed
Paste any JSON and get formatted output instantly. No installation required.
Open JSON Formatter →Frequently Asked Questions
How do I pretty-print JSON from curl?
Pipe the curl output to jq: curl https://api.example.com/data | jq '.' — jq pretty-prints and syntax-highlights the JSON. If jq is not available, use Python: curl https://api.example.com/data | python3 -m json.tool
How do I install jq?
On macOS: brew install jq. On Ubuntu/Debian: apt-get install jq. On CentOS/RHEL: yum install jq. On Windows: scoop install jq or choco install jq.
Is there a way to pretty-print JSON without installing anything?
Yes — if you have Python 3 installed (it ships with macOS and most Linux distributions): python3 -m json.tool data.json, or pipe to it: echo '{"key":"value"}' | python3 -m json.tool. No external package installation needed.
How do I extract a field from a JSON response in curl?
Pipe to jq with a field selector: curl -s https://api.example.com/user | jq '.email'. For nested values: jq '.address.city'. For a raw string output (without quotes): jq -r '.email'.