Search code examples
csvmuleanypoint-studiodataweave

How do you ignore the 1st two lines of a CSV file in Dataweave?


I am using Anypoint Studio 6.1 with Mule 3.8.1 and in the Transform Message component I have an input CSV file which has fieldnames on the 1st line and datatypes for that filed on the 2nd line. I want to start processing the records from line 3 onwards, but cannot configure the Reader Configuration to miss the 2nd line containing the datatypes.

Is there a way I can do this so the 1st line is used for fieldnames, the 2nd line is ignored and the 3rd line onwards are the records that are processed?

Test CSV:

Product,Price
string,decimal(10,2)
phone,99
tv,499

XML flow:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <flow name="csvreaderFlow">
        <dw:transform-message metadata:id="08c0a5d4-9d7d-44ad-b72b-bac83ab6b6c1" doc:name="Transform Message">
            <dw:input-payload doc:sample="sample_data\list_csv.csv" mimeType="application/csv">
                <dw:reader-property name="bodyStartLineNumber" value="2"/>
            </dw:input-payload>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload map ((payload01 , indexOfPayload01) -> {
    products: payload01.Product
})]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>

Even if I increase the body line start number to 10, it still shows the 2 line and the 2 records to process. I thought this would cause an error.

enter image description here

enter image description here

Thanks


Solution

  • In the Input reader configuration for CSV on the Dataweave, they have a setting "bodyStartLineNumber" (set that to '3' in your case). Let me know if you have issues,

    Here are the steps to be followed on the Dataweave,

    1. Right click on the "Payload" label (left panel).
    2. Click on the "Reader Configuration" option.
    3. GUI opens with multiple options to be set.
    4. Configure the "Body Start Line Number"