Format JSON from the Command Line

To format JSON on the command line, run python3 -m json.tool data.json — it's built into every Python 3 installation and requires no extra install. For more power, pipe through jq .. Both tools work on Linux, macOS, and Windows.

Prefer a GUI? Paste your JSON into Jsonic's online JSON formatter for instant pretty printing with error highlighting.

Open JSON Formatter

python -m json.tool — no install needed

python3 -m json.tool is the fastest way to format JSON when you already have Python 3, which ships by default on macOS and most Linux distributions. It pretty prints with 4-space indentation and exits with code 1 on invalid JSON — making it useful for CI validation too.

# Format a file and print to stdout
python3 -m json.tool data.json

# Format piped input
echo '{"name":"Alice","age":30}' | python3 -m json.tool

# Save formatted output to a new file
python3 -m json.tool data.json > formatted.json

# Sort keys alphabetically
python3 -m json.tool --sort-keys data.json

# Minify (compact output, no whitespace)
python3 -m json.tool --compact data.json

On Windows with Python in the PATH, use python instead of python3. The --sort-keys flag is handy when diffing two JSON files and key order is inconsistent.

jq — the standard JSON CLI tool

jq is the go-to tool for any serious JSON work in the terminal. It formats, queries, filters, and transforms JSON. The identity filter . outputs the input as pretty-printed JSON. Install once, use everywhere.

# Install
brew install jq          # macOS (Homebrew)
sudo apt install jq      # Ubuntu / Debian
winget install jqlang.jq # Windows (winget)

# Pretty print a file
jq . data.json

# Pretty print from piped input
cat data.json | jq .

# Use 4-space indent instead of the default 2
jq --indent 4 . data.json

# Extract a single field
jq '.name' data.json

# Minify output (compact, no whitespace)
jq -c . data.json

# Format API response from curl
curl -s https://api.example.com/users | jq .

The -r flag outputs raw strings without quotes — useful when extracting a field value to use in a shell script. For example: jq -r '.token' response.json.

Node.js one-liner

If you have Node.js installed but not jq, a quick one-liner handles formatting. You can also add a shell alias to your ~/.bashrc or ~/.zshrc so the command is always available.

# One-liner: read from file via stdin redirect
node -e "
  const d = require('fs').readFileSync('/dev/stdin', 'utf8');
  console.log(JSON.stringify(JSON.parse(d), null, 2));
" < data.json

# Add a permanent alias to ~/.bashrc or ~/.zshrc
alias jsonformat="node -e "process.stdout.write(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2)+'\n')""

# Use the alias after reloading your shell
echo '{"a":1,"b":2}' | jsonformat

# Or use npx with the 'json' package (no global install)
echo '{"a":1}' | npx json

The /dev/stdin trick works on Linux and macOS. On Windows, read from a file directly: node -e "console.log(JSON.stringify(require('./data.json'),null,2))".

curl + jq for API debugging

The most common real-world workflow is piping curl output through jq. The -s flag silences curl's progress meter so only JSON reaches jq.

# GET request — format the full response
curl -s https://httpbin.org/json | jq .

# POST request with a JSON body — format the response
curl -s -X POST https://api.example.com/data \
  -H 'Content-Type: application/json' \
  -d '{"key":"value"}' | jq .

# Extract a nested field from the response
curl -s https://httpbin.org/json | jq '.slideshow.title'

# Get an array element at index 0
curl -s https://api.example.com/users | jq '.[0]'

# List all keys at the top level
curl -s https://api.example.com/config | jq 'keys'

# Format and save the response to a file
curl -s https://api.example.com/data | jq . > response.json

Add -H 'Authorization: Bearer $TOKEN' for authenticated endpoints, referencing a shell variable so credentials stay out of your command history.

Windows: PowerShell

PowerShell has built-in JSON cmdlets that require no installation. The pipeline converts JSON to a .NET object and back, applying indentation automatically. Use -Depth to control how deep nested objects are expanded — the default depth of 2 can truncate deeply nested structures.

# Format a JSON file (PowerShell built-in)
Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10

# Save the formatted output
Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10 | Set-Content formatted.json

# Format an API response
Invoke-RestMethod https://httpbin.org/json | ConvertTo-Json -Depth 10

# Sort keys when converting back to JSON
Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10

# Using Python on Windows (if installed)
python -m json.tool data.json

