ma7555
ma7555

Reputation: 400

Filter an array inside JSON w/ jq while retaining surrounding structure

JSON data as below, I want to discard when "bar"="cccc" without altering the structure.

{
      "foofoo": {
        "barbar": [
          {
            "foo": "0000",
            "bar": "aaaa"
          },
          {
            "foo": "1111",
            "bar": "bbbb"
          },
          {
            "foo": "2222",
            "bar": "cccc"
          }
          ]
      }
}

something like this does the trick but it alters the JSON structure.

.foofoo.barbar[] | select(.bar !="cccc")

results in:

{
  "foo": "0000",
  "bar": "aaaa"
}
{
  "foo": "1111",
  "bar": "bbbb"
}

needed result is:

{
  "foofoo": {
    "barbar": [
    {
      "foo": "0000",
      "bar": "aaaa"
    }
    {
      "foo": "1111",
      "bar": "bbbb"
    }
    ]
  }
}

Thanks!

Upvotes: 0

Views: 69

Answers (1)

peak
peak

Reputation: 116740

The problem with your attempt is that you've expanded .foofoo.barbar by writing .foofoo.barbar[].

What you want instead is to reset it:

.foofoo.barbar |= map(select(.bar !="cccc"))

Upvotes: 2

Related Questions