Quickly Tidy Up JSON from the Command Line (and vim)

Posted by Grego on February 2, 2015

I haven’t posted much in a while; I’ve been investigating how to compile Swift from the command line, and getting closer every day.

Today’s trick is how to neaten up your JSON quickly. This is useful when troubleshooting web services.

Most people probably resort to some online web site or service that will allow you to paste in your JSON, and get back lovely pretty JSON. That’s great, but this is faster and doesn’t require an internet connection for those odd moments that you have no internet.

Method 1 - Using Python

As of Python 2.6+ you should have access to the json.tool module, you can run it via:

python -m json.tool

It reads from stdin so we have a few choices. Using the paste board:

pbpaste | python -m json.tool #pasting from OS X paste board.

(Should also work with xclip on linux, but I don’t recall the exact options and functionality offhand)

And in vim, paste your untidy JSON and create a selection, then use

:!python -m json.tool

You can also map a key to it, but I haven’t found one yet that is particularly useful.

Method 2 - jq

When Python or the json.tool module is unavailable, install jq:

brew install jq

Keeping in mind that jq is much more than a pretty printer for JSON, we have to pass some filters to it in order to do anything. Check man jq for more details. Here we will use jq’s simplest filter, .:

       The absolute simplest (and least interesting) filter is .. This is a filter that takes its input and produces it unchanged as output.

       Since jq by default pretty-prints all output, this trivial program can be a useful way of formatting JSON output from, say, curl.

           jq '.'
              "Hello, world!"
           => "Hello, world!"

And here’s some JSON I grabbed randomly from the internet to test it on:

{ "order_num" : "O2012019231a", "order_date" : "2012-06-27", "order_id" : 21934, "order_item" : [ { "product_id" : 20933, "quantity" : 3, "price" : 36000, "product_name" : "Thingamagic 2000", "unit_price" : 12000 }, { "product_id" : 10366, "quantity" : 1, "price" : 100, "product_name" : "Super Duper Blooper", "unit_price" : 100 } ] }

So go copy that then try this in your terminal:

pbpaste | jq .

And it will output beautifully colored JSON:

  "order_num": "O2012019231a",
  "order_date": "2012-06-27",
  "order_id": 21934,
  "order_item": [
      "product_id": 20933,
      "quantity": 3,
      "price": 36000,
      "product_name": "Thingamagic 2000",
      "unit_price": 12000
      "product_id": 10366,
      "quantity": 1,
      "price": 100,
      "product_name": "Super Duper Blooper",
      "unit_price": 100

And of course, through vim, make your selection and run jq on it via:

:!jq .

Pretty JSON Colors in vim

Hey wouldn’t it be nice to have pretty colors in vim, too? Give vim-json a shot:

Plugin 'elzr/vim-json'

Final Result:

Using jq and vim-json:

vim json demo