โ01-02-2019 11:03 AM
I could really use some help. I have flattened documents and for each distinct product-id value, I wish to create an array list of the variants that belong in the product-idโs family. My desired results should look something like this, but Iโm not sure how to get there.
{
"masterproducts": [
{
"product-id": "1",
"variants": [
{
"variant": "988221"
},
{
"variant": "988331"
}]
},
{
"product-id": "1001",
"variants": [
{
"variant": "013356"
},
{
"variant": "933542"
},
{
"variant": "952005"
}]
},
{
"product-id": "1003",
"variants": [
{
"variant": "777100"
}]
}
]
}
Here's what I have so far.
My pipeline:
Output data from the sort snap. Sorting by product-id, variant.
What my GroupBy snap looks like:
โ01-02-2019 03:23 PM
I am unable to see your pipeline or input data in those images. can you give a clear example of the input data and the desired output data for the given input?
โ01-02-2019 03:36 PM
thanks for responding. not sure why, but my images wouldnโt upload. i inquired about this and am currently waiting for a moderator to help me with getting my illustrations and examples uploaded.
โ01-03-2019 08:57 AM
Hi CJ, I was able to get the images uploaded. You can see what my input looks like, and what Iโm trying to achieve, Iโve illustrated in the mocked up JSON thatโs at the beginning of my post. Basically, for each product-id, I want there to be an array of variantidโs.
โ01-03-2019 10:53 AM
Using Json generator I used this array of flattened docs:
from there I sort on product-id using sort snap,
then I groupBy product-id into a field called โgroupโ:
Finally in the mapper I use this expression:
which generates an array of docs where each doc has โproduct-idโ: [array of variants]:
If you need the output to look EXACTLY like you described you can use this expression instead of the one above: { โproduct-idโ : $group[0][โproduct-idโ],โvariantsโ :$group.reduce((accum, curval) => accum.concat([curval[โvariantโ]]), )}
which will generate output exactly as you described: [{โproduct-idโ:โ1001โ,โvariantsโ:[โ777101โ,โ777100โ]},{โproduct-idโ:โ1002โ,โvariantsโ:[โ777102โ,โ777103โ,โ777104โ,โ777105โ]}]
Let me know if this answers your problem.