09-22-2020 11:12 AM
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
09-23-2020 11:09 AM
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:
What I like about this approach is its simplicity. But it’s lacking some flexibility:
09-22-2020 01:26 PM
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:
09-23-2020 12:17 PM
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.