Scenario:
Today, i have a flow to create a simple CSV with dataweave and ftp component, i receive the data by text/plain file, the process in fact is when the record its 1 , i'll put the header = true, the next records will be false, just need the firts recor put the header to add the column names. To solve this validation today i have a choise component to generate with 2 similar dataweave the only change that has the first record its with the next code "output application/csv separator=",",header=true" and the secod dataweave "output application/csv separator=",",header=false", this post i generated beacause i need your help if exist different way to solve this design, or just with a one dataweave inside this detect that the variable record change the header = true or false depends by the variable record
Actual Code:
if the variable record = 1
%dw 2.0
output application/csv separator=",",header=true
---
payload.rows map {
"number" : $.number default '',
"total" : $.total default 0,
}
else
%dw 2.0
output application/csv separator=",",header=false
---
payload.rows map {
"number" : $.number default '',
"total" : $.total default 0,
}
I would wait if there is a possibility (join both dataweave because the difference is the header):
%dw 2.0
output application/csv separator=",", header=(if(vars.record == 1) true else false)
---
payload.rows map {
"number" : $.number default '',
"total" : $.total default 0,
}
NOTE: this is just doubt i don't know if exist any way to do.
Any help would be appreciated. Thank you.
You can define dynamic properties using the write
function. Note that it requires actually setting the output writer to binary and declaring the mime type as CSV so Mule propagates it as such. This is because CSV writing happens at the function level:
%dw 2.0
output application/csv with binary
var data = payload.rows map {
"number" : $.number default '',
"total" : $.total default 0,
}
---
write(data, "csv", {"header": (vars.record == 1), "separator": ","})