Sri Arun
Sri Arun

Reputation: 143

How to transform comma separated string into csv in mule?

I have a requirement where I have dynamic comma separated string value in a flow variable, which I need to convert into a csv file values.
for example, I can have following comma separated string values in variable:

abc,efg,ijk

or

mno,123,qrs,w3e    

should be transferred into csv values like:

"abc","efg","ijk"    

or

"mno","123","qrs","w3e"     

I tried the following:

<set-variable variableName="stringValue" value="#['abc,efg,ijk']" doc:name="Variable"/>

  <dw:transform-message doc:name="Transform Message">
       <dw:set-payload><![CDATA[%dw 1.0
%output application/csv header=true, quoteValues=true, quoteHeader=true
---
flowVars.stringValue splitBy ","]]></dw:set-payload>
 </dw:transform-message>    

So, here I a expecting the csv file with value:

 "abc","efg","ijk"    

But its not working , getting error!

Upvotes: 1

Views: 1744

Answers (1)

Brad Cooper
Brad Cooper

Reputation: 379

You may have already solved this, but if not - you're actually very close. The transformation to CSV expects a 2-dimensional array as input: an array of the lines for the file, where each line is an array of columns.

For example: [["line1, Col1", "line1, Col2"], ["line2, col1", "line2, col2"], ... ].

After the splitBy in your example you only have a single array - ["abc","efg","ijk"], so the solution is to wrap that in an array: [flowVars.stringValue splitBy ","]. This gives the output you are looking for.

Upvotes: 1

Related Questions