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:
Thank you @robin, your help is much appreciated.
- tlikarish6 years agoEmployee
Hi Smitha,
This should be possible. Try and look at this example and see if you can adapt it to for your needs.
The mapper can be used to conveniently alter binary data before parsing. See the “views” section in the Mapper documentation.
In the attached pipeline, the mapper is using binary views, converting the input data to a string, then replacing the
\r\n
to a space charactercr-example.zip (3.1 KB)
- bsmithab6 years agoNew Contributor
Hi tlikarish,I have tried the approach you have suggested, since as you see in the attached snapshot the record spans over multiple line and there is only CR in one of the columns in between, I have changed the expression to replace -$content.toString().replace(‘\r’, ’ '), by this the records did not get loaded properly, the records was still not considered as single record.
Can you let us know if we need to do something elseThanks
Regards
smitha csvparser.zip (25.5 KB)- tlikarish6 years agoEmployee
Are you seeing an error message? Since you’re using quotes and the column is quoted, the CSV Parser should treat the carriage return as part of the column’s value and not as a row delimiter. Is it possible the quoting is off?
- tlikarish6 years agoEmployee
Doh – also messed up with the expression I gave you.
$content.toString().replace('\r', ' ')
Only replaces the first match. You should probably use
replaceAll
or change the regular expression to/\r/g
. This will replace all carriage returns, so if the lines are delimited with\r\n
, then you’d have to use something like/\r(?!\n)/g
, which would remove all carriage returns not followed by a line feed.