简体   繁体   English

如何在 JQ 中通过不同级别的键收集值?

[英]How do I collect values by keys from different levels in JQ?

Let's suppose I have a JSON like this:假设我有一个像这样的 JSON:

[
    {
        "a": 1,
        "l": [
            {"b": "z"},
            {"b": "x"}
        ]
    },
    {
        "a": 2,
        "l": [
            {"b": "c"}
        ]
    }
]

I want to collect the data from all embedded arrays and to get an array of all objects with "a" and "b" values.我想从所有嵌入式 arrays 收集数据,并获取具有“a”和“b”值的所有对象的数组。 For the JSON above the result should be:对于上面的 JSON,结果应该是:

[
    {"a": 1, "b": "z"},
    {"a": 1, "b": "x"},
    {"a": 2, "b": "c"}
]

What JQ expression do I need to try to solve the issue?我需要什么 JQ 表达式来尝试解决问题?

You can use .l[] within the expression in order to return each element of the array returned in the response.您可以在表达式中使用.l[]以返回响应中返回的数组的每个元素。 So, use this one below所以,在下面使用这个

map({a} + .l[])

Demo演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM