2

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.

jerney
  • 2,187
  • 1
  • 19
  • 31

2 Answers2

3

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

machaval
  • 4,969
  • 14
  • 20
  • Thank you. Is there no way I can do this conditionally? For example, let's say some of the files coming from file:read have a header, and others don't. I can programmatically check if the header exists, but it would seem that I couldn't even do something like use a choice router where one branch parses w/ header and the other one without. Is that true? – jerney Sep 03 '19 at 23:21
1

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

enter image description here

Salim Khan
  • 4,233
  • 11
  • 15
  • Are you doing this in a standalone playground or within Studio (what studio version and runtime) ? – Salim Khan Sep 03 '19 at 20:21
  • 1
    To replicate what you are seeing , i removed the input directive and was just getting the second row in the output (similar to how Studio behaves). To make it work , you would have to explicitly configure at source as mentioned by Mariano in the answer below. – Salim Khan Sep 03 '19 at 21:12