Home > System Tutorial > LINUX > body text

JQ command usage examples in Linux

PHPz
Release: 2024-02-10 21:12:12
forward
486 people have browsed it

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.

How to install JQ command

Use the following command to install jq in Centos8:

[root@localhost ~]# dnf -y install jq
Copy after login
Linux 中的 JQ 命令使用实例

grammar

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...]
Copy after login

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.

How to use JQ commands to organize JSON data

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"}}
Copy after login

We can use the cat command to display data:

[root@localhost ~]# cat employee.json 
{"workers":{"name": "John Brooks","id": "003"}}
Copy after login
Linux 中的 JQ 命令使用实例

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"
  }
}
Copy after login
Linux 中的 JQ 命令使用实例

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.

How to use JQ commands to access properties

.FieldsFilters 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"
}
Copy after login
Linux 中的 JQ 命令使用实例

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"
Copy after login
Linux 中的 JQ 命令使用实例

How to use JQ commands to access array items

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"}]
Copy after login

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"}]
Copy after login
Linux 中的 JQ 命令使用实例

要输出 JSON 文件中存在的所有数组,我们将运行以下命令:

[root@localhost ~]# jq '.[]' employee.json 
{
  "name": "John Brooks",
  "id": "003"
}
{
  "name": "Randy Park",
  "id": "053"
}
{
  "name": "Todd Gray",
  "id": "009"
}
Copy after login
Linux 中的 JQ 命令使用实例

要仅输出第二个数组,我们可以通过以下方式修改上述命令:

[root@localhost ~]# jq '.[1]' employee.json 
{
  "name": "Randy Park",
  "id": "053"
}
Copy after login
Linux 中的 JQ 命令使用实例

请记住,数组从索引 0 开始的。

我们还可以使用 .字段 运算符访问数组中存在的属性。例如,如果我们想访问第三个数组中的 name 属性,那么我们将运行以下命令:

[root@localhost ~]# jq '.[2].name' employee.json 
"Todd Gray"
Copy after login
Linux 中的 JQ 命令使用实例

类似地,要访问数组中的所有名称属性,我们可以执行以下命令:

[root@localhost ~]# jq '.[].name' employee.json 
"John Brooks"
"Randy Park"
"Todd Gray"
Copy after login
Linux 中的 JQ 命令使用实例

总 结

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!