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

Viewed 226

Let's suppose I have a JSON like this:

[
    {
        "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. For the JSON above the result should be:

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

What JQ expression do I need to try to solve the issue?

1 Answers

You can use .l[] within the expression in order to return each element of the array returned in the response. So, use this one below

map({a} + .l[])

Demo

Related