We know that Mule 4 comes with DataWeave as the default expression language. Mule 4 with DataWeave is very powerful. If we need to iterate the list, then we know that we can use <foreach> component. It was available in Mule 3 and it is available in Mule 4 as well. You can refer the this documentation to understand the difference.
We have so many examples for iterating over List, but let’s say we need to iterate over Map entries. Is it possible? Of course it is possible!! Thanks to DataWeave. We can use DataWeave entrySet function in this case. Here is the complete example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" | |
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" | |
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:mspring="http://www.mulesoft.org/schema/mule/spring" | |
xmlns:spring="http://www.springframework.org/schema/beans" | |
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" | |
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd | |
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd | |
http://www.mulesoft.org/schema/mule/spring http://www.mulesoft.org/schema/mule/spring/current/mule-spring.xsd | |
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd"> | |
<http:listener-config name="HTTP_Listener_config"> | |
<http:listener-connection host="localhost" port="8081" /> | |
</http:listener-config> | |
<flow name="foreachDemo"> | |
<http:listener config-ref="HTTP_Listener_config" path="foreach"/> | |
<ee:transform> | |
<ee:message> | |
<ee:set-payload><![CDATA[%dw 2.0 | |
output application/json | |
--- | |
{ | |
id: '12345', | |
firstName: 'FName', | |
lastName: 'LName' | |
} | |
]]> | |
</ee:set-payload> | |
</ee:message> | |
</ee:transform> | |
<logger level="INFO" message="JSON Payload to iterate: #[payload]" /> | |
<foreach collection="#[dw::core::Objects::entrySet(payload)]"> | |
<logger level="INFO" message="Inside foreach: #[payload]" /> | |
</foreach> | |
</flow> | |
</mule> |
Output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO 2018-06-18 09:15:18,833 [[MuleRuntime].cpuLight.07: [dummy].foreachDemo.CPU_LITE @4b36449b] [event: 0-c0aa57b0-737f-11e8-9d25-34363b68f902] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: JSON Payload to iterate: { | |
"id": "12345", | |
"firstName": "FName", | |
"lastName": "LName" | |
} | |
INFO 2018-06-18 09:15:20,082 [[MuleRuntime].cpuLight.07: [dummy].foreachDemo.CPU_LITE @4b36449b] [event: 0-c0aa57b0-737f-11e8-9d25-34363b68f902] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Inside foreach: { | |
"key": "id", | |
"value": "12345", | |
"attributes": null | |
} | |
INFO 2018-06-18 09:15:20,089 [[MuleRuntime].cpuLight.07: [dummy].foreachDemo.CPU_LITE @4b36449b] [event: 0-c0aa57b0-737f-11e8-9d25-34363b68f902] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Inside foreach: { | |
"key": "firstName", | |
"value": "FName", | |
"attributes": null | |
} | |
INFO 2018-06-18 09:15:20,094 [[MuleRuntime].cpuLight.07: [dummy].foreachDemo.CPU_LITE @4b36449b] [event: 0-c0aa57b0-737f-11e8-9d25-34363b68f902] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Inside foreach: { | |
"key": "lastName", | |
"value": "LName", | |
"attributes": null | |
} |