Pretty print JSON on the command line with Python
I’m often dealing with little bits of JSON, and sometimes it’s helpful to pretty-print them for readability.
Python has a handy tool to make that easy on the command line: json.tool
.
E.g.
echo '{"foo": "bar", "thing": { "baz": 42 } }' | python -m json.tool
prints:
{
"foo": "bar",
"thing": {
"baz": 42
}
}
If you’re on Mac, you can convert JSON in the clipboard to its pretty-printed form with:
pbpaste | python -m json.tool | pbcopy
Ubuntu equivalent with xclip
:
xclip -o | python -m json.tool | xclip
I have that aliased to pbj
.
Mac:
alias pbj='pbpaste | python -m json.tool | pbcopy'
Ubuntu:
alias pbj='xclip -o | python -m json.tool | xclip'