使用jq根据JSON中的值进行分组可以通过使用group_by
函数来实现。group_by
函数接受一个表达式作为参数,该表达式用于从JSON对象中提取要分组的值。以下是使用jq进行分组的步骤:
[
{ "name": "Alice", "age": 25, "city": "New York" },
{ "name": "Bob", "age": 30, "city": "London" },
{ "name": "Charlie", "age": 35, "city": "New York" },
{ "name": "Dave", "age": 40, "city": "London" }
]
jq 'group_by(.city)' data.json
[
[
{ "name": "Alice", "age": 25, "city": "New York" },
{ "name": "Charlie", "age": 35, "city": "New York" }
],
[
{ "name": "Bob", "age": 30, "city": "London" },
{ "name": "Dave", "age": 40, "city": "London" }
]
]
这样,我们就根据"city"字段成功地将JSON数据分组了。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云