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

python pretty print json (20)


I combine Python's json.tool with pygmentize:

echo '{"foo": "bar"}' | python -m json.tool | pygmentize -g

There are some alternatives to pygmentize which are listed in my this answer.

Here is a live demo:

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"

Vanilla Bash

A simple Bash script (grep/awk) for pretty JSON printing, without third party install:


grep -Eo '"[^"]*" *(: *([0-9]*|"[^"]*")[^{}\["]*|,)?|[^"\]\[\}\{]*|\{|\},?|\[|\],?|[0-9 ]*,?' | awk '{if ($0 ~ /^[}\]]/ ) offset-=4; printf "%*c%s\n", offset, " ", $0; if ($0 ~ /^[{\[]/) offset+=4}'


1) Read file and pretty print in console

cat file.json |

2) Use with the windows GIT Bash from file to file (UTF8 based):

cat fileIn.json |sh.exe > fileOut.json

JSONLint has an open-source implementation on github can be used on the command line or included in a node.js project.

npm install jsonlint -g

and then

jsonlint -p myfile.json


curl -s "" | jsonlint | less

yajl is very nice, in my experience. I use its json_reformat command to pretty-print .json files in vim by putting the following line in my .vimrc:

autocmd FileType json setlocal equalprg=json_reformat

UPDATE I'm using jq now as suggested in another answer. It's extremely powerful at filtering JSON, but, at its most basic, also an awesome way to pretty print JSON for viewing.

jsonpp is a very nice command line JSON pretty printer.

From the README:

Pretty print web service responses like so:

curl -s -L http://<!----> | jsonpp

and make beautiful the files running around on your disk:

jsonpp data/long_malformed.json

If you're on Mac OS X, you can brew install jsonpp. If not, you can simply copy the binary to somewhere in your $PATH.

Check out Jazor. It's a simple command line JSON parser written in Ruby.

gem install jazor
jazor --help

I use jshon to do exactly what you're describing. Just run:


You can also pass arguments to transform the JSON data.

I use the "space" argument of JSON.stringify to pretty-print JSON in JavaScript.


// Indent with 4 spaces
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, 4);

// Indent with tabs
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, '\t');

From the Unix command-line with nodejs, specifying json on the command line:

$ node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));" \


    "foo": "lorem",
    "bar": "ipsum"

From the Unix command-line with Node.js, specifying a filename that contains JSON, and using an indent of four spaces:

$ node -e "console.log(JSON.stringify(JSON.parse(require('fs') \
      .readFileSync(process.argv[1])), null, 4));"  filename.json

Using a pipe:

echo '{"foo": "lorem", "bar": "ipsum"}' | node -e \

I wrote a tool that has one of the best "smart whitespace" formatters available. It produces more readable and less verbose output than most of the other options here.


This is what "smart whitespace" looks like:

I may be a bit biased, but it's an awesome tool for printing and manipulating JSON data from the command-line. It's super-friendly to use and has extensive command-line help/documentation. It's a Swiss Army knife that I use for 1001 different small tasks that would be surprisingly annoying to do any other way.

Latest use-case: Chrome, Dev console, Network tab, export all as HAR file, "cat site.har | underscore select '.url' --outfmt text | grep mydomain"; now I have a chronologically ordered list of all URL fetches made during the loading of my company's site.

Pretty printing is easy:

underscore -i data.json print

Same thing:

cat data.json | underscore print

Same thing, more explicit:

cat data.json | underscore print --outfmt pretty

This tool is my current passion project, so if you have any feature requests, there is a good chance I'll address them.

I'm using httpie

$ pip install httpie

And you can use it like this

 $ http PUT localhost:8001/api/v1/ports/my 
 HTTP/1.1 200 OK
 Connection: keep-alive
 Content-Length: 93
 Content-Type: application/json
 Date: Fri, 06 Mar 2015 02:46:41 GMT
 Server: nginx/1.4.6 (Ubuntu)
 X-Powered-By: HHVM/3.5.1

     "data": [], 
     "message": "Failed to manage ports in 'my'. Request body is empty", 
     "success": false

Install yajl-tools with the command below:

sudo apt-get install yajl-tools


echo '{"foo": "lorem", "bar": "ipsum"}' | json_reformat

It is not too simple with a native way with the jq tools.

For example:

cat xxx | jq .

Or, with Ruby:

echo '{ "foo": "lorem", "bar": "ipsum" }' | ruby -r json -e 'jj JSON.parse gets'

Simply pipe the output to jq ..


twurl -H '.......' | jq .

That's how I do it:

curl yourUri | json_pp

It shortens the code and gets the job done.

The JSON Ruby Gem is bundled with a shell script to prettify JSON:

sudo gem install json
echo '{ "foo": "bar" }' | prettify_json.rb

Script download:

Try pjson. It has colors!

Install it with pip:

⚡ pip install pjson

And then pipe any JSON content to pjson.

With Perl, if you install JSON::PP from CPAN you'll get the json_pp command. Stealing the example from B Bycroft you get:

[[email protected] ~]$ echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp
   "bar" : "ipsum",
   "foo" : "lorem"

It's worth mentioning that json_pp comes pre-installed with Ubuntu 12.04 (at least) and Debian in /usr/bin/json_pp

With Python 2.6+ you can just do:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool

prettyjson_f() {
    python -m json.tool "$1"

prettyjson_w() {
    curl "$1" | python -m json.tool

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

You can use: jq

It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.

Here is an example:

$ jq . <<< '{ "foo": "lorem", "bar": "ipsum" }'
  "bar": "ipsum",
  "foo": "lorem"

Or in other words:

$ echo '{ "foo": "lorem", "bar": "ipsum" }' | jq .
  "bar": "ipsum",
  "foo": "lorem"