Skip to content

ForEach Mediator

The ForEach Mediator processes a collection, such as a JSON array or XML list derived from the message body or a defined variable, by splitting it into multiple messages, each corresponding to an item in the collection. The message in each iteration is flowing through the specified mediation sequence. After flowing through the mediation sequence, the sub-messages in each iteration are merged back to the corresponding original parent collection in the original message body or variable.

Syntax

<foreach collection="expression" parallel-execution=(true | false) continue-without-aggregation=(true | false) update-original=(true | false) target-variable=(string) result-content-type="JSON | XML" result-enclosing-element=(string) counter-variable=(string) >
    <sequence>
        (mediator)+
    </sequence>+
</foreach>

Configuration

The parameters available to configure the ForEach mediator are as follows.

General configurations

Parameter Name Description
Collection to Iterate This parameter specifies the collection to be processed by the ForEach mediator. You need to provide an expression that points to a collection within the message body or a variable. The collection can be extracted as follows depending on the content type.
  • JSON : ${payload.items}
  • XML : ${xpath('//data/list')}
  • Variable : ${var.myCollection}
Execute Parallel Specifies whether the messages should be processed in parallel.
  • True (default): Executes the messages in parallel.
  • False : Executes the messages sequentially.
Continue without aggregation Specifies whether the parent flow should continue without waiting for the aggregation.
  • True : Continue the parent flow without waiting for the aggregation.
  • False (default): Wait till aggregation completes and continue the flow.

Output configurations

Parameter Name Description
Update Original Collection If enabled, the original list will be updated with the content. The content type should match the original collection type.
Variable Name The name of the variable where the new content will be saved. This parameter is required if Update Original Collection is disabled.
Variable Type The type of the variable where the new content will be saved. Supported values:
  • JSON (default)
  • XML
This parameter is required if Update Original Collection is disabled.
Result Enclosing Element Name Specifies the name of the root element wrapping the aggregation result. Applicable only when Variable Type is XML.

Advanced configurations

Parameter Name Description
Counter Variable Name You can access the current iteration number using this variable within the sequence used in the ForEach mediator. This option is available only when Parallel Execution is disabled.

Examples

Example 1 - Iterating over an XML list derived from the message body and updating the original collection

<foreach collection="${xpath('//data/list')}" parallel-execution="true" update-original="true" continue-without-aggregation="false">
    <sequence>
        <payloadFactory media-type="xml">
            <format>
                <person xmlns="">
                    <surname>${xpath('//list/name/text()')}</surname>
                    <age>10</age>
                </person>
            </format>
        </payloadFactory>
        <call>
            <endpoint>
                <http method="post" uri-template="http://localhost:5454/api/transform" />
            </endpoint>
        </call>
    </sequence>
</foreach>

Example 2 - Iterating over a JSON array derived from the message body and setting the new content to a variable

<foreach collection="${payload.data.list}" parallel-execution="true" update-original="false" target-variable="processedList" result-content-type="JSON" continue-without-aggregation="false">
    <sequence>
        <log category="INFO">
            <message>Processing message : ${payload}</message>
        </log>
        <payloadFactory media-type="json">
            <format><![CDATA[{
                "_name": ${payload.name},
                "age": 5
                }]]>
                </format>
        </payloadFactory>
        <call>
            <endpoint>
                <http method="post" uri-template="http://localhost:5454/api/transform" />
            </endpoint>
        </call>
    </sequence>
</foreach>

Example 2 - Iterating over a JSON array derived from a variable

<foreach collection="${var.list}" parallel-execution="true" update-original="true" continue-without-aggregation="false">
    <sequence>
        <log category="INFO">
            <message>Processing message : ${payload}</message>
        </log>
        <payloadFactory media-type="json">
            <format><![CDATA[{
                "_name": ${payload.name},
                "age": 5
                }]]>
                </format>
        </payloadFactory>
        <call>
            <endpoint>
                <http method="post" uri-template="http://localhost:5454/api/transform" />
            </endpoint>
        </call>
    </sequence>
</foreach>