Philip Youssef bio photo

Philip Youssef

Engineering @LendUpLoans. Previously @Twitter, @Groupon, @Microsoft.
Pasta enthusiast & restless programmer.

Twitter LinkedIn Github Stackoverflow
JSON has become one of the main formats for exchanging data through HTTP APIs. Being able to request and properly print json in your console is useful from time to time. Here are three ways to easily "pretty print" your data.

Option 1 - Simple formatting using python 

If you have access to python on your system, simply add an alias to "python -mjson.tool" to pretty print any well formed json. Execute one of the following commands to add the alias directly (depending on whether you use bash or zsh):
See an example in action:

Option 2 - Prettyjson: a colorized option

prettyjson is a node.js package that you can call form the command line to get colorized and bracket stripped json printed in your CLI.

To install:
Once installed, you can try it out. Here is a before shot:

And after:

As you can see prettyjson does a good job of stripping superflous brackets and quotes. 

Note: If you can't execute prettyjson after installing it, make sure the node package manager bin folder is in your path. You can do so by adding the following to your .bashrc/zshrc:
export PATH=/usr/local/share/npm/bin/:$PATH

Option 3 - underscore-cli

Yet another node.js based option is the underscore-cli package. It doesn't strip some of the less useful json characters form the display like prettyjson, but it does colorize your text and it has other powerful features.

To install:
sudo npm install -g underscore

Underscore pretty formatting in action:

It's not just pretty printing that makes this package neat, you can extract specific portions of it as well:

Head on over to ddposon's github page for more details.