Reputation: 91
I am trying to generate a CSV
output with Headers in the first row then left empty in the second row and the data starts from the third row. How to generate this using DataWeave 2.0.
Thanks in advance.
Upvotes: 0
Views: 794
Reputation: 25872
The correct answer is what George posted. The output with an empty row is not a valid CSV and for that reason a CSV output in DataWeave can not be used to generate it. You can however hack it your way by writing the header and the data as separate strings and concatenating a newline in the middle. I don't recommend to do this but if you have some weird requirement and you are forced to do it, I modified George's example to do it.
%dw 2.0
output text/plain
var data = [
{
header1: "v1",
header2: "v2",
header3: "v3"
},
{
header1: "v4",
header2: "v5",
header3: "v6"
}
]
var headers = data[0] mapObject ((value, key, index) -> (index): key) // take the headers from the first row of data to be used as the data of the first row in the output
---
write(headers, "application/csv", {header:false}) ++ "\n" ++ write(data, "application/csv", {header:false})
Upvotes: 2
Reputation:
As per the spec what you want to generate is NOT a CSV file.
The closest you can get to it is to add a row with no data, which can be accomplished with the DW expression below:
%dw 2.0
output application/csv
var data = [
{
header1: "v1",
header2: "v2",
header3: "v3"
},
{
header1: "v4",
header2: "v5",
header3: "v6"
}
]
var empty = {
header1: "",
header2: "",
header3: ""
}
---
empty >> data
Upvotes: 1