Star
Star

Reputation: 1503

Data Weave not able to escape the string inside double quotes- Mule ESB

I'm trying to convert csv to csv. My deliliter is ,, there are certain texts in input which also comes as with comma example: "Fun,Chair,tables" but it will be wrapped with quotes. Data Weave thinks this is as delimiter and separates in to different column in response.

Please find the Input CSV file

 sequence,items
 1,Fun
 2,"Fun,chairs,tables"
 3,Games

But getting response as

Number,itemDetails
1,Fun
2,Fun\,Chair\,tables
3,Games

In the above response, second two is splitted which i'm not expecting my dataweaver

  %dw 1.0
  %output application/csv header=true 
   ---
  payload map { 
   Number:$.sequence,
   itemDetails:$.items

Expected response is

 Number,itemDetails
 1,Fun
 2,"Fun,chairs,tables"
 3,Games

Problem here is "Fun,chairs,tables", double quotes should go as test field, instead data weaver consider this as a delimiter. In the Data weave header,tried with quote= " " and escape=" ". But not working. I have tried same thing on Datamapper working perfectly fine. Believe somethings needs to be done on header, not sure what but??. Checked the properties in read configuration properties in Data Weave CSV settings also. No help.

Edit: With the DataWeaver escape sequence, %output application/csv header=true escape="\"" Below is the response

Number,itemDetails
1,Fun
2,Fun",Chair",tables
3,Games

Your thoughts and suggestions will be helpful. Thanks in advance.

Upvotes: 2

Views: 7394

Answers (4)

we have to change the reader property like below. I've also faced the same problem while reading the csv input.

<dw:reader-property name="escape" value="&quot;"/>

above worked in my case and solved the problem

Upvotes: 0

Abhay
Abhay

Reputation: 324

You can enclose the value in quotes by yourself

%dw 1.0
%output application/csv header=true
---
payload map ((item , index) -> {
    Number: item.sequence,
    itemDetails: item.items
} when index != 1
otherwise {
    Number: item.sequence,
    itemDetails: "\"" ++ item.items ++ "\""
})

Output

Number,itemDetails
1,Fun
2,"Fun\,chairs\,tables"
3,Games

Upvotes: 0

testbeginer
testbeginer

Reputation: 11

Try

%output application/csv quoteValues=true ,escape =" "

Upvotes: 0

Ismael G.
Ismael G.

Reputation: 378

Did you try escape="\"" ? I think this will escape the double quotes

Upvotes: 1

Related Questions