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.