Converting CSV to array using DataWeave - mule

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

Related

Dynamically reference payload field names in Dataweave

I am using Anypoint Studio 7.3 and Mule 4.1.
I am looking to dynamically pass the field name from a JSON payload when transforming a message so on the 1st call I want to use the values in the "cat_name" field and when I call it a 2nd time I want to use the values in the "dog_name" field as the output message structure will be the same. So for "cat_name" I would want $.(vars.codetest) to be resolved as payload.cat_name and for "dog_name" I would want $.(vars.codetest) to be resolved as payload.dog_name
Is there a way of doing this?
%dw 2.0
output application/json
---
(payload distinctBy $.#[vars.codetest]) map ((payload01, indexOfPayload) ->{
name: $.(vars.codetest)
})
Thanks for any help
Something like this should work:
%dw 2.0
output application/json
---
payload
distinctBy $[vars.codetest]
map ((element) -> { name: element[vars.codetest] })
You might need parens around codeTest (i.e. (vars.codetest)) so that it gets evaluated before the lookup.

Define Datawave data into database and use that defined data to transformation

Can You please help me to transform the data from database?
I mean the transformation is defined in Database.
I am retrieving the transformation from Database and put it in Datawave(DW).
But it is not working. It is showing the output as string.
Following i am retrieving from database:
flowVars.orderJsonData map
{
phone: flowVars.orderJsonData[0].phone
}
I put this into variable and put into the datawave as output
But it is working as string only.
Please help.
Assuming flow variable dwexp has the dataweave expression, you can execute that using expression-transformer. Below is the syntax
<expression-transformer expression="#[dw(flowVars.dwexp,"application/json")]" doc:name="Expression"/>
dw() function is in the format
#[dw("<Dataweave script>","Output Mime type")]

Mulesoft Dataweave convert CSV to map dynamically

I've created a flow that accepts a CSV via a form-data API POST and then processes each row in a batch job. Ideally, I do not want to hand-map each column before passing it to the batch executor, but I could not figure out how to just "pass through" the key/value pairs dynamically. I want to ensure that the CSV can have new columns and we'll pass them through without knowing about them beforehand... This is what I have:
%dw 1.0
%output application/java
---
payload map {
title: $.title,
description: $.description,
template_id: $.template_id,
pricing_flat_price: $.pricing_flat_price,
scheduled_start_date: $.scheduled_start_date,
resource_id: $.resource_id,
industry_id: $.industry_id,
owner_email: $.owner_email,
location_offsite: $.location_offsite
}
And this is what I'm going for (in non-sensical psuedocode):
%dw 1.0
%output application/java
---
payload map {
*:*
}
I have to imagine this is easily done, but I couldn't figure it out.
Thank you,
Steve
You can simply use transformation like below -
%dw 1.0
%output application/java
---
(payload)
hand-map each column before passing it to the batch executor
You shouldn't need to do this. Pass the whole csv to the batch-input phase and allow it to split each line into a Linked Hashmap.

How to refer payload which is a ResultSet Iterator in mule dataweave?

Sql query returns a streamed output as Resultset iterator object from the Database component.
i want to convert this to xml in dataweave. But don't know how to refer the incoming object,
If it's a map i can access it simply by using .operator like payload.student
Tried using payload.next() but it gives an error. Also tried the following,
%var input1 payload as :iterator but still wont' work
Here the steps:
Drag and drop the Transform Message (Dataweave) component after your configured DB Connector. You will see that the input payload for dataweave script is filled with the db result List<Map>.
Then you can access the fields, using the map function in dw.
dw script
%dw 1.0
%output application/xml
---
{
"Results":{
(payload map {
"key1":$."db_field1",
"key2":$."db_field2"
})
}
}
Can you post your code (XML) and a screenshot of dataweave or debugger?
My first guess would be to use a standard transfomer to transform that object to a list or map before the dataweave transformer.

Mule how to map Json values from payload to flow variables

I have a JDBC COnnector which retrieves values from the database and I have mapped them from object to JSON. Now I want to extract specific values from the json to flow variables. When I try to log #[message.payload], I get the full payload in the log which is in JSON format. However when I try to choose an attribute (eg. testattribute in json) #[message.payload.testattribute], I get mule expression error. How do I refer to the json values?
Once the payload is a JSON string, you can't extract anything from it anymore with an expression.
So either:
use MEL to extract the values into flow variables prior to convert the payload into JSON,
transform the JSON payload to either POJOs or Map/Lists, use MEL to extract the values into flow variables, and re-transform the payload back to JSON.
If your requirement is to get the values from json data you can use a simple MEL expression like
#[json:<name of the node>]
For example if you have an sample json data like this
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
and if you want get the id from the data just use the below simple MEL.
#[json:'id']
Mule has JSON-to-Object transformer which can be used to get JSON elements.
For example if your JSON is following :-
{
"token" : 123,
"id" : 456,
"email" : "abc#abc.com",
"status" : "Success"
}
Now, to extract the elements, you need to use :-
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" />
And then can extract like :- #[message.payload.email] or #[message.payload.status]
Use components as follow
Object - String
Object - JSON
then u can directly extract using value using json expression as #{json:email},
in below xml i have used the variable component to set the #{json:email} in "var1" flow variable for usability
Note: json expression is even available in studio version of 5.x
<object-to-string-transformer doc:name="Object to String"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<set-variable variableName="var1" value="#[json:email]" doc:name="Variable"/>