如何使用 mule 4 dw 2.0 将 json 转换为 csv?
How to convert json to csv using mule 4 dw 2.0?
我正在尝试将 json 转换为 mule 4 dataweave 2.0 中的 csv 格式。但它不起作用。我们应该在 dataweave 中添加什么特定于 csv 格式的内容吗?
我在我的 dw 写作中尝试过:
%dw 2.0
output application/csv
---
payload
请在下面找到我的 json 输入文件:
{"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"}}
嗨,为了让 DW 编写 CSV,输出 DataStructure 需要是一个对象数组,因此为了做到这一点,您需要将输入的形状转换为一个对象数组,其中对象需要是简单值(字符串|数字|布尔值|日期)
例如,您输入的内容很简单
%dw 2.0
output application/csv
---
flatten(payload.content.file.body.records) map ((record, index) ->
{
alternateHierarchy: record.alternateHierarchy,
tablePath: record.tablePath
}
)
这将输出
alternateHierarchy,tablePath
ACD,/root/account/Account
ACD,/root/account/Account
我正在尝试将 json 转换为 mule 4 dataweave 2.0 中的 csv 格式。但它不起作用。我们应该在 dataweave 中添加什么特定于 csv 格式的内容吗? 我在我的 dw 写作中尝试过:
%dw 2.0
output application/csv
---
payload
请在下面找到我的 json 输入文件:
{"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"}}
嗨,为了让 DW 编写 CSV,输出 DataStructure 需要是一个对象数组,因此为了做到这一点,您需要将输入的形状转换为一个对象数组,其中对象需要是简单值(字符串|数字|布尔值|日期)
例如,您输入的内容很简单
%dw 2.0
output application/csv
---
flatten(payload.content.file.body.records) map ((record, index) ->
{
alternateHierarchy: record.alternateHierarchy,
tablePath: record.tablePath
}
)
这将输出
alternateHierarchy,tablePath
ACD,/root/account/Account
ACD,/root/account/Account