jq tool and its common usage | JD Logistics Technology Team

Recently, I have been dealing with JSON a lot at work, and I have done an in-depth study of jq. The previous use of jq has always been at the level of JSON data formatting. In fact, its capabilities are far more than that.
When processing JSON data, we often need to perform filtering, querying and editing operations on the command line. jq is a powerful command-line JSON processing tool, which allows us to easily perform various operations on JSON data. This article will briefly introduce the basic concepts and common functions of jq, and provide some practical examples.

1. Introduction to jq

jq is a lightweight and flexible command-line JSON processor. It allows you to extract, filter and modify JSON data based on keys, values ​​and array indexes. jq mainly has the following features:

  1. Based on streaming processing, suitable for large JSON data files.

  2. Provides a rich set of operators and functions, compatible with Unix pipes and I/O redirection.

  3. Supports advanced functions such as conditional filtering, string operations, mathematical operations, and custom functions.

2. Install jq

On most Linux distributions, jq can be easily installed via the package manager:

  • On Debian-based distributions:

    sudo apt-get install jq
    
    
  • On RHEL based distributions:

    sudo yum install jq
    
    
  • On Arch Linux:

    sudo pacman -S jq
    
    
  • On macOS, it can be installed using Homebrew:

    brew install jq
    
    

3. Common usage of jq

Here are some common uses of jq.

1. Read attribute values

To extract property values ​​from the input JSON object, you can use .operators.

Example: There is a sample.jsonfile named as follows:

{
  "name": "Alice",
  "age": 30,
  "city": "New York"
}

Extract names:

cat sample.json | jq '.name'

Output result:

"Alice"

2. Filter array elements

To filter array elements, you can use []operators.

Example: There is a students.jsonfile named as follows:

[
  {
    "name": "Alice",
    "age": 30,
    "city": "New York"
  },
  {
    "name": "Bob",
    "age": 25,
    "city": "San Francisco"
  },
  {
    "name": "Charlie",
    "age": 23,
    "city": "Los Angeles"
  }
]

Extract all names:

cat students.json | jq '.[].name'

Output result:

"Alice"
"Bob"
"Charlie"

3. Select specific array elements

If you want to return specific elements with certain attributes, you can use selectors. For example, to select students over 25 years old:

cat students.json | jq '.[] | select(.age > 25)'

Output result:

{
  "name": "Alice",
  "age": 30,
  "city": "New York"
}

4. Sorting and uniqueizing arrays

You can use sort, sort_by()and uniquefunctions to operate on arrays.

For example, sample-array.jsonsort and deduplicate ages from a file:

[30, 20, 25, 30, 25, 20]

Sort and remove duplicates:

cat sample-array.json | jq 'unique | sort'

Output result:

[
  20,
  25,
  30
]

5. Modify data

In addition to filtering and querying, jq also supports creating and modifying data. For example, students.jsonadd the "isActive" attribute to each student in:

cat students.json | jq '.[] | {name, age, city, isActive: true}'

Output result:

{
  "name": "Alice",
  "age": 30,
  "city": "New York",
  "isActive": true
}
{
  "name": "Bob",
  "age": 25,
  "city": "San Francisco",
  "isActive": true
}
{
  "name": "Charlie",
  "age": 23,
  "city": "Los Angeles",
  "isActive": true
}

4. Summary

jq is a very powerful command-line JSON processing tool that supports various data manipulation functions and can help you process JSON data more conveniently and efficiently. This article lists some common jq use cases, but jq's functions are much more than that. More rich features can be found in the official documentation ( click to view ). Proficiency in jq will help improve the command line's ability to process JSON data and provide a more convenient data source for other processing and analysis tools.

Author: JD Logistics Zhang Tao

Source: JD Cloud Developer Community Ziyuanqishuo Tech Please indicate the source when reprinting

Qt 6.6 is officially released. The pop-up window on the lottery page of Gome App insults its founder . Ubuntu 23.10 is officially released. You might as well take advantage of Friday to upgrade! RISC-V: not controlled by any single company or country. Ubuntu 23.10 release episode: ISO image was urgently "recalled" due to containing hate speech. Russian companies produce computers and servers based on Loongson processors. ChromeOS is a Linux distribution using Google Desktop Environment 23-year - old PhD student fixes 22-year-old "ghost bug" in Firefox TiDB 7.4 released: officially compatible with MySQL 8.0 Microsoft launches Windows Terminal Canary version
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10117969