jsoncsvdataweavecsvtojsoncsvtoarray

I'm trying to convert data from csv to json . My csv input is split by (;) is there an easy way to do so?


the dataweave script i tried is throwing an error that says i have to first transform the data into an array using then concert to a json object.It's returning null instead of the values.

INPUT:
id;email;phone;Fname;Lname
154784;lamiaomd00@gmail.com;0123456789;omd;lamia

MY CODE:

%dw 2.0
output application/json
---
payload map (item, index) -> {
    id: item.id,
    email: item.email,
    phone: item.phone,
    Fname: item.Fname`
    Lname: item.Lname
}

OUTPUT:

[
  {
    "id": null,
    "email": null,
    "phone": null,
    "Fname": null,
    "Lname": null
  }
]

Solution

  • You must tell DataWeave that it is a CSV but separated by a semicolon instead of a comma. If it is a Mule application the preferred solution is by setting the output type reader properties at the component that creates that payload. In Mule 4.x you can do that by using the outputMimeType attribute of connectors. Reader properties are configured after the format".

    For example if you read the CSV from a Mule 4 file connector you can set the separator character like this:

    <file:read doc:name="Read" config-ref="File_Config" path="staff.csv"
                   outputMimeType='application/csv; separator=";"; header=true'/>
    

    After setting the separator character it should work.

    If for some reason you can not change the output type at the source you can do it in the flow using <set-payload>.

    <set-payload value="#[payload]" mimeType='application/csv; separator=";"; header=true' />