简体   繁体   中英

Converting CSV to array using DataWeave

I'm using Mule Requester to load a CSV file. After the file is loaded, the payload is a byte array, which I store in a flow variable, mycsv . I keep getting an exception.

org.mule.api.MessagingException: Exception while executing: 
    NetIds: flowVars.mycsv map $."My Column Name"   
                        ^
Cannot coerce a :binary to a :array (com.mulesoft.weave.mule.exception.WeaveExecutionException). Message payload is of type: byte[]

Here's my DataWeave code:

%dw 1.0
%output application/java
---
{
    Values: flowVars.mycsv map $."My Column Name"
}

The previous flow element is a choice , so I set the metadata on that to output to a FlowVar with the right name and referenced a sample CSV file, so DataWeave things the variable type is List<Csv> .

How can I read the CSV? Thanks!

It's because it doesnt know the mimeType of the flowVar because it's not set. Try this before the dataweave transformer:

<set-variable value="#[flowVars.mycsv]" variableName="mycsv" mimeType="application/csv" doc:name="Variable" />

or set the mimeType when you first read the csv.

This is the first time I found a requirement to put two different formats into one csv file. In order to get the expected result, I prefer to use this trick: Create both expressions and combine them into one simple array, using flatten Add an empty object as line separator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM