Thiru
Thiru

Reputation: 424

How to iterate a map using Foreach in mule?

I'm giving "MAP" as input to foreach :

{Id=1, Sum=10, Name=Jon1, Level=1}, 
{Id=2, Sum=20, Name=Jon2, Level=1}, 
{Id=3, Sum=30, Name=Jon3, Level=1}...................,

Based on "Sum" value I need to send each record into two different files. Where I struck is I don't know how to write these conditional statements in foreach, when and logger statements where I kept question mark.

 <foreach  doc:name="For Each" collection="?????????????????">  
 <choice doc:name="Choice">
 <when expression="???????????&lt;=30">
 </when>
 <otherwise>
 <data-mapper:transform doc:name="DataMapper"/>
 <logger message="default logger "?????????" level="INFO"doc:name="Logger"/>
 </otherwise>
 </choice>        
 </foreach>

please suggest me on this and comment if you know how to write conditional statements if "CSV" is input.I'm new to mule ., Thanks.,

Upvotes: 1

Views: 2915

Answers (2)

Sudarshan
Sudarshan

Reputation: 8664

I would link a previous answer of mine, the essence being do not try to code procedural logic into flow, use components for that kind of stuff.

But if you are trying to learn then @Ryan answer is on the mark.

Upvotes: 0

Ryan Carter
Ryan Carter

Reputation: 11606

Mule uses MEL based on MVEL as an expression language. It allows you to use the dot syntax to navigate Maps and POJOs etc. or standard Java method invocation:

#[message.payload.get('Sum')]

#[message.payload.Sum]

The foreach will automatically default to the message payload, if you do not provide a collection expresssion. If your payload is a Collection then it should be fine. It looks like your payload is a Collection of maps, so you should be able to use:

<foreach  doc:name="For Each" collection="#[message.payload]">  
 <choice doc:name="Choice">
 <when expression="#[message.payload.Sum &lt; 30]">
 </when>
 <otherwise>
 <logger message="#[message.payload.Sum]" level="INFO"doc:name="Logger"/>
 </otherwise>
 </choice>        
 </foreach>

If you want to iterate different entries in a SINGLE map you can use the following:

<foreach collection="#[message.payload.entrySet()]">
  ...
</foreach>

Upvotes: 1

Related Questions