Dataweave database output to CSV response - mule

I am trying to get rows from database and send the response in "CSV" format.
This is the transform component right after database select
%dw 2.0
output application/csv
---
payload map {
name : $.NAME,
id: $.Num,
age: $.age
}
When I hit the API using postman/soapUI, I get response as json, not csv.

Something like this maybe? List> converted to CSV (assuming you were issuing a select from the DB)

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.

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.

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

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 able to modify json output datamapper

I'm trying convert to csv to json format using mule datamapper ,it is working fine.
below output it produce
[ {
"propertyState" : "AL",
"propertyCity" : "NJ",
"propertyZipCode" : "67890",
"propertyCounty" : "US"
} ]
But want to remove [ ] this from json format. using datamapper is it possible
mule modify json output datamapper
[] defines List for the elements .. That is required if your elements are repeating and a Valid JSON format ..
If you don't want the [] to be there then the work around will be use <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to List" /> to extract each elements value from the JSON payload and create your payload using expression ..
But again this is not a recommended approach since you always require [] in your JSON as you will be getting multiple rows from CSV file and the JSON format should have [] since that will represent it as List which is a valid format