1. Pretty Printing with `cURL` + `jq` (Recommended)
The most powerful way to format JSON in terminal is piping `curl` output into `jq`. `jq` is a lightweight C CLI tool that formats JSON with colorized syntax highlighting automatically.
# 1. Basic cURL piped to jq (Colorized 2-space pretty printing)
curl -s https://jsonplaceholder.typicode.com/todos/1 | jq '.'
# 2. Extract specific JSON fields directly
curl -s https://jsonplaceholder.typicode.com/todos/1 | jq '.title'
# 3. Compact / Minify JSON output
curl -s https://jsonplaceholder.typicode.com/todos/1 | jq -c '.'Pro Tips
- Install jq on macOS via `brew install jq` or Ubuntu/Debian via `sudo apt install jq`.
- Use `jq -C` to force color output even when piping to `less -R` pager.
2. Pretty Printing with Built-in Python (`json.tool`)
If `jq` is not installed on a server, Python's standard library includes a built-in `json.tool` module that works out-of-the-box on virtually every Linux/macOS system without installing third-party tools.
# Pipe cURL output to Python 3 json.tool
curl -s https://jsonplaceholder.typicode.com/users/1 | python3 -m json.tool
# Python with 4-space custom indent
curl -s https://jsonplaceholder.typicode.com/users/1 | python3 -m json.tool --indent 43. Creating a Permanent Terminal Shell Alias
To avoid typing long command pipelines repeatedly, add a custom shell alias to your `~/.zshrc` or `~/.bashrc` file:
# Add to ~/.zshrc or ~/.bashrc
alias curljson='curl -s -H "Accept: application/json"'
function fjq() {
curl -s "$1" | jq '.'
}
# Usage:
# fjq https://api.example.com/data