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:
{ "foo": "lorem", "bar": "ipsum" }
… into something like this:
{
"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
- Pretty-print JSON with jq (recommended)
- Pretty-print JSON with Python’s json.tool
- Using php or json_pp
- Integrate pretty-printing into bash scripts (pipes, curl, in-place)
- Edge cases, NDJSON and validation
- Sources
- Conclusion
Quick one-liners
If you just want to turn:
{ "foo": "lorem", "bar": "ipsum" }
into something readable, these one-liners do the job:
- jq (recommended):
echo '{"foo":"lorem","bar":"ipsum"}' | jq .
# or
jq . file.json
- Python (built-in module):
echo '{"foo":"lorem","bar":"ipsum"}' | python3 -m json.tool
# or
python3 -m json.tool < file.json
- Perl json_pp:
echo '{"foo":"lorem","bar":"ipsum"}' | json_pp
# or
json_pp < file.json
Any of the above will print a nicely indented object, e.g.:
{
"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:
jq . file.json
- Pretty-print from a web request:
curl -s https://api.example.com/data | jq .
- Read from stdin (here‑document):
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 jqordnf 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:
# Read file
python3 -m json.tool < file.json
# From stdin
echo '{"foo":"lorem","bar":"ipsum"}' | python3 -m json.tool
Notes:
- Use
python3on systems wherepythonis Python 2 or not present. - If you need to preserve non-ASCII characters, use a short Python snippet:
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):
echo '{"foo":"lorem","bar":"ipsum"}' | php -r 'echo json_encode(json_decode(file_get_contents("php://stdin")), JSON_PRETTY_PRINT);'
- json_pp (Perl):
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):
result=$(curl -s https://api.example.com/data | jq '.items[0]')
echo "$result"
- Save pretty output back to the same file (safe pattern):
tmp=$(mktemp)
jq . file.json > "$tmp" && mv "$tmp" file.json
- Using sponge (from moreutils) to avoid temp files:
sudo apt-get install -y moreutils
jq . file.json | sponge file.json
- Add a small helper in your ~/.bashrc:
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.ndjsonwill pretty-print each object individually; to turn all lines into an array usejq -s . file.ndjson. -
Validation: Use the formatter as a quick validator.
jq . file.json > /dev/nullorpython3 -m json.tool < file.json > /dev/nullwill 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
- Stack Overflow — How can I pretty-print JSON in a shell script?
- Unix & Linux StackExchange — How can I pretty format a JSON file with all the correct indents and everything?
- How to Pretty Print JSON File in Linux Terminal — ItsFOSS
- Pretty-print JSON with jq — LornaJane.net
- Pretty printing JSON and XML in the shell — Alexwlchan
- Pretty Print JSON in Linux Command Line — LinuxHandbook
- How to Pretty Print JSON in Linux Shell Script — TecAdmin
- The Best Way To Pretty Print JSON On The Command-Line — Skorks
- Gist — Pretty print JSON via Elixir (example)
- JSON Pretty Print — jsonformatter.org
- Stack Overflow — How can I pretty-print a JSON file from the command line?
- Pretty Print JSON in Linux Terminal — Thanos Koutr
- Parse And Pretty Print JSON Using Jq In Linux — OSTechNix
- Commandline JSON Pretty Print everywhere — Coderwall
- Pretty Print JSON & Move it to Command Line — Akshay Ranganath
- Top 5 Techniques to Pretty Print JSON Files — FossLinux
- How Can I PrettyPrint JSON in a Shell Script — SaturnCloud
- jsonpretty — GitHub (CLI pretty-printer)
- How to Pretty Print JSON Output in cURL — Mkyong
- How to Use JQ to Process JSON on the Command Line — Linode Docs
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 .).