For Each Scope in Mule

The For Each scope splits a payload into elements and processes them one by one through the components that you place in the scope. It is similar to a for-each/for loop code block in most programming languages and can process any collection, including lists and arrays. The collection can be any supported content type, such as application/json, application/java, or application/xml.

General considerations about the For Each scope:

  • By default, For Each tries to split the payload. If the payload is a simple Java collection, the For Each scope can split it without any configuration. The payload inside the For Each scope is each of the split elements. Attributes within the original message are ignored because they are related to the entire message.
  • For Each does not modify the current payload. The output payload is the same as the input.
  • For non-Java collections, such as XML or JSON, use a DateWeave expression to split data. Use the Collection field for this purpose.



[{ “id”:1, “role”:”Application Developer” }, {“id”:3,”role”:”Application Manager”}]


Code :

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw=""
xmlns:db="" xmlns:http=""
xmlns:ee="" xmlns:cxf=""
xmlns="" xmlns:doc=""
xmlns:spring="" xmlns:xsi=""

<http:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8081"  doc:name="HTTP Listener Configuration" />
<flow name="forEachFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/foreach" doc:name="HTTP" />
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
        <logger message="#[payload]" level="INFO" doc:name="Logger"/> 
  <foreach doc:name="For Each Loop" >
  <logger level="INFO" message="#[payload]" doc:name="Logger" />
<object-to-string-transformer doc:name="Object to String"/>
<set-payload value="#[payload]"  doc:name="Set Payload"/>
<logger level="INFO" message="#[payload]" doc:name="Logger" />

Thank you for reading!