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)