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
.
Following are some key points regarding For Each scope
<?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>
Add a logger which will print the received payload
Now add For Each activity
Where following parameters are passed
In our case – payload.sale will hold the collection
Add logger to print each payload
Run the application to see the results
First logger – will print the actual input payload
INFO 2021-12-29 12:10:12,090 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<sale>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
</sale>
INFO 2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
</item>
For Each Logger (First iteration)
INFO 2021-12-29 12:10:12,103 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="2121" sale="31123">
<shipping>23231</shipping>
<desc>ok</desc>
<price>12</price>
<quantity>12</quantity>
For Each Logger (Second iteration)
INFO 2021-12-29 12:10:12,111 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="3202" saleId="11111">
<shipping>11111</shipping>
<desc>ok</desc>
<price>08</price>
<quantity>08</quantity>
</item>
For Each Logger (Third iteration)
INFO 2021-12-29 12:10:12,120 [[MuleRuntime].uber.07: [foreach].foreachFlow.CPU_LITE @4af6527c] [processor: foreachFlow/processors/1/processors/0; event: 2c52ff40-6872-11ec-a16b-00090faa0001] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: <?xml version='1.0' encoding='UTF-8'?>
<item itemId="5635" saleId="99999">
<shipping>99999</shipping>
<desc>ok</desc>
<price>333</price>
<quantity>33</quantity>
</item>
Use the Transform message in For Each Scope to convert XML to json into the Anypoint Studio Console.
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…