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:
Here is some raw JSON output that we get from a basic cURL command:
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:
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:
Let’s click the Raw button on the page to render the real content URL which we will consume:
It’s not too readable in the browser, or the command line as you can see when we run the cURL command:
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:
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.