Search code examples
dataweavemulesoftmule4

How to convert a CSV with data having comma inside quotes to JSON in Mule 4


I am trying to convert a CSV with complex data to JSON but the quotes don't transform as expected.

Below is my flow in Mule application:

<flow name="csv-Flow">
    <file:listener doc:name="File" directory="${path}" config-ref="File-Inbound" outputMimeType="application/csv; quote='; escape=&quot;\\&quot;; separator=&quot;,&quot;; header=true">
        <scheduling-strategy>
            <fixed-frequency frequency="${frequency}"/>
        </scheduling-strategy>
    </file:listener>
    <set-payload value='#[%dw 2.0
    output application/json
    ---
    payload map {
     "Name": $."Name",
     "Address": $."Address",
     "Phone": $."Phone"
    }]' doc:name="Set Payload"/>
    <logger level="INFO" doc:name="Logger" message="#payload"/>
</flow>

Input csv:

Name,Address,Phone
xyz,"{""House No"":""SD/123"",""Street"":""First"",""State"":""PA""}",123456789

Actual JSON Output:

 {
  "Name": "xyz",
  "Address": "\"{\"\"House No\"\":\"\"SD/123\"\"",
  "Phone": "\"\"Street\"\":\"\"First\"\""
 }

Expected JSON Output:

{
 "Name": "xyz",
 "Address": "{"House No":"SD/123","Street":"First","State":"PA"}",
 "Phone": "123456789"
}

Any suggestions will be greatly appreciated.


Solution

  • Change the outputMimeType attribute of the File listener to:

    outputMimeType='application/csv; quote="\""; header=true; escape="\"\""; separator=","'
    

    The output will be:

    [
      {
        "Name": "xyz",
        "Address": "{\"House No\":\"SD/123\",\"Street\":\"First\",\"State\":\"PA\"}",
        "Phone": "123456789"
      }
    ]
    

    Note that the backslack '' characters are required by JSON to escape quotes inside a string. Your expected JSON output is not valid JSON because the quotes inside Address are not escaped correctly.

    Also note that the payload is an array that the map function is iterating over, so the output is also an array in JSON.