Forum Discussion
@darshthakkar you can use the Apace Velocity support that is available within the Generator Snaps, for example
for the following input JSON (e.g. JSON Generator) that:
- includes two fields (
msg1
,msg2
) that are intended to be combined into one output XML element - a
vary
field that could be eithernull
, contain a single String value, or a list of string values
[
{
"msg1" : "Hello", "msg2":"World", "num" : 1, "vary": null
},
{
"msg1" : "Hi", "msg2":"World", "num" : 2, "vary": "b"
},
{
"msg1" : "Hey", "msg2":"World", "num" : 3, "vary": ["b", "c"]
}
]
An XML Generator configuration like:
“Use default value for substitution” is enabled if you wish to replace any null
values with an empty String equivalent (see “Default value for substitution”)
The “Edit XML” contains:
#set( $msg = $msg1 + " " + $msg2 )
<root>
<msg>$msg</msg>
<num>$num</num>
#if ($vary.getClass().getName() == "java.util.ArrayList")
#foreach( $v in $vary )
<vary>$v</vary>
#end
#elseif ($vary.getClass().getName() == "java.lang.String")
<vary>$vary</vary>
#end
</root>
This will result in the following output:
darshthakkar
4 years agoValued Contributor
Thank you @robin, your help is much appreciated.
Related Content
- 3 years ago