Forum Discussion
alex_panganiban
7 years agoContributor
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’]]), ) } }
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")
Related Content
- 2 years ago