Using jq to pretty print JSON output

If you haven’t already discovered jq, you definitely need to take a look.  This nifty little tool is handy for manipulating JSON content at the command line and within scripts.  The first quick thing I think will be helpful is showing how to pipe raw JSON output to jq to pretty print it (aka show it in the nice nested view).

Once you’ve installed jq, you can run the raw command to get the help output:

03-jq-help

Here is some raw JSON output that we get from a basic cURL command:

01-curl-json

It’s not super easy to read when it is all packed on one line, so let’s pipe the output to the jq command and see the same results:

02-curl-json-jq

You can see the nice nested layout of the JSON output there.  This is a small example, so let’s take something a little larger.

UPDATED PRO TIP:  If you add the -s directive to the cURL command to get rid of the download output as per @shmick (https://twitter.com/shmick/status/777506873041756160)

I’ll use the William Lam Github example here for the VMworld fans.  William has posted JSON content for the VMworld session content from the US event at his Github page:

04-vmworld-top-sessions

Let’s click the Raw button on the page to render the real content URL which we will consume:

05-vmworld-sessions-raw

It’s not too readable in the browser, or the command line as you can see when we run the cURL command:

06-curl-raw

All we have to do to fix that up is to pipe the output from our cURL command to jq and we are able to see the pretty printed version of the JSON:

07-curl-pretty-print

There is much, much more to what you can do with the jq tool, but this was something that I thought was a good start.  Make sure to download it at the jq site, and it is already included in some platforms like CoreOS out of the box.

2 thoughts on “Using jq to pretty print JSON output”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.