JSON is a data representation format that is used to store and transfer data between different layers of an application; it stores data in key: value pairs.

The syntax of JSON was derived from JavaScript but it itself is language independent. It is compatible with many programming languages; these languages include code that can be used to integrate JSON into the program; but unfortunately, we cannot work with JSON directly in Linux shell as it cannot interpret it. To work with JSON in the Linux shell we use a mixture of tools such as JQ and sed.

In this post, we will learn to use the JQ command to manipulate and work with JSON data in a Linux shell.

How to Install the JQ command

The JQ command is not available in some Linux distributions by default; It needs to be downloaded into the system before it can be used on the terminal; You can download the JQ command just like any other package on your system. On Ubuntu 20.04 use the below-given command to install the JQ utility:

sudo apt install jq 

Just replace apt with the package manager of your system if you are running a distribution other than Ubuntu.

If you are running a distribution like CentOS 8 which already has JQ by default then you will get an output similar to this:

sudo dnf install jq 

Syntax

Now we can start using the JQ command as it has been successfully installed on our system, but first, let’s take a look at the syntax of the JQ command:

jq [options]  [file...]

jq [options] --args  [strings...]

jq [options] --jsonargs  [JSON_TEXTS...]

The JQ command can be used in many different ways; It can be used directly on a JSON file and can also be combined with several other commands to interpret JSON data. The JQ command can be used with different filters such as the “.”, “|”, “,” or the “.[]” filter to organize JSON data.

The JQ command also takes different options as arguments such as the --tab, --stream, --indent n, --unbuffered, and the -L directory option. The syntax of the JQ command might seem complex at first but you will get familiar with it once you read the whole article.

How to Organize JSON data using JQ command

The simplest and frequently used feature of the JQ command filters. They are used to organize and prettify JSON data when printing it to standard output.

In this example, we have a JSON file named employee.json and we need to output the data to the standard output:

{"workers":{"name": "John Brooks","id": "003"}}

We can use the cat command to show the data:

cat employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

The data printed to the standard output using the cat command is unorganized and messy. We can organize this data by using the JQ command along with the ‘.’ filter:

jq '.' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

Now the data has become a lot more organized, colorful, and easier to understand. This filter is especially needed when accessing data from APIs; The data stored in APIs can be very unorganized and confusing.

How to Access a Property using JQ command

The .field filter along with the JQ command can be used to access object property in the shell.

If we only want to access and print a single property to the standard output then we can use the .field operator. E.g to access the worker’s property we can use this command:

jq '.workers' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

We can also access the items present within the property by using the .field operator. To access the name item in the worker’s property we will use:

jq '.workers.name' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

How to Access an Array Item using JQ command

We can also access and output the elements present within an array in a JSON file by using the .[] operator. For this example we are going to modify our JSON file so it looks like this:

[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]

To output all the arrays present in the JSON file we will run the command given below:

jq '.[]' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

To output only the second array we can modify the above-given command in the following way:

jq '.[1]' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

Remember that the array starts at 0

We can also access the properties present within the array by using the .field operator. E.g if we want to access the name property in the third array then we will run the following command:

jq '.[2].name' employee.json

JQ Command in Linux with Examples command jq linux Linux Commands

Similarly, to access all the name properties inside arrays we can execute this command:

jq '.[].name' employee.json 

JQ Command in Linux with Examples command jq linux Linux Commands

Conclusion

The JQ command is used to transform JSON data into a more readable format and print it to the standard output on Linux. The JQ command is built around filters which are used to find and print only the required data from a JSON file.

In this how-to guide, we have learned to use the JQ command to organize and filter JSON data.