Search code examples
jq

How to merge arrays within JSON object


Given a JSON object

{
"foo":["barfoo"],
"bar":["foobar"]
}

I would like to merge both arrays, i.e. output:

barfoo
foobar

Obviously I can list either array using, e.g. jq -r .foo[] but I'm not sure how to go about merging both.

The array names (foo / bar) are constants, they do not change.


Solution

  • To get all the second-level items, iterate two levels deep:

    jq -r '.[][]'
    

    To get all the leaf-level items, descend recursively, and filter for scalars:

    jq -r '..|scalars'