cancel
Showing results for 
Search instead for 
Did you mean: 

Mapper following Group By Field

chrisbodman
New Contributor II

Hi Everyone,

I have a record, that has a field of CSVs. I split the CSVs using the JSON splitter, and then use a group by fields snap to create my objects which have the recordId, and the individual values from the CSV field. I end up with what I need ( {Id:1, Roof:true}, {Id:1, doors:true}), but then I need to have the object created with the options as fields… So {Id:1, Roof:true, doors:true}. Sometimes, there’s no roof attribute, or doors attribute. There could be 1 field in the original CSV field, there could be 10.

1 REPLY 1

tlikarish
Employee
Employee

Hi chrisbodman,

If I understand your problem, you’re looking to take the output of a Group By Fields and flatten the grouping into a single object?

So in your example, this

{Id:1, Roof:true}, {Id:1, doors:true}

becomes

{Id:1, Roof:true, doors:true}

If that’s the case, than you could use this expression in a mapper.

jsonPath($, "$group[*]").reduce((accum, o) => accum.extend(o), {})

Essentially you’re doing a reduce on the array to build a new object as the result. Here’s a break down of how it works:

  1. jsonPath($, "$group[*]") get the elements underneath $group
  2. reduce((accum, o) => ..., {}) reduce the array of objects into a single object {}
  3. accum.extend(o) for each object in the array, extend the accumulating object with its members

Here’s a pipeline example you can use to play around with it. Let me know if this works.

reduce-example_2021_03_18.slp (4.7 KB)