Reading Json data with jq

Ashvini Praisoody
3 min readApr 29, 2021

JSON is a lightweight format that is nearly ubiquitous for data-exchange. jq is a command-line tool for parsing JSON.

We will demonstrate how to use the jq to parse JSON at the command line .

Jq is the JSON command line processor . Here I am going to show how to use jq to pass some json input and then store a particular property from the json into a variable . So this will work on any json data source .I have just pulled up the ICNDB database API . I simply just send a call to this URL http://api.icndb.com/jokes/random/3 in the command line .

We can actually send a request to the API using a curl command .So if I say curl and then pass the URL we will see the data comes but it is not very well formatted and that’s where we can use jq to actually parse this data and display it in a much more friendly way if I just send that request again .

I will just make sure to use -s silencer that does not display any of the loading informations and just pipe the result into the jq processor . Hit enter we can see the data comes back again but it has been nicely formatted and highlighted for us . So that’s probably the first advantage of using jq on the command line but we can also do more with it . we can actually access properties of the result . we are interested in is a property called value and it is an array of those three jokes that have come back.

So now we want to access the first joke for example . we send the request again but we pass what’s known as a filter into jq and the first filter which is kind of used by default . Hit enter we will see the some response again but we can see the value that comes back in a similar way that we would use this in JavaScript. we can access the individual properties of the json .

Here we don’t see that type property in there it’s just the content that we are in the value property . So we can just access part of that array as well . If we want to access the first joke obviously the zero indexed . Hit enter we will see the first joke come back .

Hope you got a basic idea of reading the json data using jq commands. Follow for more updates on jq.

--

--

Ashvini Praisoody
0 Followers

Undergraduate SLIIT | Intern Software Engineer WSO2