jq to the rescue

JQ is a really good tool when you have lot of JSON and you just interested in some of that data.

What if I just want to count number of entries in a list

Imagine I have this

And now you want to know how many entries there are in that array/list

jq query is so simple

. | length

(dot pipe length)

and the result is simply the number of elements e.g. 5

Only see the first N entries in an array (list)

.[:3]

The above would result in returning the first 3 entries in the array (list)

Only see some attributes

Imagine you have a JSON like this, and you only want to see the “name” attribute

(extract specific attributes from a json array with objects, from array to array)

the JQ query would then be something like this

[.[] | { “email” : .email } ]

and that would give you the following output

You can use jqpplay, JQ Kung Fu and other online alternatives to try it out, and ofcourse the command line ‘jq’ command.

Just a list of strings for one “column” / attribute

If you want to just create an array of strings, then you try this jq query

[.[] | .email ]

Which would give you the following

JSON to CSV

You will the following output with lots of quotes

That is not so nice, so to avoid the quotes then use the “Raw Output” function
then the output will look like this

Much better 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *