Reputation: 3
I have this JSON object that I want to extract with Jq:
{
"key_1": "uo2",
"key_2": false,
"measurements": [
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_P"
},
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_3"
},
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_001"
}
],
"key_3": "bwr",
"key_4": null,
"key_5": 31066.0
}
Now I want to select this object, if the array measurements has an object that has key_c=="Value 3", as long as it does not have any object that has key_c=="Value 4". The object above should be selected, but not the one below.
{
"key_1": "uo2",
"key_2": false,
"measurements": [
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_4"
},
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_P"
},
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_3"
},
{
"key_a": null,
"key_b": 37.5,
"key_c": "Value_001"
}
],
"key_3": "bwr",
"key_4": null,
"key_5": 31066.0
}
The array measurements can be of any length, and is not sorted.
Thanks
Upvotes: 0
Views: 245
Reputation: 116740
As a scrunched one-liner:
select(.measurements|any(.key_c=="Value_3") and all(.key_c!="Value_4"))
Upvotes: 0
Reputation: 14655
I would suggest a filter like this one
def condition1: any(.key_c == "Value_3");
def condition2: any(.key_c == "Value_4") | not;
select(.measurements|condition1 and condition2)
Upvotes: 1