Useful Programs That I Used: jq command-line JSON Processor

Useful Programs That I Used: jq command-line JSON Processor

Hi everyone,

In this blogpost, I would like to give you some information about jq. As a developer, you work many time with JSON data. These types of data can sometimes be huge and complicated, therefore you can not find easily what you search in your complex JSON output. In these complicated times, you only want to see the data you need. At these times, jq is a good practice for you.

What is jq?

jq is a lightweight and flexible command-line JSON processor. It is an open-source project on github and it is written by C language. There are more than 19K star in the project, and more than 1K fork it is. It's available for all platforms like Linux, OS X and Windows and many more. You can visit jqwebsite for more details about jq. Usage of this tool is easy to learn, If you would like to do more practice you can visit jqplay website. This website contains good examples of how to use it.

Samples

I will use stedolan's jq github profile like below. The <request> will be like below data.

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=2'

Output of above code is enough complicated. So we will manipulate this request for our needed.

If you run above code in the terminal, you will see the not formatted JSON data.

Screen Shot 2021-04-25 at 16.09.41.png

Formatting

Pipe

curl <request> | jq
curl <request> | jq .

Screen Shot 2021-04-25 at 16.38.06.png

Get specific array item from JSON data.

curl <request> | jq '.[0]'

Above request give you an item that index is 0 from JSON list.

Get specific key values from JSON data

curl <request> jq '.[].node_id'

Screen Shot 2021-04-25 at 16.51.35.png

Restructure your output

You can also create your output JSON data like below. I will try to get just commiter name, commit message and commit author email. So you can just type like below to create new output.

curl <request> | jq  '[.[] | {message: .commit.message, name: .commit.committer.name, email:.commit.author.email}]'

Screen Shot 2021-04-25 at 16.58.07.png

Select specific condition

curl <request> | jq '.[] | select(.commit.message=="Fix #2197 extended regex pattern example") |  {message: .commit.message, name: .commit.committer.name, email:.commit.author.email} '

I searched a specific message in JSON and get some data for my new output JSON.

Screen Shot 2021-04-25 at 17.08.06.png

Conclusion

I tried to give you a little information on how to use jq. I always use this tool when I work with JSON data. I believe that this tool will help you more when you begin to use. Apart from what I have shown above, this program has more features. Actually, you need to examine the official website for more information.

I hope you enjoy while reading.

Have a nice coding.