Formatting JSON in command line

Python json.tool to the rescue!

Posted by Tomy Jaya on January 22, 2016

Today I Learnt (TIL)

To format/ pretty-print a one-line JSON file, you can use python json.tool module.

For example:

python -m json.tool instafeed.json > instafeed_formatted.json

Piping can also work:

echo {name: "Tomy", gender: "M"} | python -m json.tool 

If you’re an Amazon Linux or Mac OS user, this command is very handy as both of them come with Python 2.6+ installed by default.

Hence, it’s recommended to alias it:

alias prettyjson='python -m json.tool'

Speaking of aliases, perhaps I should share my alias list (in Mac and Amazon EC2) in a future blog post. Please remind me in case I forget. :)

Source:

http://stackoverflow.com/questions/352098/how-can-i-pretty-print-json