How to Pretty-Print JSON from the Command Line
Use echo '{"a":1}' | python -m json.tool or echo '{"a":1}' | jq '.'. Both commands take compact JSON as input and output it formatted with indentation. Python's json.tool is built-in on any system with Python. jq is a dedicated JSON processor with more features.
Method 1: Python (Built-in)
# From a string
echo '{"name":"Alice","age":30,"hobbies":["reading","coding"]}' | python -m json.tool
# Output:
# {
# "name": "Alice",
# "age": 30,
# "hobbies": [
# "reading",
# "coding"
# ]
# }
# From a file
python -m json.tool data.json
# With sorted keys
python -m json.tool --sort-keys data.json
Method 2: jq
# Pretty-print
echo '{"name":"Alice","age":30}' | jq '.'
# Extract a field
echo '{"name":"Alice","age":30}' | jq '.name'
# "Alice"
# Compact output (minify)
echo '{"name": "Alice"}' | jq -c '.'
# {"name":"Alice"}
# Install jq
brew install jq # macOS
apt install jq # Ubuntu/Debian
choco install jq # Windows
Method 3: Node.js
echo '{"a":1}' | 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)))"
Try It Online
Use our JSON Formatter to pretty-print JSON in your browser without installing anything.
Frequently Asked Questions
What is jq?
jq is a lightweight command-line JSON processor. It can pretty-print, filter, transform, and query JSON data. Install with brew install jq (macOS), apt install jq (Ubuntu), or choco install jq (Windows).
How do I pretty-print a JSON file in place?
With jq: jq '.' file.json > tmp.json && mv tmp.json file.json. Or with Python: python -c "import json,sys;d=json.load(open(sys.argv[1]));json.dump(d,open(sys.argv[1],'w'),indent=2)" file.json.