首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mule4中ForEach中的聚合

Mule4中ForEach中的聚合
EN

Stack Overflow用户
提问于 2018-10-31 15:27:34
回答 1查看 2.1K关注 0票数 0

我想聚合For Each的每次迭代的响应。在每次迭代中,我都会获得Json数据,并希望将这些Json数据聚合到Json Array/List中。请让我知道如何通过在转换中使用数据编织或任何其他方式来做到这一点?

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

代码语言:javascript
复制
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"
    }
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-31 23:21:08

您可以遵循如下内容

代码语言:javascript
复制
<!-- 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语法。对于Mule4,您可以使用attribute代替variable来存储记录。除了将Dataweave语言版本从1.0更改为2.0外,Dataweave语法将保持不变。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53078298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档