0

我需要为输入中的每个对象在 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 的新手。

谢谢,阿努普

4

1 回答 1

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"
    })
))
于 2019-05-14T12:56:24.093 回答