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 to use JQ commands to manipulate and process JSON data in the shell.
Use the following command to install jq
in Centos8:
[root@localhost ~]# dnf -y install jq
Now we can start using the JQ command since 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 a number of different ways; it can be used directly on JSON files or combined with several other commands to interpret JSON data. JQ commands can be used with different filters such as ".", "|", "," or ".[]" filters to organize JSON data.
The JQ command also takes different options as arguments, such as –tab, –stream, –indent n, –unbuffered and the -L directory option. The syntax of JQ commands may seem complicated at first, but you'll become familiar with it after reading the entire article.
The simplest and most commonly used feature of JQ command filter. They are used to organize and beautify 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 standard output:
{"workers":{"name": "John Brooks","id": "003"}}
We can use the cat command to display data:
[root@localhost ~]# cat employee.json {"workers":{"name": "John Brooks","id": "003"}}
The data printed to standard output using the cat command is unorganized and confusing. We can use JQ commands and "." to organize this data, and use .
to filter:
[root@localhost ~]# jq '.' employee.json { "workers": { "name": "John Brooks", "id": "003" } }
Now the data is more organized, colorful and easier to understand. This filter is especially needed when accessing data from an API; the data stored in the API can be very unorganized and confusing.
.Fields
Filters and JQ commands can be used to access object properties in the shell.
If we only want to access a single property and print it to standard output, then we can use the .field
operator. For example, to access a worker's properties we can use the following command:
[root@localhost ~]# jq '.workers' employee.json { "name": "John Brooks", "id": "003" }
We can also access the items present in the attribute using the .field
operator. To access the name item in the worker attribute we will use:
[root@localhost ~]# jq '.workers.name' employee.json "John Brooks"
We can also access and output the elements present in the array in the JSON file using the .[]
operator. For this example, we will modify our JSON file and add the following:
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
Check the employee.json file:
[root@localhost ~]# cat employee.json [{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
要输出 JSON 文件中存在的所有数组,我们将运行以下命令:
[root@localhost ~]# jq '.[]' employee.json { "name": "John Brooks", "id": "003" } { "name": "Randy Park", "id": "053" } { "name": "Todd Gray", "id": "009" }
要仅输出第二个数组,我们可以通过以下方式修改上述命令:
[root@localhost ~]# jq '.[1]' employee.json { "name": "Randy Park", "id": "053" }
请记住,数组从索引 0 开始的。
我们还可以使用 .字段
运算符访问数组中存在的属性。例如,如果我们想访问第三个数组中的 name 属性,那么我们将运行以下命令:
[root@localhost ~]# jq '.[2].name' employee.json "Todd Gray"
类似地,要访问数组中的所有名称属性,我们可以执行以下命令:
[root@localhost ~]# jq '.[].name' employee.json "John Brooks" "Randy Park" "Todd Gray"
JQ 命令用于将 JSON 数据转换为更易读的格式并将其打印到 Linux 上的标准输出。JQ 命令是围绕过滤器构建的,过滤器用于从 JSON 文件中仅查找和打印所需的数据。
The above is the detailed content of JQ command usage examples in Linux. For more information, please follow other related articles on the PHP Chinese website!