Home > Mobile >  Manipulate json, remove two items in a group by key value
Manipulate json, remove two items in a group by key value

Time:10-20

How can I manipulate this chunk of json:

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

So that it matches on "that" and removes the key and value both in that grouping, leaving json like this:

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

I am attempting to use jq on linux.

CodePudding user response:

Try this

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

Demo

CodePudding user response:

Figured it out.

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

  • Related