grep is the standard unix tool to filter text files and will remain the standard tool because of its speed and many examples available online, but grep its not well suited to parse JSON because it doesnt do any filtering between key and values of JSON. But there is a nice solution to parse and filter JSON, its called jq.

Example: How to read JSON input

jq can be used to read JSON output from another process like curl or provide a file name to parse

Example: reading input from another process and from a file

Example: print the full JSON pretty printed.

The “.” expression means print all key-values of JSON.

Example: key print all key-values

Example: Print a specific value of a key

To print a specific key-value pair is easy, we just need to pass the name of the key, so if we want only the key-value pair of key “type” we can enter the following

Example: print specific value of key

Example: Print values in nested JSON structs

If we want to print each value of key id we can use the following syntax

Example: print values in nested JSON structs

Example: print only keys

There might be cases that we need only the keys of a JSON, to do this we can use the “keys” command

Piping

Did you nottice the | character? its used to pipe data from one processing step to the next, like we do in *nix shells

Example: print only values

If we want to print only the values we need to do a small change to our syntax, nottice the “.value[]” parameter

Example: filtering JSON output with conditionals

We have the following JSON file and we need to print only the entries with value.id = 497

In this case we can use the “select” statement

Example: use the select statement

Using select we filtered JSON structs of the file that the value.id is equal to 497.

Also there are the standard logical operators like and, or not we can use to do more complex queries.

Example: select with logical operators

Very often we wont need all values, but only some of them, to select specific values we can do the following after the select expression

multiple return values can be defined separated by comma

Creating custom ouput

If we want to create a more human friendly output we can avoid bash or any other scripted solutions and instead use jq to create the output by doing simple string manipulation, it even supports doing math.

Creating JSON output

If we want to create JSON output we can use the following syntax which is very straight forward.

Regexes

jq can pefrom regex matching which is a powerfull tool. We have the following file

If we want to get any JSON elements of the array that the name Starts wirth “K” and ends to “s” we can use the test function which accepts regular expressions

If we want to negate the results in order to get only JSON elements of the array which the name does not start with “K” and does not end to “s” we can pipe the results of test function to to the input of not function

JSON Arrays

This file has an array of JSON elements

To get the number of elements we can use the length function

Array starts from zero

And we can get the last element of the array using -1 as index

To get the indexes as an array we cn use the keys function

And finnally to iterate each index and print a value of the array based on the index value we can do this

Schema validation

The “type” keyword can help us validate the schema of JSON, in the bellow example jq will print only JSON structs that the .value.id property is a number and not a string

I hope you find the article easy to read and help you understand jq!

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store