Forum Discussion
Another question… If an email exists for the person, I need to create an xml node like this:
<email_address>XXXX@xxx.edu</email_address>
<email_types>
<email_type>work</email_type>
</email_types>
< / email >
But if we don’t have an email address, it needs to be just:
< / email >
Is there a way to do this in the mapper? I can handle one condition or the other, but not both. Do I have to use a router and 2 mappers?.. (I can’t get the end tags to show in this editor unless I add spaces)
Hi,
Assuming that in the input document of the Mapper (where you want to produce the JSON Object to be formatted to valid XML) you have this kind of structure:
{
“email”: {
“type”: “work”,
“address”: “one@two.com”
}
}
then, if you put this into the Expression in the Mapper Snap:
($.hasPath("email") && $email != null) ? {}.extend( {email_address:$email.address, email_types:{email_type:$email.type } } ) : null
and assign it to the $email Target Path, that should produce the intended results.
Here’s a pipeline where the elaborated above is implemented:
XML_with_Mapper_2_2020_07_02.slp (6.5 KB)
BR,
Dimitri
You might be able to get around it by using the following expression in a mapper before trying to process the $messages value:
sl.ensureArray($messages)
This way, if it is a single object, it will be a single-entry array. If it is already an arrary, it simply passes through.
Yes, Kory is correct: it’s standard practice to use sl.ensureArray() on such elements. This is particularly the case when dealing with documents that were converted from XML. Unlike JSON, XML doesn’t have an explicit syntax for arrays, so there’s ambiguity when converting an element with one child – it may or may not represent an array with one element.