【问题标题】:Manipulate json, remove two items in a group by key value操作json,通过key值移除一组中的两项
【发布时间】:2021-10-20 01:36:03
【问题描述】:

我该如何操作这块 json:

{
"id": "whatever",
"attributes": [
    {
      "key": "this",
      "value": "A"
    },
    {
      "key": "that",
      "value": "B"
    },
    {
      "key": "other",
      "value": "C"
    }
  ]
}

这样它就可以匹配“那个”并删除该分组中的键和值,让 json 像这样:

{
"id": "whatever",
"attributes": [
    {
      "key": "this",
      "value": "A"
    },
    {
      "key": "other",
      "value": "C"
    }
  ]
}

我正在尝试在 linux 上使用 jq。

【问题讨论】:

    标签: json jq


    【解决方案1】:

    试试这个

    .attributes |= map(select(.key != "that"))
    

    Demo

    【讨论】:

    • 这也行,谢谢。
    【解决方案2】:

    想通了。

    jq 'del(.attributes[] | select(.key == "that"))' test.json | sponge test.json
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多