Foreach Scope: How to extract the response from each data collection?

Foreach Scope: How to extract the response from each data collection?

Typically, within the foreach scope, data processing occurs in a meticulously sequential manner. With each iteration over an array, this robust method meticulously handles one record or item at a time, ensuring precision and accuracy in the processing workflow.

In each iteration of the foreach loop, it overrides the responses from the previous iteration. To collect data from each iteration, declare a collection variable before the foreach loop. Inside the loop, use the same variable to append the current data with the concatenation operation.

How It Works:

The powerful function of ‘foreach’ simplifies the intricate process of splitting and aggregating message collections with unparalleled ease and efficiency. It effortlessly breaks down a payload into individual elements, systematically processing each one through the designated components, ensuring a seamless and organized workflow from start to finish.

Within the realm of programming, it is important to note that the Foreach scope maintains the type of the message collection as well as preserving the integrity of its original contents. By meticulously processing each item within the collection individually, the For Each scope ensures a comprehensive and thorough examination of every element present.

You can also use batch scope in a for-each loop for quicker processing. For instance, if you have a collection and set the Batch Size to 1, the entire collection processes in one batch in the for-each loop.  

In this compelling example, MuleSoft demonstrates its prowess by utilizing a Foreach scope to elegantly extract and iteratively process data from a file connector. This showcases the sophisticated capabilities of MuleSoft in handling data manipulation tasks efficiently and effectively, making it a powerful tool for seamless integration processes within any organization.

Flow:

Testing the API with the postman

Response:

[

  {

    “employeeInfo”: {

      “name”: “John Doe”,

      “dob”: “1990-03-19”,

      “ssn”: “234-23-525”,

      “address”: “California”,

      “phone”: “8770586755”,

      “email”: “[email protected]”,

      “doctor”: “thomas collins”,

      “hospital_id”: “grandoaks”,

      “hospital”: “grand oak community hospital”,

      “cardNo”: “7844481124110331”,

      “appointment_date”: “2025-04-02”

    },

    “companyInfo”: {

      “Comapany”: “Massil”,

      “Technology”: “Mulesoft”

    }

  },

  {

    “employeeInfo”: {

      “name”: “John Doe”,

      “dob”: “1990-03-19”,

      “ssn”: “234-23-525”,

      “address”: “California”,

      “phone”: “8770586755”,

      “email”: “[email protected]”,

      “doctor”: “thomas collins”,

      “hospital_id”: “grandoaks”,

      “hospital”: “grand oak community hospital”,

      “cardNo”: “7844481124110331”,

      “appointment_date”: “2025-04-02”

    },

    “companyInfo”: {

      “Comapany”: “Massil”,

      “Technology”: “Mulesoft”

    }

  },

  {

    “employeeInfo”: {

      “name”: “John Doe”,

      “dob”: “1990-03-19”,

      “ssn”: “234-23-525”,

      “address”: “California”,

      “phone”: “8770586755”,

      “email”: “[email protected]”,

      “doctor”: “thomas collins”,

      “hospital_id”: “grandoaks”,

      “hospital”: “grand oak community hospital”,

      “cardNo”: “7844481124110331”,

      “appointment_date”: “2025-04-02”

    },

    “companyInfo”: {

      “Comapany”: “Massil”,

      “Technology”: “Mulesoft”

    }

  }

]

The blog addresses one of the scenarios in MuleSoft’s Foreach Scope. For more information or queries on MuleSoft, write to us at [email protected].

Connect With Us

Connect with us
Scroll to Top