2

My goal is just to use Dataweave to remove the first line ('a') in the below CSV file and then convert CSV to JSON. I can get my Dataweave working online in the Dataweave Playground (https://developer.mulesoft.com/learn/dataweave/) but it will not run on an actual runtime for some reason.

Input CSV:

a
id,name
1,lee
2,natasha

Dataweave:

%dw 2.0
input payload application/csv headerLineNumber=2
output application/json
---
payload

Output JSON:

[
  {
    "id": "1",
    "name": "lee"
  },
  {
    "id": "2",
    "name": "natasha"
  }
]

Problem, when I run this on a local runtime, here is the output I get using the exact same dataweave. Curious what I could be doing wrong? Why would the exact same script work online but not on an actual runtime?

[
    {
        "a": "id",
        "column_1": "name"
    },
    {
        "a": "1",
        "column_1": "lee"
    },
    {
        "a": "2",
        "column_1": "natasha"
    }
]
llawliet
  • 125
  • 1
  • 1
  • 5
  • Does this answer your question? [Reading CSVs w/o Headers in DataWeave 2.0](https://stackoverflow.com/questions/57778065/reading-csvs-w-o-headers-in-dataweave-2-0). This is a duplicated question and should be closed. – aled Dec 08 '22 at 11:48

1 Answers1

0

I am noticing the same behavior, and as per this answer the input directive does not work with reader properties.

However, to achieve what you want you can do either of the following.

  1. Use an additional transform message that will give application/csv as output and with writer property headerLineNumber=2. And then transform this to JSON with another Transform message component.

    <ee:transform doc:name="To remove first line using headerLineNumber" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0 output application/csv headerLineNumber=2 --- payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <ee:transform doc:name="To convert to JSON" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0 output application/json --- payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    
  2. Define "MIME Type" at the connector from where you are getting your CSV. Generally most connecters has a "MIME Type" tab, where you can define the output and additional writer properties. If you know that your connector will always generate a CSV like that, you can use this

enter image description here

Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22
  • Your answer is correct but unfortunately a duplicate. – aled Dec 08 '22 at 11:50
  • 1
    You are right, that it is about the same `input` derivative. However, the question is also about skipping the first row of the CSV data, which the other question does not answer. But I agree if OP had found that question, he/she had enough info to do this without asking a new question – Harshank Bansal Dec 08 '22 at 11:54
  • Both of these options worked! Thank you very much, I see the same output in runtime now as the Dataweave Playground. – llawliet Dec 08 '22 at 14:07