简体   繁体   English

Jq:如果对象有一个对象数组,其键的值不匹配,如何选择一个对象?

[英]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)

Try it online! 在线试试吧!

作为一个皱缩的单线:

select(.measurements|any(.key_c=="Value_3") and all(.key_c!="Value_4"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JQ解析JSON对象数组,使用select匹配object元素中的指定键值,然后转换为object元素,然后转换为select - Use JQ to parse JSON array of objects, using select to match specified key-value in the object element, then convert to CSV 如何根据jq中上述数组中对象中元素的值选择/获取对象元素的(对象数组)键? - How to select/get object elements' (array of objects) keys based on value of an element in an object in the aforementioned array in jq? jq map object 键值到包含两者的对象数组 - jq map object key value to array of objects containing both 使用 JQ 解析 JSON 嵌套对象,使用 select 匹配嵌套对象中的键值,同时显示现有结构 - Use JQ to parse JSON nested objects, using select to match key-value in nested object while showing existing structure jq select object 其键值在列表中 - jq select object whose key value in the list 如何使用jq交换对象的键和值? - How to swap key and value of an object using jq? jq:将对象数组转换为对象 - jq: translate array of objects to object Select 对象基于使用 jq 的 object 中的变量值 - Select objects based on value of variable in object using jq 使用jq向文件中json数组中的每个对象添加新的键/值 - Add a new key/value to every object in json array in file with jq 如何通过jq选择键和子对象属性? - How to select key and sub-object property via jq?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM