cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Mapping optional arrays of objects to arrays of objects

rpatrick00
Contributor

I have an input schema that contains some optional arrays of objects in one format that I need to map to a target schema with an array of objects in another format.

The problem I am having is how to get the Mapper to map the array if it is present and ignore it if it is not. For example, my mapping table expression for one field below works fine for documents where the Address element is present but causes an error if it is missing.

jsonPath($, "$MatchUpdatePartyPayload.Address[*].NonStandardUS.Line1") 

Thanks,
Robert

12 REPLIES 12

Youโ€™ve probably already noticed this, but I should note for whoever else might be following along that both the label and the description of the โ€œNull-safe accessโ€ checkbox are confusing. What itโ€™s meant to address is data that missing from the input, not data thatโ€™s present but whose value is null โ€“ you donโ€™t need to check this checkbox just to map a present-but-null value to the output.

The description (tool tip) for this setting is a bit more accurate:

Enable this to ignore missing data when accessing the source path, such as $a but a does not exist in the source data

The problem with that description is that โ€œignoreโ€ implies it will simply omit any value from the output. Instead, it maps it to a null value. What weโ€™re looking for here is a way to actually do what this says โ€“ if a mapped value is missing from the input, donโ€™t put anything in the output for it.

So, yes, there are a number of ways to approach a solution, depending on how flexible you want to get. A simple way would be to replace the โ€œNull-safe accessโ€ checkbox with a 3-way choice:

Missing Data Policy:

  1. Error (equivalent to โ€œNull-safe accessโ€ = false now)
  2. Map to null (equivalent to โ€œNull-safe accessโ€ = true now)
  3. Omit (not possible now)

What I like about this approach is its simplicity. But itโ€™s lacking some flexibility:

  1. One setting applies to all of the mappings for that Mapper. What if you want different policies for different mappings within the same Mapper?
  2. The only condition where this will omit data from the output is when the value is missing from the input. What if you want to omit for other reasons, such as when the value is present but is a blank string? For that, I think youโ€™d need some sort of support in the expression language. Something like $.get(โ€œpathโ€, omit()) or $.getOrOmit(โ€œpathโ€). That adds more flexibility at the cost of relying on EL tricks that some users might find somewhat intimidating.

ptaylor
Employee
Employee

If you enable the Null-safe evaluation, then you could follow this Mapper with a second Mapper to prune out the null or blank fields with a mapping like this:

Expression:    $.filter((value, key) => value && value.trim())
Target path: $

See the Preview panels in this example:

image

rpatrick00
Contributor

Agreed. I had thought about the custom function in the expression language. The only issue I have with that one is that it is also, if ever so slightly, non-intuitive that you change behavior of the entire mapping operation for that row with a function in the expression (which typically just manipulates what data gets assigned).

If you really wanted to be able to set different policies for each row in the mapping table, you could add some sort of per-row setting which would clutter up the UI but might be a little more intuitive. For example, you could set the behavior at the mapper level and then override a particular row with the per-row settingโ€ฆ

Anyway, I will leave it to you to decide how you want to surface the functionality. I am just happy that you see the need for this.