我正在尝试将数组有效负载映射到Dataweave中的CSV中,但无法实现结果。
csv不需要标题,数组中的内容将逐列打印。我面临通过嵌套数组进行映射的问题。
输入有效载荷
[
{
"Invoice": {
"Invoice Number*": "Test",
"Supplier Number": "1201",
"Submit For Approval?": "Yes",
"Invoice Date*": "20190828",
"Line Level Taxation*": "Yes",
"Payment Date": "00/00/0000",
"Original invoice number": "",
"Original invoice date": ""
},
"Invoice Line": [
{
"Invoice Number*": "Test1",
"Line Number": "1",
"Description*": "Test1",
"Price*": "500",
"Quantity": null,
"Unit of Measure*": null,
"PO Number": "001",
"PO Line Number": "1"
},
{
"Invoice Number*": "Test2",
"Line Number": "2",
"Description*": "Test2",
"Price*": "500",
"Quantity": null,
"Unit of Measure*": null,
"PO Number": "001",
"PO Line Number": "2"
}
],
"Invoice Tax Line": [
{
"Tax Amount": "500",
"Invoice Line Number": "1",
"Line Number": "1"
},
{
"Tax Amount": "50",
"Invoice Line Number": "2",
"Line Number": "2"
}
]
}
]
预期产量
column_0, column_1, column_2 ... //no header
"Invoice Number*","Supplier Number","Submit For Approval?"... //Invoice
"Invoice Number*","Line Number*"... //InvoiceLine[0]
"Tax Amount","Invoice Line Number","Line Number"... //Tax Line[0]
"Invoice Number*","Line Number*"... //InvoiceLine[1]
"Tax Amount","Invoice Line Number","Line Number"... //Tax Line[1]
我如何编写dataweave映射以将结果存档,如上?
答案 0 :(得分:4)
这是我为您的用例找到的解决方案。基本上,有两个函数根据类型来分配正确的方法。然后,您还想使用zip函数将一个“发票行”与一个“发票税项”混合在一起,以便正确混合它们。
%dw 2.0
output application/csv headers=false
import * from dw::core::Objects
fun collectKeyNames(obj: {}): Array<{}> =
[
obj
]
fun collectKeyNames(arr: Array): Array<{}> =
arr flatMap ((obj, index) -> collectKeyNames(obj))
---
payload flatMap ((item, index) ->
collectKeyNames(item.Invoice) ++
(collectKeyNames(item."Invoice Line" zip item."Invoice Tax Line"))
)