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.

cURL + jq Command Examples
# 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 '.'
Explanation: The `-s` flag silences cURL progress output so only clean formatted JSON appears.

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.

cURL + Python json.tool Command
# 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 4
Explanation: `python3 -m json.tool` parses stdin and outputs 2-space indented formatted JSON.

3. Creating a Permanent Terminal Shell Alias

To avoid typing long command pipelines repeatedly, add a custom shell alias to your `~/.zshrc` or `~/.bashrc` file:

Custom Shell Alias in ~/.zshrc
# 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
Explanation: `fjq <URL>` fetches and formats JSON in one command.