Reputation: 392
My input, command and first (partial) results:
echo '
{
"a": {
"b": [
"c",
"d"
],
"e": [
"f",
"g"
]
},
"h": {
"c": [
"i",
"j"
],
"d": [
"k"
],
"f": [
"l",
"m",
"n"
],
"g": [
"o"
]
}
}' | jq '.a | keys_unsorted[]'
"b"
"e"
I would like to iterate over the results' ("b", "e") arrays (["c", "d"] and ["f", "g"]) and reuse that result of that query to search the second part of my tree (the "h" part), iterating of the arrays of the outcome and display those.
So my complete result would look like this:
"b", "c", "i"
"b", "c", "j'
"b", "d", "k"
"e", "f", "l"
"e", "f", "m"
"e", "f", "n"
"e", "g", "o"
Catch is, I would like to do this in a single query, because of resources/time constraints in the final solution.
I tried (a lot of) things, like "keys as $k ..." but am not able to wrap my brain around it on how to use this.
Can someone give me a hint or show me a working solution and tell me how it works?
Thanks in advance!
Upvotes: 1
Views: 793
Reputation: 116800
Consider first this filter:
(.a | keys_unsorted[]) as $k1
| (.a |.[$k1][]) as $k2
| (.h |.[$k2][]) as $k3
| [$k1, $k2, $k3]
With your data, the invocation:
jq -c -f filter.jq input.json
produces:
["b","c","i"]
["b","c","j"]
["b","d","k"]
["e","f","l"]
["e","f","m"]
["e","f","n"]
["e","g","o"]
The rest is left as an exercise :-)
Upvotes: 4