Input:
{
"level1": {
"id": "a"
},
"level2": [
{
"id": "1"
},
{
"id": "2"
}
]
}
{
"level1": {
"id": "b"
},
"level2": [
{
"id": "3"
},
{
"id": "4"
}
]
}
Expected output:
a 1
a 2
b 3
b 4
What I basically want to accomplish is:
jq -r '[ .level1.id, (.level2[] |.id ) ]|@tsv'
where .level1.id repeats for every object in the .level2 array but I don’t seem to be able to think about the problem the right way
>Solution :
Using .level2[] within another array constructor (the outer […]) just collects all items iterated over into that constructed array. Separate them to iterate outside another array, make both parts create (sub)arrays themselves, then add them together:
[.level1.id] + (.level2[] | [.id]) | @tsv
Alternatively, use variables which you can reference from inside the array constructor:
.level2[] as {$id} | [.level1.id, $id] | @tsv
Output:
a 1
a 2
b 3
b 4