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