How to Format JSON in Bash – jq, Python, and Terminal Tools

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.

Try it directly in your browser:

Format JSON in browser →

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.

Frequently Asked Questions

How do I pretty-print JSON in bash?

The two most common ways are: (1) echo '{"key":"value"}' | jq . — using jq, the JSON processor for the command line; or (2) echo '{"key":"value"}' | python3 -m json.tool — using Python's built-in JSON formatter. Both produce indented, readable output.

How do I format a JSON file with jq?

Run: jq . input.json — this reads the file and pretty-prints it to stdout. To write the formatted output back to a file, use: jq . input.json > formatted.json

How do I format a JSON API response in bash?

Pipe the curl output to jq: curl -s https://api.example.com/data | jq . — the -s flag silences curl's progress meter, and jq . pretty-prints the JSON response with syntax highlighting in the terminal.

Ready to format json in browser?

Open JSON Formatter →
About the author

Pasindu Ishan is a Senior Software Engineer working on data streaming systems. These guides are about writing JSON that other people’s parsers will accept, including the parts of the format that are stricter than they look. He builds the free in-browser tools at JSON Dev Tools.