1
votes

How do you parse a CSV without a header in DataWeave 2.0?

I have the following CSV:

Chris,Doe,Student
Bob,Smith,Teacher

and am trying to merely convert it to JSON like this:

[
  [ "Chris", "Doe", "Student" ],
  [ "Bob", "Smith", "Teacher" ]
]

or even this:

[
  {"0": "Chris", "1": "Doe", "2": "Student" },
  {"0": "Bob", "1": "Smith", "2": "Teacher" }
]

Here's my DataWeave:

%dw 2.0
input payload application/csv header=false
output application/json
---
payload

But this is the payload being returned from the DW script:

[
  {
    "Chris": "Bob",
    "Doe": "Smith",
    "Student": "Teacher"
  }
]

I've tried messing around with the metadata, specifying CSV metadata type that include/exclude headers, but no luck.

2

2 Answers

2
votes

This doesn't work at runtime because input directive doesn't work in mule reader properties need to be configured on the source of the value. Go to the source of your value (file:read, http:listener, etc) click on MimeType in there go an pick application/csv and under that there are the mimeType properties. In there go and select header | false

0
votes

The script that you published in there works for me. Modified it a bit and am attaching the screenshot below.

enter image description here