Guide to Linux jq Command for JSON Processing


Introduction

JSON (JavaScript Object Notation) is a popular data format used for exchanging information between applications. It is a lightweight and easy-to-understand. To process JSON data efficiently, Linux provides a command-line tool called jq. This powerful tool enables users to extract, manipulate, and transform JSON data with ease. With jq, users can quickly perform a wide range of tasks on JSON data, including filtering, sorting, and selecting specific fields. Additionally, jq supports a variety of output formats, making it easy to integrate with other command-line tools. In this article, we will explore the jq command and its various features and functionalities for JSON processing.

Installation Process of jq

Before we can start using jq, we need to make sure it is installed on our system. jq can be installed using the linux package manager. For example, to install jq on Ubuntu, we can use the following command −

$ sudo apt-get install jq

After completing any of these installation methods, you can verify the installation by running the following command −

$ jq --version

If jq is installed correctly, this command should display the version number of jq that is installed on your system.

Starting with the basic Command

Example

$ echo '{"fruit":{"name":"mango","color":"yellow","price":4.20}}' | jq '.'

The jq command with the "." filter is being used to pretty-print the JSON object in the console output. The "." filter simply selects the entire input object and returns it.

Output

{
   "fruit": {
      "name": "mango",
      "color": "yellow",
      "price": 4.2
   }
}

Installing and Using curl Command

Example

$ sudo apt install curl

After installing you can run this command in your terminal −

$ curl http://api.open-notify.org/iss-now.json | jq '.'

This command uses the curl command to make an HTTP GET request to the URL http://api.open-notify.org/iss-now.json, which returns a JSON object with the current latitude and longitude coordinates of the International Space Station (ISS).

Output

% Total    % Received % Xferd   Average Speed    Time    Time     Time      Current
                                 Dload  Upload  Total   Spent     Left       Speed
100   112  100   112    0     0   159      0    --:--:-- --:--:-- --:--:--     159
{
   "message": "success",
   "iss_position": {
      "latitude": "19.4146",
      "longitude": "-3.3218"
   },
   "timestamp": 1678259912
}

Creating a JSON File

Example

First entering into Desktop

$ cd Desktop/

Creating a file in your Desktop file name is fruit.json

$ touch fruit.json

Open the file in your Desktop and enter the below code.

{
   "fruit": {
      "name": "mango",
      "color": "yellow",
      "price": 4.2
   }
}

After entering this code save the code and come back to the terminal and run the below command −

$ cat fruit.json

Output

{
   "fruit": {
      "name": "mango",
      "color": "yellow",
      "price": 4.2
   }
}

Different Approaches to Access Properties of a JSON File

Example

Now we try to chain property values together in order to access nested objects, here is an example −

$ jq '.fruit.name' fruit.json
"mango"

we retrieve multiple keys from a JSON object using a comma to separate them, here is an example −

$ jq '.fruit.color,.fruit.price' fruit.json
"yellow"
4.2

Printing Each Element in an Array

This is the basic format of an array.

$ echo '["Aditya","Adil","Arindom"]' | jq '.[]'

Output

"Aditya"
"Adil"
"Arindom"

Creating a New JSON File in Your Desktop

Creating file in your Desktop file name is fruits.json

$ touch fruits.json

Now open the new file in your desktop and copy paste the below code.

[
   {
      "name": "Mango",
      "color": "yellow",
      "price": 4.2
   },
   {
      "name": "Grave",
      "color": "green",
      "price": 2.5
   },
   {
      "name": "Blueberry",
      "color": "blue",
      "price": 1.25
   }
]

After that save the file and come back to your terminal again and run this code.

Printing all the Fruits Name from Each Object in the Array

Example

$ jq '.[].name' fruits.json

The [] operator is used to iterate over each element of the array, and the | operator is used to chain multiple filters together. The first filter, .[], selects each element of the array, and the second filter, .name, selects the value of the "name" key from each selected element.

Output

"Mango"
"Grave"
"Blueberry"

Accessing Price by Index

Example

$ jq '.[1].price' fruits.json

Array index start from 0 here Mango is the 0th index, Grave is the 1st index and Blueberry is the 2nd index.

In this command we try to print the 1st index which is the price of the Grave.

Output

2.5

Slicing of an Array Using jq Command

Example

Slicing in jq refers to selecting a subset of elements from an array or object based on their index or key.

$ echo '[10,12,14,16,18,20,22,24,26,28,30]' | jq '.[4:8]'

Output

[
   18,
   20,
   22,
   24
]

In the example you provided, the slice . [4:8] selects the elements with indices 4,5,6 and 7, but excludes the element with index 8.

Finding Max Min Values From an Array

In case we require to determine the smallest or largest element of an input array, we can make use of the min and max functions

Example

Finding minimum value,

$ jq '[.[].price] | min' fruits.json

Output

1.25

Using the Select Function

The select function is an additional useful tool that we can utilize for JSON querying.

Example

$ jq '.[] | select(.color=="yellow" and .price>=4.2)' fruits.json

Output

{
   "name": "Mango",
   "color": "yellow",
   "price": 4.2
}

Deleting Keys From JSON

On occasion, we may need to eliminate a key-value pair from JSON objects.

Example

$ jq 'del(.fruit.color)' fruit.json

Output

{
   "fruit": {
      "name": "mango",
      "price": 4.2
   }
}

Conclusion

The Linux jq command is an essential tool for processing JSON data on the command line. In this article, we provided a guide to the jq command for JSON processing, including how to install it, how to use it for basic operations. We also provided examples of how to use the jq command to process JSON data, along with code and output.

Updated on: 29-Mar-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements