我需要为输入中的每个对象在json数组中输入两个对象。
我编写了以下代码:
%dw 2.0
output application/csv
var categoryList = ["Business","Authorization"]
---
payload map((payload01,indexOfPayload01)->
categoryList map((payload02,indexOfPayload02)->
{
"Name": payload01.Name,
"Category":payload02,
"Code":"Customer",
"Percentage":null,
"End Tag": "End"
})
)
我的输出是:
[
[
{
"Name": "Desc 1",
"Category": "Business",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
},
{
"Name": "Desc 1",
"Category": "Authorization",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
}
],
[
{
"Name": "Desc 2",
"Category": "Business",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
},
{
"Name": "Desc 2",
"Category": "Authorization",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
}
]
]
但是我希望这些值作为单个json数组。在这里,数据被分为两个对象。我的预期输出:
[
{
"Name": "Desc 1",
"Category": "Business",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
},
{
"Name": "Desc 1",
"Category": "Authorization",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
},
{
"Name": "Desc 2",
"Category": "Business",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
},
{
"Name": "Desc 2",
"Category": "Authorization",
"Code": "Customer",
"Percentage": null,
"End Tag": "END"
}
]
我该如何完成?不用说,我是Mule的新手。
谢谢, 阿努普
答案 0 :(得分:2)
您可以使用我在本示例中显示的flatMap,或将结果展平为示例2
%dw 2.0
output application/csv
var categoryList = ["Business","Authorization"]
---
payload flatMap((payload01,indexOfPayload01)->
categoryList map((payload02,indexOfPayload02)->
{
"Name": payload01.Name,
"Category":payload02,
"Code":"Customer",
"Percentage":null,
"End Tag": "End"
})
)
此示例显示了如何使用Flattn
%dw 2.0
output application/csv
var categoryList = ["Business","Authorization"]
---
flatten(payload map((payload01,indexOfPayload01)->
categoryList map((payload02,indexOfPayload02)->
{
"Name": payload01.Name,
"Category":payload02,
"Code":"Customer",
"Percentage":null,
"End Tag": "End"
})
))