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

alex_panganiban
Contributor

Oh, this is AWESOME! While the scenario I created isn’t exactly what I needed (I was trying to keep it simple), the solution you provided should be enough for me to run with. I’m gonna try it out now. Thanks so much for the assist!

ok great, feel free to ask if you need further help.

Hi CJ, your solution has been really helpful to me. Thank you so much. I have an add-on question about this solution though. As it’s building the new array using the reduce statement you passed on to me, how can I insure that I’m not putting duplicate values into it? Hoping you can help.

Thanks, Alex

glad it was helpful.
You can use a map to store your values instead of an array and store the values as your keys. Maps do not allow for duplicate key entries, which would take care of this problem for you, and you could collect your ‘values array’ by using keys() method described here:
https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439367/Object+Functions+and+Properties#Ob...

This is just one solution, there are many ways you can keep track of this if needed.

alex_panganiban
Contributor

Thanks so much again for your help. I’m pretty new to using both JSON and Javascript, so I’m learning quite q bit here. I’m so much closer to where I need to be.

Ultimately, I need something that looks like this:

Catalog
Product : {
@product-id : 1002,
variations : {
variants : [{
variant @product-id : 888
variant @product-id : 777
variant @product-id : 666
}]
}
}

Output from my GroupBy:

v2

Expression Builder Expression and Map:

Catalog%20Map
b64c095060137eafdb3bf869c02d45ea7779a20e.png

My actual results. Rather than the “variant” element repeating, it seems to be looping back up to “variants.”
ActualResults

I tried using a JSON Path Epresssion Tester that I found on Google, but it wasn’t much help for me. Most likely due to my inexperience. Any guidance would be greatly appreciated.

Alex