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.


echo '{"foo": "bar", "thing": { "baz": 42 } }' | python -m json.tool


    "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.


alias pbj='pbpaste | python -m json.tool | pbcopy'


alias pbj='xclip -o | python -m json.tool | xclip'

Tech mentioned