Gulgule
Gulgule

Reputation: 31

ForEach Aggregation

I am using For-Each Processor. As a part of this i need to aggregate the responses of each iteration. For doing this i am using the below expression:

Step 1: Create a flowVar with name 'aggregator' with value '#[[]]'. This is initialised before For-Each Step2: In For-Each Processor once the processing is done. I use the following expression to aggregate: '#[flowVars.aggregation.add(message.payload)]'.

The Payload i get in each iteration is

[{
"Transactions": [
  {
    "Id": "",
    "CompanyId": "123",
    "ContractId": "777"
  },
  {
    "Id": "",
    "CompanyId": "123",
    "ContractId": "777"
  }
]

The result of aggregation is :

[{  
"Transactions": [
{
  "Id": "",
  "CompanyId": "123",
  "ContractId": "777"
},
{
  "Id": "",
  "CompanyId": "123",
  "ContractId": "777"
}
]
}, {
"Transactions": [
{
  "Id": "",
  "CompanyId": "555",
  "ContractId": "2345"
},
{
  "Id": "",
  "CompanyId": "7777",
  "ContractId": "2389"
}
  ]
}]

I want to merge data in single "Transactions" as a JSON Output. I am unable to achieve this.

Upvotes: 0

Views: 442

Answers (2)

Johnson
Johnson

Reputation: 461

Keeping with your current approach you could add the below dataweave after the aggregation:

%dw 1.0
%output application/json
---
{
    Transactions: flatten payload.Transactions
}

If the input to this transform is the output of your aggregation that you described in your question, the output is:

{
  "Transactions": [
    {
      "Id": "",
      "CompanyId": "123",
      "ContractId": "777"
    },
    {
      "Id": "",
      "CompanyId": "123",
      "ContractId": "777"
    },
    {
      "Id": "",
      "CompanyId": "555",
      "ContractId": "2345"
    },
    {
      "Id": "",
      "CompanyId": "7777",
      "ContractId": "2389"
    }
  ]
}

Upvotes: 0

Simrat Kaur
Simrat Kaur

Reputation: 41

You can try using splitter aggregator. When the splitter splits a message, it adds three new outbound variables into each of the output fragments. These three variables are later used by the Aggregator to reassemble the message:

MULE_CORRELATION_GROUP_SIZE: number of fragments into which the original message was split

MULE_CORRELATION_SEQUENCE: position of a fragment within the group

MULE_CORRELATION_ID: single ID for entire group (all output fragments of the same original message share the same value).

When an aggregator receives a single fragment, it knows what group to put it into and how large this group should be. Once all of the fragments have arrived, it passes on the complete group as a single message.'

Can refer mule doc for details.

For example: enter image description here

Upvotes: 1

Related Questions