【问题标题】:Aggregation in ForEach in Mule 4Mule 4 中的 ForEach 聚合
【发布时间】:2018-10-31 07:27:34
【问题描述】:

我想汇总 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"
    }
  ]
}

【问题讨论】:

标签: mule dataweave


【解决方案1】:

你可以关注类似的东西

<!-- 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 语法。对于 mule 4,您可以使用 attribute 而不是 variable 来存储记录。除了将 Dataweave 语言版本从 1.0 更改为 2.0 之外,Dataweave 语法将保持不变。

希望这会有所帮助。

【讨论】:

  • 在 Mule 4 中,您应该使用变量 (vars.x),而不是属性。
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
相关资源
最近更新 更多