Examples of JQ commands in Linux

Time:2022-5-11

JSON is a data representation format used to store and transfer data between different layers of an application; It stores data in key: value pairs. In this article, we will learn how to use JQ commands to manipulate and process JSON data in the shell.
How to install JQ commands
In centos8, use the following command to install JQ:

[[email protected] ~]# dnf -y install jq
JQ command usage instance in Linux JQ command usage instance in Linux

grammar
Now we can start using the JQ command because 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…]
JQ commands can be used in many different ways; It can be used directly for JSON files, or it can be combined with several other commands to interpret JSON data. The JQ command can be used with different filters, such as “.” “|”, “,”, or “. []” filters to organize JSON data.

The JQ command also takes different options as parameters, such as — tab, — stream, — indent n, — unbuffered and – L directory options. The syntax of the JQ command may seem complex at first glance, but you’ll become familiar with it after reading the entire article.

How to use JQ command to organize JSON data
The JQ command filter is the simplest and most commonly used feature. They are used to organize and beautify JSON data as it is printed to standard output.

In this example, we have one named employee JSON file of JSON, we need to output the data to standard output:

{“workers”:{“name”: “John Brooks”,”id”: “003”}}
We can use cat command to display data:

[[email protected] ~]# cat employee.json
{“workers”:{“name”: “John Brooks”,”id”: “003”}}
JQ command usage instance in Linux JQ command usage instance in Linux
The data printed to standard output using cat command is unorganized and chaotic. We can use the JQ command and “.” To organize these data and use them To filter:

[[email protected] ~]# jq ‘.’ employee.json
{
“workers”: {

"name": "John Brooks",
"id": "003"

}
}
JQ command usage instance in Linux JQ command usage instance in Linux
Now, the data becomes more organized, colorful and easier to understand. This filter is especially needed when accessing data from the API; The data stored in the API can be very disorganized and confusing.

How to access properties using JQ commands
. field filters and JQ commands can be used to access object properties in the shell.

If we only want to access a single attribute and print it to standard output, we can use Field operator. For example, to access the properties of a worker, we can use the following command:

[[email protected] ~]# jq ‘.workers’ employee.json
{
“name”: “John Brooks”,
“id”: “003”
}
JQ command usage instance in Linux JQ command usage instance in Linux
We can also use it Field operators access items that exist in properties. To access the name item in the worker properties, we will use:

[[email protected] ~]# jq ‘.workers.name’ employee.json
“John Brooks”
JQ command usage instance in Linux JQ command usage instance in Linux

How to access array items using JQ command
We can also use it [] operator accesses and outputs the elements existing in the array in the JSON file. For this example, we will modify our JSON file and add the following content:

[{“name”: “John Brooks”,”id”: “003”},{“name”: “Randy Park”,”id”: “053”},{“name”: “Todd Gray”,”id”: “009”}]
Check out employee JSON file:
www.ccidnet.com/2016/0822/10173006.shtml
www.donews.com/news/detail/4/2970162.html
www.51cto.com/it/news/2020/0113/18299.html
www.donews.com/news/detail/4/2971720.html
[[email protected] ~]# cat employee.json
[{“name”: “John Brooks”,”id”: “003”},{“name”: “Randy Park”,”id”: “053”},{“name”: “Todd Gray”,”id”: “009”}]
JQ command usage instance in Linux JQ command usage instance in Linux
To output all arrays that exist in the JSON file, we will run the following command:

[[email protected] ~]# jq ‘.[]’ employee.json
{
“name”: “John Brooks”,
“id”: “003”
}
{
“name”: “Randy Park”,
“id”: “053”
}
{
“name”: “Todd Gray”,
“id”: “009”
}
JQ command usage instance in Linux JQ command usage instance in Linux
To output only the second array, we can modify the above command in the following way:

[[email protected] ~]# jq ‘.[1]’ employee.json
{
“name”: “Randy Park”,
“id”: “053”
}
JQ command usage instance in Linux JQ command usage instance in Linux
Remember that arrays start at index 0.

We can also use it Field operators access properties that exist in the array. For example, if we want to access the name attribute in the third array, we will run the following command:

[[email protected] ~]# jq ‘.[2].name’ employee.json
“Todd Gray”
JQ command usage instance in Linux JQ command usage instance in Linux
Similarly, to access all the name attributes in the array, we can execute the following command:

[[email protected] ~]# jq ‘.[].name’ employee.json
“John Brooks”
“Randy Park”
“Todd Gray”
JQ command usage instance in Linux JQ command usage instance in Linux

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

Recommended Today

Vue3.0 + vite2.0 + ts4.0 build a simple version of mobile blog

vue3-vite2-blog-h5 A simple version of mobile blog. Front end projects mainly adoptVue3Latest grammar sugar<script setup>andVant3.0To build; useTsxTo render common components; useVite2.0To build and package. Back end projects mainly adoptNodeframeKoa2as well asMongoDBDatabase. PC blog online preview address:http://www.rasblog.com PC blog warehouse address:https://github.com/Sujb-sus/vue-node-mongodb-blog H5 blog warehouse address:https://github.com/Sujb-sus/vue3-vite2-ts-blog-h5 Project structure Technology application 1、 REM adaptation Install plug-insyarn add amfe-flexible […]