โ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-03-2019 12:41 PM
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!
โ01-03-2019 12:52 PM
ok great, feel free to ask if you need further help.
โ01-11-2019 03:21 PM
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
โ01-14-2019 01:37 PM
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.
โ01-03-2019 04:03 PM
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:
Expression Builder Expression and Map:
My actual results. Rather than the โvariantโ element repeating, it seems to be looping back up to โvariants.โ
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