I'm trying to convert json to csv format in mule 4 dataweave 2.0. But it's not working. Is there anything specific for csv format that we should add in our dataweave ? I tried in my dw writing :
%dw 2.0
output application/csv
---
payload
Kindly find below my json input file:
{"exportId": "WakeupHierarchy",
"instance": "20190821151943036",
"content": [
{
"file": {
"header": {
"id": "12",
"name": "WakeupHierarchy",
"description": "Wakeup Hierarchy",
"version": "1.0"
},
"body": {
"records": [{
"alternateHierarchy": "ACD",
"tablePath": "/root/account/Account",
"referenceDataForeignKey": { "linked_record": {}},
"accountForeignKey": {
"linked_record": {
"id": 100199493,
"identification": {
"sapId": "100091984",
"name": "CHILDRENS HOSPITAL & MEDICAL CENTER"},
"address": {
"line1": "8200 DODGE ST",
"line2": "ACCOUNTS PAYABLE" },
"functions": {
"commercialCustomer": true,
"shipTo": true },
"otherProperties": {
"status": "A",
"sapOrderBlockStatus": null},
"hierarchies": {
"memberships": []},
"affiliations": {
"shipTo": [],
"billTo": []},
"accountTBD": { "credit": {"creditAccount": null},
"franchise": {"franchiseActivity": null },
"tbdGroup": {
"vcRebateProgram": null,
"vcOrSurgicalBasedOnProductPurchasingVcSgOrBoth": null }},
"technicalData": {
"lastUser": "edie.belzner@alcon.com",
"linkedTable": "/root/account/Account"}}},
"technicalData": {
"lastUser": "ed.com",
"primaryKey": "A93"}},
{ "alternateHierarchy": "ACD",
"tablePath": "/root/account/Account",
"referenceDataForeignKey": {
"linked_record": {}},
"accountForeignKey": {
"linked_record": {
"id": 19,
"identification": {
"sapId": null,
"sfdcId": "803827"},
"address": {
"line1": "2601 S MCKENZIE ST STE 234",
"line2": null},
"functions": {
"commercialCustomer": false,
"shipTo": false},
"otherProperties": {
"status": null,
"sapOrderBlockStatus": null},
"hierarchies": {
"memberships": []},
"affiliations": {
"shipTo": [],
"billTo": []},
"accountTBD": {"credit": {"creditAccount": null},
"franchise": {"franchiseActivity": null},
"tbdGroup": {
"vcRebateProgram": null,
"vcOrSurgicalBasedOnProductPurchasingVcSgOrBoth": null}},
"technicalData": {
"lastUser": "edie.belzner@alcon.com",
"linkedTable": "/root/account/Account" }}}'
"technicalData": {
"lastUser": "edie@hotmail",
"primaryKey": "AC|19"}}]},
"footer": {
"nb_records": 87
}}}],
"pagination": {"nextPage": "51943036/2"}}
Hi In order for DW to write a CSV the output DataStructure needs to be an Array of Objects so in order to do that you need to transform the shape of your input into an array of objects where the object need to be of simple values (string|number|boolean|dates)
For example something simple for your input would be
%dw 2.0
output application/csv
---
flatten(payload.content.file.body.records) map ((record, index) ->
{
alternateHierarchy: record.alternateHierarchy,
tablePath: record.tablePath
}
)
This would output
alternateHierarchy,tablePath
ACD,/root/account/Account
ACD,/root/account/Account