Master Parsing & Generating JSON in CLI

Antony Amirtha Raj
Francium Tech
Published in
3 min readJan 29, 2019

--

Photo by Kevin Noble on Unsplash

We often work with several JSONs multiple times a day. Many times, they are not even formatted and they are a pain to work with.

Recently I happened to work with a huge and unformatted JSON. I am generally used to formatting it using websites like jsonviewer.stack.hu. Since it was a large JSON, the website didn’t respond for a while. Then I started looking for a tool in Linux that can help me better to parse any large file.

Then I landed at using jq. It is like sed for JSON data. One can easily use it along with grep, awk etc. Only then we could feel native to Linux.

Let’s explore it with an example. Let’s take a publicly available JSON API. It looks a bit like this.

Let’s print the basic of jq. Let’s start with the basic command.

We need to note a couple of things here. I am using the command http. This command stems from HTTPie. It is a better version of curl or wget. It is written in Python. It does the pretty print and colorizes the output too. It also prints HTTP headers while only using HTTP command. Now here I am piping the response to jq. Here the dot represents printing the json as is.

Now let us navigate and print results array from the JSON. Let us see how easy it is now.

I think it cannot get any better than this. Now, let’s print just the title from this JSON.

Similarly, we can navigate through the depths. Now, let’s see how we can construct the JSON from jq per se. Let’s construct JSON with just title and ingredients.

You may notice the surrounding square brackets that ensure all of the spit JSON structure shows up in Array. It pretty prints the generated JSON too. It still needs a key missing at the top level. Let’s add one now.

Thus we have added recipes key to our generated JSON at the top.

Hope you have enjoyed reading this article and learnt jq in an interesting manner. What a joy to use jq.

Francium Tech is a technology company laser focussed on delivering top quality software of scale at extreme speeds. Numbers and Size of the data don’t scare us. If you have any requirements or want a free health check of your systems or architecture, feel free to shoot an email to contact@francium.tech, we will get in touch with you!

--

--