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.