通过jq删除对象数组中的键

问题描述 投票:2回答:1

如果我想在第一个输出中从JSON中删除age。有没有办法一步完成,换句话说,没有调用jq 2次?

➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤ echo '[{"id": 1, "name": "Arthur", "age": "21"},{"id": 2, "name": "Richard", "age": "32"}]' | jq .
[
  {
    "id": 1,
    "name": "Arthur",
    "age": "21"
  },
  {
    "id": 2,
    "name": "Richard",
    "age": "32"
  }
]

>>>  0s elasped...
➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤ echo '[{"id": 1, "name": "Arthur", "age": "21"},{"id": 2, "name": "Richard", "age": "32"}]' | jq '.[] | del(.age)' | jq -s
[
  {
    "id": 1,
    "name": "Arthur"
  },
  {
    "id": 2,
    "name": "Richard"
  }
]

>>>  1s elasped...
➜ $?=0 @arastogi-ld2.linkedin.biz inGraphs/common-templates [ 1:39AM] ➤
json key jq
1个回答
6
投票

使用jq调用时使用map()非常简单。

jq 'map(del(.age))' < json

map()用于给定的过滤器del(.age)将为输入数组的每个元素运行它,并以新数组的形式返回输出。

© www.soinside.com 2019 - 2024. All rights reserved.