Mule 4中ForEach中的聚合

时间:2018-10-31 07:27:34

标签: mule dataweave

我想汇总For Each每次迭代的响应。在每次迭代中,我都会获取Json数据,并且想要将该Json数据聚合到Json Array / List中。请让我知道如何通过在Transform中使用数据编织或任何其他方式来做到这一点吗?

下面是我在For Each的每次迭代中获取的示例Json数据。

1st iteration:
{
    "accountId": "12345",
    "accountNumber": "999",
    "accountTitle": "ABC"
}

2nd iteration:
{
    "accountId": "98765",
    "accountNumber": "888",
    "accountTitle": "XYZ"
}

I want final aggregated output as below.  
{
  accountList: [
    {
        "accountId": "12345",
        "accountNumber": "999",
        "accountTitle": "ABC"
    },
    {
        "accountId": "98765",
        "accountNumber": "888",
        "accountTitle": "XYZ"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您可以遵循

<!-- Define blank arraylist for aggregation -->
        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="result"><![CDATA[%dw 1.0
%output application/java
---
[]]]></dw:set-variable>
        </dw:transform-message>
        <foreach doc:name="For Each">
            <!-- generate response using flow logic and add following step for updating result array -->
            <dw:transform-message doc:name="Transform Message">
                <dw:set-variable variableName="result"><![CDATA[%dw 1.0
%output application/java
---
flowVars.result ++ [payload]]]></dw:set-variable>
            </dw:transform-message>
        </foreach>
        <!-- Transform aggregated result to any format. I used JSON -->
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
accountList : flowVars.result]]></dw:set-payload>
        </dw:transform-message>

我使用了Mule 3.x语法。对于m子4,可以使用attribute代替variable来存储记录。除了将Dataweave语言版本从1.0更改为2.0之外,Dataweave语法将保持不变。

希望这会有所帮助。