The Best Way To Pretty Print JSON On The Command-Line

Print

Developers tend to work with APIs a lot and these days most of these APIs are JSON. These JSON strings aren’t exactly easy to read unless they are formatted well. There are many services online that can pretty print JSON for you, but that’s annoying. I love the command-line and whenever I am playing with new APIs or writing my own I mostly use CURL which means I need a good way to pretty print my JSON on the command-line. It should be simple, quick, easy to remember, easy to install – we’re not trying to solve complex algorithms, just format some JSON.

The ‘Good’ Old Faithful

One way that is always available is the Python JSON tool. So you can always do this:

echo '{"b":2, "a":1}' | python -mjson.tool

Which will give you:

{
    "a": 1,
    "b": 2
}

This is alright and, as I said, it is always available. However note that it has sorted our keys which is a major disadvantage. It is also a bit of a handful to write when you just want to pretty print some JSON. I only ever use this when I am on an unfamiliar computer and there is nothing better.

YAJL Tools

If you’re not using YAJL you should be. It is a small JSON library written in C. The parser is event driven and super fast. In the Ruby world we have a nice set of bindings and there are bindings for other languages as well. It is my go-to JSON library.

YAJL also cames with a couple of tools, json_reformat and json_verify. These are pretty self-explanatory and you can get your hands on them like this:

brew install yajl

or

sudo apt-get install yajl-tools

After that all you have to do is:

echo '{"b":2, "a":1}' | json_reformat

Which will give you:

{
    "b": 2,
    "a": 1
}

This is pretty nice. My one tiny niggle is that json_reformat is still a bit of a mouthful, but if you just want basic JSON formatting, it’s a good solution.

Ppjson

Of course being developers, we don’t have to put up with even minor niggles since we can build ourselves exactly the tools we need (and a little bit extra to boot). I was writing a command-line framework and I just happened to need a command-line tool, which is how I ended up with ppjson. I use Ruby quite a bit, so it is a Ruby tool and even if I do say so myself, it’s pretty nice.

You just need to:

gem install ppjson

This will let you do:

echo '{"b":2, "a":1}' | ppjson

Which will give you:

{
  "b": 2,
  "a": 1
}

It uses YAJL through multi_json under the hood, so you still get the speed of YAJL, but it can also do a few extra things for you.

You can pass or pipe it some JSON and it will pretty print it to standard output for you. But you can also pass it a file name with some JSON in it and it will pretty print the contents of the file for you:

ppjson -f abc123.json

You can also store the pretty printed contents back into the file:

ppjson -fi abc123.json

Sometimes you have saved some pretty printed JSON in a file, but now you want to use it as a body of a CURL POST request, for example. Well ppjson can uglify your JSON for you as well:

ppjson -fu abc123.json

This will output a minified JSON string to standard output. And of course you can also update the original file with the uglified JSON as well:

ppjson -fui abc123.json

It will do you basic JSON pretty printing with an easy to remember executable name, but it also has a few nice convenience features to make your life that little bit easier.

The best part is that using Escort, it was a breeze to write. I’ll talk about some of the other interesting projects that ‘fell out’ of Escort some other time.

Anyway, now you no longer need to remember that IDE or editor shortcut to format your JSON or look for an online tool to do the same, the command-line has you more than covered.

Image by NS Newsflash

  • http://www.codingninja.co.uk/ Zen Master

    Cool post. For DOS users the good “old” faithful works, but you need to exclude the single quotes around the json thus: echo {“b”:2, “a”:1} | python -mjson.tool

  • http://skorks.com/ Alan Skorkin

    Yeah someone had already mentioned jq to me – looks awesome.

  • http://twitter.com/tparvi Toni Parviainen

    I’ve been using Tidy JSON (DOS) http://www.raboof.com/projects/tidyjson/

  • http://matpalm.com mat kelcey

    alias pp=’python -mjson.tool’ has lived in my .bashrc for some time now :) (it would be just p, if that wasn’t already taken)

    but why exactly is sorting the keys a bad thing? maps are unsorted yeah? anyone depending on the order is going to get bitten as the json moves around and is unpacked/repacked by various parts of the system.

    • http://skorks.com/ Alan Skorkin

      Yeah aliasing, I gotta teach myself to use it more :). You certainly don’t want to depend on the order, however you often pretty print for the sake of readability/debugging purposes. You want to be able to tell at a glance if the document looks right, if certain keys are missing/added or if certain values are not what you expect.

      When the keys are reordered you have to carefully look through the whole document which can be very burdensome with very large documents.

  • mayhap

    node -e ‘console.log(JSON.stringify(JSON.parse(process.argv[1]),”",2))’ ‘{“b”:2, “a”:1}’

    Probably a way to read from stdin too, but too lazy to look it up.

  • http://theagileplanner.com/ Graham Ashton

    Nice. I aliased `python -mjson.tool` to ppjson many moons ago. Hadn’t noticed the ordering change, but then for the things I’ve used it for it hasn’t been an issue.

  • Paulo

    Hi Alan! Nice article…and glad you’re back (I thought you’re start-up article would be your last)!

    btw, if you find some time to spend on the long awaited (at least by me :) article about the “Ace up your sleeve” you mentioned in your article “You Don’t Need Math Skills…”, that would be simply awesome…

    Anyway, nice article.

    • http://skorks.com/ Alan Skorkin

      Hi Paulo,

      My hands just never seemed to reach as far as blogging anything for a long time, hopefully I can be a bit more regular about it now. Glad you liked the article.

  • http://twitter.com/samstrasser Sam Strasser

    Have you seen http://stedolan.github.io/jq/ ? I’ve been using it a lot for beautifying REST responses and I love it. You can filter the json really easily

  • Pingback: [tekwalk] console and json | ABK's Tech Memorandum

  • Pingback: json, csv, xml data processing | pythonrocks