Garrett Boatman
Garrett Boatman

Reputation: 117

Looking for a jq filter to exclude nested objects based on the value of a nested property

Given this input:

{
  "10000703": {
    "show_id": 1641788,
  },
  "10000838": {
    "show_id": 1517903,
  },
  "10001325": {
    "show_id": 1641788,
  },
}

I'm looking for a filter to say "return all objects where show_id does not equal 1641788"

The expected output would be:

{
  "10000838": {
    "show_id": 1517903,
  },
}

Haven't been able to exclude nested objects :(

Upvotes: 4

Views: 2925

Answers (1)

peak
peak

Reputation: 116780

This is a good example of the convenience of with_entries/1 and of the brevity that's possible with jq:

with_entries( select(.value.show_id != 1641788 ))

with_entries/1 converts an object into an explicit .key/.value representation. Please see the jq manual for details.

Alternatively and with even greater brevity, one can in this case also use del/1:

del( .[] | select( .show_id == 1641788 ) )

Upvotes: 6

Related Questions