Recently I’ve worked on a small project where I needed to pretty print JSON from the command line for quick verification.
For the task I created a pp alias in my .bashrc that worked perfectly (all the code is on the same line):
alias pp='python -c "import sys, json; print json.dumps( json.load(sys.stdin), sort_keys=True, indent=4)"'
Just today I was re-reading the official Python documentation on a json package and came across a small gem – json.tool module that is used for validation and pretty-printing.
I modified my alias which is now way shorter and does the same job as my old one:
alias pp='python -mjson.tool'
And here is pretty-printing in action:

If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately.
You can also follow me on GitHub or Twitter.



{ 9 comments… read them below or add one }
Nice tip, thanks!
Hi Jakub,
You’re welcome
Really handy. Just added it to my .zshrc.
Just in case somebody need to pretty print json from website: http://www.pretty-print.org
YOu should mention that you need python version 2.6 or later. 2.3.3 doesn’t have it.
Very very nice, this will help me significantly. Thanks for the tips! It’s faster than copying and pasting into an online parser.
You’re welcome
Thanks a lot! Very nice shortcut.
Thanks. Great tip. BTW, your alias might collide with ‘pp’ on some machines with PAR packager. Maybe alias to ‘jpp’ or at least recommending checking for collisions?
{ 1 trackback }