Always use -Depth 10 (or higher) when working with complex nested objects. Without it, PowerShell silently replaces deeply nested values with the string "System.Object[]".

bat — syntax-highlighted JSON viewing

bat is a cat replacement with syntax highlighting. It auto-detects .json files and renders them with colours in the terminal — useful for reading JSON without reformatting it.

# Install
brew install bat          # macOS
sudo apt install bat      # Ubuntu (the binary may be called 'batcat')

# View a JSON file with syntax highlighting
bat data.json

# Explicitly set language if the extension is missing
bat --language json output

# Combine with jq: format then highlight
jq . data.json | bat --language json

# Page through large files
bat --paging always data.json

On Ubuntu the binary is named batcat. Add alias bat=batcat to your shell profile to match the macOS name.

Validate JSON from the CLI

Both python3 -m json.tool and jq exit with a non-zero code when JSON is invalid, so you can use them in scripts and CI pipelines.

# Validate with Python — prints error message and exits 1 on failure
python3 -m json.tool data.json > /dev/null && echo "valid" || echo "invalid"

# Validate with jq — also exits non-zero on bad JSON
jq . data.json > /dev/null && echo "valid" || echo "invalid"

# Validate multiple files in a loop
for f in *.json; do
  python3 -m json.tool "$f" > /dev/null 2>&1     && echo "$f: OK"     || echo "$f: INVALID"
done

# Use in CI — fail the build on invalid JSON
python3 -m json.tool config.json > /dev/null

When python3 -m json.tool encounters invalid JSON it prints the line number and character offset of the error — helpful when debugging large files.

Minify JSON from the CLI

Minification removes all unnecessary whitespace. Use it to reduce file size before serving JSON over a network or storing it in a key-value store.

# Minify with jq (-c = compact output)
jq -c . data.json

# Minify with Python
python3 -m json.tool --compact data.json

# Minify with Node.js
node -e "
  const d = require('fs').readFileSync('/dev/stdin', 'utf8');
  process.stdout.write(JSON.stringify(JSON.parse(d)));
" < data.json

# Minify and save
jq -c . data.json > minified.json

For an online alternative, use Jsonic's formatter which supports both pretty printing and minification in one click.

Tool comparison

ToolInstall requiredPlatformStrengths
python3 -m json.toolNo (needs Python 3)AllBuilt-in, validates JSON, sort keys
jqYesAllQuery, filter, transform, curl piping
Node.js one-linerNo (needs Node.js)AllNo extra install for JS developers
PowerShell ConvertTo-JsonNoWindowsNative Windows, deep object support
batYesAllSyntax highlighting, paging, easy reading

Frequently asked questions

How do I pretty print JSON in the terminal?

Pipe your JSON through python3 -m json.tool (no install needed) or jq . (install required). For a file: python3 -m json.tool data.json or jq . data.json. For piped input: echo '{"a":1}' | python3 -m json.tool.

How do I format JSON without installing anything?

Use python3 -m json.tool — it ships with Python 3, which is pre-installed on macOS and most Linux distributions. On Windows, use PowerShell: Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10. Both work with zero extra dependencies.

What is jq and how do I use it to format JSON?

jq is a lightweight command-line JSON processor. Install with brew install jq (macOS) or apt install jq (Ubuntu). Format a file with jq . data.json, or pipe input with cat data.json | jq .. It also supports querying, filtering, and transformation — it's the de facto standard for JSON in the terminal.

How do I format the output of a curl command as JSON?

Pipe curl output through jq: curl -s https://api.example.com/data | jq .. The -s flag silences curl's progress output so only the JSON response is piped to jq. For Python: curl -s https://api.example.com/data | python3 -m json.tool.

How do I format JSON on Windows command line?

In PowerShell: Get-Content data.json | ConvertFrom-Json | ConvertTo-Json -Depth 10. Always set -Depth to 10 or higher — the default of 2 silently truncates nested objects. If Python is installed, python -m json.tool data.jsonworks on Windows too.

How do I validate JSON from the command line?

python3 -m json.tool exits with code 1 and prints the error location when the JSON is invalid. jq . also exits non-zero and reports the parse error. Use either in CI scripts: python3 -m json.tool config.json > /dev/null — the build fails automatically if the file is malformed.

Format JSON without the terminal

Need to format a one-off JSON payload without opening a terminal? Paste it into Jsonic's JSON formatter — instant pretty printing, validation, and minification in your browser.

Open JSON Formatter