Programming

JSON pretty-print in Shell Scripts - jq, python, php

Pretty-print JSON in shell scripts using jq, Python's json.tool, php or json_pp. One-line examples for bash, curl, NDJSON and safe in-place file updates.

1 answer 5 views

How can I pretty-print JSON in a shell script?

Is there a (Unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

json
{ "foo": "lorem", "bar": "ipsum" }

… into something like this:

json
{
 "foo": "lorem",
 "bar": "ipsum"
}

Use a dedicated JSON tool (jq) or a language builtin (Python’s json.tool, php/json_pp) to json pretty print in a shell script. For bash json one‑liners try: echo ‘{“foo”:“lorem”,“bar”:“ipsum”}’ | jq . or python3 -m json.tool < file.json — both will format that single‑line object into an indented, human‑readable block. jq is the usual recommendation for speed, flexibility and piping into other filters.


Contents


Quick one-liners

If you just want to turn:

json
{ "foo": "lorem", "bar": "ipsum" }

into something readable, these one-liners do the job:

  • jq (recommended):
bash
echo '{"foo":"lorem","bar":"ipsum"}' | jq .
# or
jq . file.json
  • Python (built-in module):
bash
echo '{"foo":"lorem","bar":"ipsum"}' | python3 -m json.tool
# or
python3 -m json.tool < file.json
  • Perl json_pp:
bash
echo '{"foo":"lorem","bar":"ipsum"}' | json_pp
# or
json_pp < file.json

Any of the above will print a nicely indented object, e.g.:

json
{
 "foo": "lorem",
 "bar": "ipsum"
}

For quick reference and community examples see the original Stack Overflow answers (example thread).


Pretty-print JSON with jq (recommended)

Why jq? It’s a lightweight, fast JSON processor designed for the command line. You can pretty-print, filter, transform and validate JSON with the same tool. Example uses:

  • Pretty-print a file:
bash
jq . file.json
  • Pretty-print from a web request:
bash
curl -s https://api.example.com/data | jq .
  • Read from stdin (here‑document):
bash
jq . <<< '{"foo":"lorem","bar":"ipsum"}'

Extras:

  • Minify with -c (compact): jq -c . file.json
  • Colorize output (if desired): many jq builds auto-color when outputting to a terminal; see your jq docs.

Install:

  • Debian/Ubuntu: sudo apt-get install -y jq
  • macOS: brew install jq
  • RHEL/CentOS: sudo yum install jq or dnf install jq

Good documentation and examples are in the Linode jq guide: https://www.linode.com/docs/guides/using-jq-to-process-json-on-the-command-line/ and practical walkthroughs such as https://lornajane.net/posts/2024/pretty-print-json-with-jq.

I’ve found jq the most convenient when you need more than just pretty printing — want to pick fields, filter arrays or convert NDJSON to arrays, jq does it all.


Pretty-print JSON with Python’s json.tool

Python ships with a tiny helper module that pretty‑prints JSON. It’s great when Python is already available.

Examples:

bash
# Read file
python3 -m json.tool < file.json

# From stdin
echo '{"foo":"lorem","bar":"ipsum"}' | python3 -m json.tool

Notes:

  • Use python3 on systems where python is Python 2 or not present.
  • If you need to preserve non-ASCII characters, use a short Python snippet:
bash
echo '{"text":"✓"}' | python3 -c 'import sys,json; print(json.dumps(json.load(sys.stdin), ensure_ascii=False, indent=2))'

See practical references: https://alexwlchan.net/2015/pretty-print/ and community Q/A examples at https://stackoverflow.com/questions/352098/how-can-i-pretty-print-json-in-a-shell-script.


Using php or json_pp

If you have PHP or Perl tools available:

  • PHP (CLI):
bash
echo '{"foo":"lorem","bar":"ipsum"}' | php -r 'echo json_encode(json_decode(file_get_contents("php://stdin")), JSON_PRETTY_PRINT);'
  • json_pp (Perl):
bash
json_pp < file.json
# or
cat file.json | json_pp

Tutorials that list these options: https://linuxhandbook.com/pretty-print-json/ and https://tecadmin.net/pretty-print-json-in-linux/.


Integrate pretty-printing into bash scripts (pipes, curl, in-place)

Want to use pretty-printing inside a script? Patterns that work well:

  • Pipe JSON through jq (common inside scripts):
bash
result=$(curl -s https://api.example.com/data | jq '.items[0]')
echo "$result"
  • Save pretty output back to the same file (safe pattern):
bash
tmp=$(mktemp)
jq . file.json > "$tmp" && mv "$tmp" file.json
  • Using sponge (from moreutils) to avoid temp files:
bash
sudo apt-get install -y moreutils
jq . file.json | sponge file.json
  • Add a small helper in your ~/.bashrc:
bash
jsonf() { jq . "${1:-/dev/stdin}"; }
# usage: jsonf file.json OR curl -s url | jsonf

Which approach you pick depends on permissions and whether atomic updates matter. Temp‑file + mv is portable and safe.


Edge cases, NDJSON and validation

A few gotchas to watch for:

  • NDJSON (newline-delimited JSON): A file with multiple JSON objects separated by newlines is not one JSON document. jq . file.ndjson will pretty-print each object individually; to turn all lines into an array use jq -s . file.ndjson.

  • Validation: Use the formatter as a quick validator. jq . file.json > /dev/null or python3 -m json.tool < file.json > /dev/null will fail (non-zero exit) on invalid JSON — handy in scripts.

  • Numeric / formatting changes: Formatters parse and reserialize JSON. That means numeric formatting can change (for example, an exponent notation might be printed differently). If you depend on exact original formatting of numbers or spacing, don’t use a reserializer.

  • Performance & very large files: jq is faster and more memory-efficient than many alternatives for big files. For enormous files, consider streaming-aware tools and check memory usage.

For NDJSON and parsing caveats see the Stack Overflow discussion and Unix answers: https://stackoverflow.com/questions/20265439/how-can-i-pretty-print-a-json-file-from-the-command-line and https://unix.stackexchange.com/questions/444610/how-can-i-pretty-format-a-json-file-with-all-the-correct-indents-and-everything.


Sources


Conclusion

For shell scripts the simplest, most flexible solution is jq — it handles pretty printing, filtering and streaming with one consistent interface. If jq isn’t available, python3 -m json.tool or json_pp are reliable fallbacks for quick json pretty print tasks. For in-place edits use a safe temp-file pattern or sponge; for NDJSON remember to treat each line as a separate JSON value (or slurp with jq -s .).

Authors
Verified by moderation
Moderation
JSON pretty-print in Shell Scripts - jq, python, php