jq group_by
时间: 2023-08-27 17:07:08 浏览: 48
`jq` is a powerful command-line tool for processing JSON data. One of its most useful features is the ability to group data by one or more fields using the `group_by` filter.
The `group_by` filter takes as input an array of JSON objects and a grouping expression, and outputs an array of groups, where each group is represented by an object with two fields: `key` and `values`.
The `key` field contains the value of the grouping expression for the group, while the `values` field contains an array of all the objects in the input array that belong to that group.
Here is an example of using `group_by` to group a list of people by their age:
```
$ cat people.json
[
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 25},
{"name": "Dave", "age": 30},
{"name": "Eve", "age": 25}
]
$ jq 'group_by(.age)' people.json
[
[
{"name": "Alice", "age": 25},
{"name": "Charlie", "age": 25},
{"name": "Eve", "age": 25}
],
[
{"name": "Bob", "age": 30},
{"name": "Dave", "age": 30}
]
]
```
In this example, we group the people by their age using the `.age` expression. The output is an array of two groups, one for age 25 and one for age 30. Each group contains an array of objects with the corresponding age value.