I am not sure if the operation name is proper here, but the example should show well my intention.
This is exactly how unwind aggregation operation in Mongo or unnest in BigQuery behave.
Having that json structure:
[
{
"root-array-a": [
11,
12,
13
],
"root-property-b": 22,
"root-property-c": 33
}
]
I would like to get a result:
[
{
"root-property-a": 11,
"root-property-b": 22,
"root-property-c": 33
},
{
"root-property-a": 12,
"root-property-b": 22,
"root-property-c": 33
},
{
"root-property-a": 13,
"root-property-b": 22,
"root-property-c": 33
},
]
2
Answers
This works:
{"root-property-a": ."root-array-a"[]}
constructs an object with aroot-property-a
key for each value inroot-array-a
(because of the[]
operator, and because jq implicitly fans out multiple outputs as necessary).+ .
adds that key to the original object, and thedel
removes the unwanted array.You can use stream the array when constructing an object which will generate each combination of inputs:
Output: