When you are working in the terminal — debugging an API, processing log files, or writing shell scripts — you need a way to format JSON without leaving the command line. This article covers the three most practical approaches: jq, Python's built-in formatter, and Node.js.
Option 1 — jq (recommended)
jq is the standard command-line JSON processor. It parses, formats, filters, and transforms JSON. Install it with your package manager:
# macOS brew install jq # Debian / Ubuntu sudo apt install jq # Fedora / RHEL sudo dnf install jq
Pretty-print a file
jq . data.json
The . filter means "output the whole document as-is." Combined with jq's default pretty-printing, this formats any valid JSON file with indentation and syntax-colored output in the terminal.
Format an API response on the fly
curl -s https://api.github.com/users/octocat | jq .
The -s flag silences curl's progress meter so only the JSON appears in the pipe.
Write formatted output to a file
jq . input.json > formatted.json
Extract a specific field
# Print just the "name" field from a JSON object jq '.name' data.json # Print all items in an array jq '.[]' items.json # Print a specific field from each array element jq '.[].email' users.json
Minify JSON with jq
jq -c . data.json # -c for compact output
Option 2 — Python's built-in json.tool
If Python 3 is installed (it is on most systems), you can format JSON without installing anything:
# Format a file
python3 -m json.tool data.json
# Format from stdin (pipe)
echo '{"name":"Alice","age":30}' | python3 -m json.tool
# Write to a file
python3 -m json.tool data.json > formatted.json
# Use a 4-space indent (default is 4; change with --indent)
python3 -m json.tool --indent 2 data.json
json.tool validates the JSON as it formats — if the input is invalid, it prints an error with the location of the problem.
Option 3 — Node.js one-liner
If Node.js is available and you prefer JavaScript:
# Format from a file
node -e "const fs=require('fs'); const d=JSON.parse(fs.readFileSync('data.json')); console.log(JSON.stringify(d,null,2));"
# Format from stdin
echo '{"name":"Alice"}' | node -e "let d=''; process.stdin.on('data',c=>d+=c); process.stdin.on('end',()=>console.log(JSON.stringify(JSON.parse(d),null,2)));"
The Node.js approach is useful in environments where jq is not available but Node.js is, such as some CI containers.
Validating JSON in bash
Both jq and python3 -m json.tool act as validators — they exit with a non-zero status code if the input is not valid JSON. This is useful in shell scripts:
if jq . data.json > /dev/null 2>&1; then
echo "Valid JSON"
else
echo "Invalid JSON — check data.json"
exit 1
fi
Formatting JSON in CI/CD pipelines
A common CI task is to verify that config files are valid JSON before deploying:
# In a GitHub Actions step
- name: Validate JSON configs
run: |
for f in config/*.json; do
jq . "$f" > /dev/null || (echo "Invalid: $f" && exit 1)
done
When to use an online formatter instead
For one-off formatting when you are not in a terminal, the JSON Formatter offers tree navigation, syntax highlighting, and copy-to-clipboard — without needing to install any tool. For batch processing or scripting, jq is the right choice.