[英]Jq: How to select an object if the object has an array of objects, that have a key with a value that does not match?
I have this JSON object that I want to extract with Jq:我有一个我想用 Jq 提取的 JSON 对象:
{
"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".现在我想选择这个对象,如果数组测量有一个具有 key_c=="Value 3" 的对象,只要它没有任何具有 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谢谢
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)
作为一个皱缩的单线:
select(.measurements|any(.key_c=="Value_3") and all(.key_c!="Value_4"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.