Updated July 3, 2023
Definition of Linux JQ
JQ is defined as a command which has a utility similar to that of a filter. When said utility is similar to a filter, what we intend to say is that the command takes an argument as an input, extracts the data in accordance with the filter, and provides an output. Did we mention the specific type of data JQ caters to? Not yet! The command is very specific to the task of JSON files. In Linux, it is not straightforward to read a JSON file directly, like the way we do it for any other type of file, and as a result, a specific utility encompasses the JQ command!
Syntax of Linux JQ
In Linux, there are a wide variety of options available for JQ, and we will discuss all of those in detail here in this section. Later in the article, take a dig at the working of the JQ command in Linux. But before taking a dig, let us have a face-to-face with the look of syntax:
1. Read JSON data
echo "${variable name}" | jq '.'
Here one would replace the variable name with the name of the variable which houses the JSON data.
2. -c option (Reading or printing each JSON object in respective lines) of reading JSON data
echo "${variable name}" | jq -c '.[]'
3. JSON file read
jq '.' <json file name>
4. Read JSON file with ‘|’
cat <json file name> | jq '.'
5. Single key values read in JSON
jq '.[] | .<key name>' <json file name>
6. Multiple key values read in JSON
jq '.[] | .<key names separated by comma>' <json file name>
7. Removal of a key from JSON data
jq 'map(del(.<key name to be deleted>))' <json file name>
8. Values Mapping in JSON data
jq 'map(.<operation required>)' <json file name>
9. Values search by index and length in JSON data
jq '.[<start of index>:<end of index>]' <json file name>
How does JQ Command Work in Linux?
As we have already mentioned about JQ command that it specifically overlooks JSON data-specific files as these files are not easily readable like normal text files. In case you are familiar with sed and awk commands, you can treat jq command on the same lines, except for the fact that jq command will only work for JSON files. Also, one must ensure jq is installed in the Linux system as this utility generally doesn’t come tagged along with any Linux distribution.
To run the installation, one would need to run the install command in the console. The command is as follows:
sudo apt-get install jq
Once the utility command is in place for running it smoothly, we are ready to start experimenting with the commands in the command line interface. Now, another important thing is to learn what JSON is. To expand the abbreviation, JSON stands for JavaScript Object Notation. This format is often used as it is lightweight for storage and transferring purposes.
Now, it is time to explain the utilities of jq from many commands whose syntax is mentioned above. The first utility is like pretty-printing the variable, which stores the JSON, where we echo the variable and then use the command jq to print it in a human-readable format. With this, if we merge the -c option, we can make it much more readable by printing each object in different columns and not that of the original json file. The next is about reading a file that contains the extension of json. Just a simple syntax of jq and the file name will do the magic! Another variability is using the cat command, pipe, and jq command to keep the magic happening!
Now it is time for us to discuss utilities where the key-value pair of JSON comes into action. When one passes a single key name out of the various options available, it will try to filter out the values corresponding to the key and show up. While sending multiple keys, one can send the same by comma-separated values, and the corresponding values for the keys will show up! The del option of the command helps remove a key from JSON without altering any other portion of the JSON, and on the other hand, the map option helps modify the values of a key in the JSON. Last but not least, jq helps search through index and length in value searching by index. The start and end indexes are specified, and the filter occurs per the specified indices. Also, if one of the indices is not specified, the filter happens from the start (if the start is missing) and from the end (if the end is missing). There can be negative indices present, which also means that the command will start searching from the end of the json object rather than the top!
Examples of Linux JQ
Following are the examples are given below:
Example #1
Read JSON data
Code:
JsonDemoData='[{"userId": 1,"id": 1,"title": "delectus aut autem","completed": false}]'
echo "${JsonDemoData }" | jq '.'
Output:
Example #2
-c option of reading JSON data
Code:
JsonDemoData='[{"userId": 1,"id": 1,"title": "delectus aut autem","completed": false}]'
echo "${JsonDemoData}" | jq -c '.[]'
Output:
Example #3
JSON file read
Code:
jq '.' jsonData.json
Output:
Example #4
Read JSON file with ‘|’
Code:
cat jsonData.json | jq '.'
Output:
Example #5
Single key values read in JSON
Code:
jq '.[] | .email' jsonData.json
Output:
Example #6
Multiple key values read in JSON
Code:
jq '.[] | .email, .ip_address' jsonData.json
Output:
Example #7
Removal of a key from JSON data
Code:
jq 'map(del(.gender))' jsonData.json
Output:
Here we see that the key email id is deleted, and the json is then presented!
Example #8
Values Mapping in JSON data
Code:
Before the operation:
jq '.' demoNumber.json
After the operation of adding 10 to each element:
jq 'map(.+10)' demoNumber.json
Output:
Example #9
Values search by index and length in JSON data
Code:
Searching for elements from index 2 to index 4:
jq '.[2:4]' demoNumber.json
Searching for the last 3 elements in the json file:
jq '.[-3:]' demoNumber.json
Output:
In this example, if index numbers are positive, the command will index them in accordance with the index of the elements. On the other hand, if the index is negative, the command will assume to start the search from the last of the array. Hence -3 in this scenario means 3 elements from the last.
Conclusion
With the set of examples and explanation of the way of working for jq command, the utilities can be made in a bash script and utilized for varied purposes. What’s even more interesting is that these commands will enable to perform data analytics in its basic instinctive utility by just using the jq command, only left the readers to find and pave the way!
Recommended Articles
We hope that this EDUCBA information on “Linux JQ” was beneficial to you. You can view EDUCBA’s recommended articles for more information.