Search code examples
mulemule-studiomule-componentmule-el

Number formatting not working in Mulesoft dataview


I am trying to format number in below json string in Mulesoft.

{
  "Data": [
    {
      "Date": "10/12/2012",
      "AccountNumber": 123456,
      "Amount": 1424511.76
    },
    {
      "Date": "10/12/2012",
      "AccountNumber": 123456,
      "Amount": 80123.86
    }
  ]
}

My data view:

 %dw 1.0
%output application/csv header=false

(payload.Data map ((payload01 , indexOfpayload01) -> {
"Trade Date" : payload01.Date, 
"Account": payload01.AccountNumber,
"Amount": payload01.Amount as :string { format: "#,###.00##"} 

}))

I want to create CSV file with this code. But it is adding "\" with commas after formatting numbers. For first Amount I am getting

"1\,424\,511.76"

I am expecting formatted number as

1,424,511.76

I am not able to understand why it is adding "\" character with comma. Please help me out!


Solution

  • It is escaping the commas because you are using a CSV(Comma Separated Value) so it will break downstream probably otherwise.

    Depending on who needs to read this CSV file, you could possibly quote the values instead:

     %output application/csv quoteValues=true
    

    Or:

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

    or maybe change the CSV to pipe delimited or some other separator:

    %output application/csv separator="|"