cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Create Array from Flattened Docs

alex_panganiban
Contributor

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:

image001

Output data from the sort snap. Sorting by product-id, variant.

image

What my GroupBy snap looks like:

image003

13 REPLIES 13

cjhoward18
Employee
Employee

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?

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.

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.

cjhoward18
Employee
Employee

Using Json generator I used this array of flattened docs:
27%20AM

from there I sort on product-id using sort snap,

then I groupBy product-id into a field called โ€˜groupโ€™:
44%20AM

Finally in the mapper I use this expression:
18%20AM

which generates an array of docs where each doc has โ€˜product-idโ€™: [array of variants]:
30%20AM

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.