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-04-2019 08:28 AM
Hi, I finally got it! Played around more with my expression and finally got what I needed. Sharing my solution:
{ “@product-id” : jsonPath($, “$group[0][‘@product-id’]”), “variations” : { “variants” : $group.reduce((accum, curval) => accum.concat([curval[‘variants’]]), ) } }
01-08-2019 04:07 PM
Just a couple of notes… The jsonPath() call used in the first part shouldn’t be necessary, you can access the field using a plain expression, like so:
$groupBy['@product-id']
The jsonPath() function would actually be more useful for the second part since you can use a wildcard in the path to traverse the array and extract all values for a given field. So, instead of the reduce() call, you should be able to just do:
jsonPath($, "$group[*].variant")
01-14-2019 03:38 PM
okay, i’ll try that out.
01-16-2019 02:54 PM
Thought I’d let you know how I was able to remove duplicate entries from my array. I basically was using this reduce method to create my array in a mapper expression.
$product.reduce((accum, curval) => accum.concat([curval[‘variation-attribute-value’][0]]), )
To remove any duplicate entries while the array was being built, I added a filter method to it, like so:
$product.reduce((accum, curval) => accum.concat([curval[‘variation-attribute-value’][0]]), ).filter((item, pos, a) => a.indexOf(item) == pos)
I pretty much just copy/pasted this filter method from the Snaplogic help documentation.
Thanks everyone for your help. I really appreciate all the support.