97008 48403
+91 40-27177600

Blog

How to iterate array inside an array in Mule ESB?

single-blog

From the technical center of excellence of Massil Technologies, our technology leaders make it easy to understand the complex situations you come across in Mule ESB from the experience of Massil Technologies to have countered them in their experience of working on client projects in real time. This blog talks about how to Iterate array inside an array in Mule ESB. This blog is a part of series of blogs being authored and published by Massil Technologies for the benefit of the IT community globally.

While working on a project, we had to work on to repeat an array inside a different array and attempting to generate an array list.

We have the list input below:

  1. <flow name=”test”>
  2. <set-variable variableName=”A” value=”#[[‘1′,’2’]]” doc:name=”Variable” />
  3. <set-variable variableName=”B” value=”#[[‘1′,’2′,’3’]]” doc:name=”Variable” />
  4. <dw:transform-message doc:name=”Transform Message”>
  5. <dw:set-payload><![CDATA[%dw 1.0
  6. %output application/java
  7. A map   ((a,aIndex) -> {
  8. (flowVars.B map ((b,bIndex) ->{
  9. aIndex: aIndex,
  10. bIndex: bIndex
  11. }))
  12. })]]></dw:set-payload>
  13. </dw:transform-message>
  14. <logger level=”INFO” message=”#[payload]”></logger>
  15. </flow>

The expected output is shown below:

  1. [
  2. {aIndex=0, bIndex=0},
  3. {aIndex=0, bIndex=1},
  4. {aIndex=0, bIndex=2},
  5. {aIndex=1, bIndex=0},
  6. {aIndex=1, bIndex=1},
  7. {aIndex=1, bIndex=2}
  8. ]

Despite of running in Data-weave , the output is as following:

  1. [{aIndex=0, bIndex=2}, {aIndex=1, bIndex=2}]

The above output appears as Data-weave is substituting the inner array value with other value.  The solution is depicted below:

Use the below Xml Flow:

<?xml version=“1.0” encoding=“UTF-8”?>

<mule xmlns:json=“http://www.mulesoft.org/schema/mule/json” xmlns:http=“http://www.mulesoft.org/schema/mule/http” xmlns:dw=“http://www.mulesoft.org/schema/mule/ee/dw”

xmlns=“http://www.mulesoft.org/schema/mule/core” xmlns:doc=“http://www.mulesoft.org/schema/mule/documentation”

xmlns:spring=“http://www.springframework.org/schema/beans”

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.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.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.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd”>

<http:listener-config name=“HTTP_Listener_Configuration” host=“0.0.0.0” port=“8081” doc:name=“HTTP Listener Configuration”/>

<flow name=“test”>

<http:listener config-ref=“HTTP_Listener_Configuration” path=“/arraylist” allowedMethods=“GET” doc:name=“HTTP”/>

<set-variable variableName=“A” value=“#[[‘1′,’2’]]” doc:name=“Variable” />

<set-variable variableName=“B” value=“#[[‘1′,’2′,’3’]]” doc:name=“Variable” />

<dw:transform-message doc:name=“Transform Message”>

<dw:set-payload><![CDATA[   %dw 1.0

%output application/java

flatten (flowVars.A map ((a, aIndex) ->

flowVars.B map ((b, bIndex) -> {

aIndex: aIndex,

bIndex: bIndex

})

))

]]></dw:set-payload>

</dw:transform-message>

<logger level=“INFO” message=“#[payload]” doc:name=“Logger”></logger>

<json:object-to-json-transformer doc:name=“Object to JSON”/></flow>

</mule>

Dataweave script:

%dw 1.0

%output application/java

flatten (flowVars.A map ((a, aIndex) ->

flowVars.B map ((b, bIndex) -> {

aIndex: aIndex,

bIndex: bIndex

})

))

 

Expected output:

  1. [
  1. {aIndex=0, bIndex=0},
  2. {aIndex=0, bIndex=1},
  3. {aIndex=0, bIndex=2},
  4. {aIndex=1, bIndex=0},
  5. {aIndex=1, bIndex=1},
  6. {aIndex=1, bIndex=2}
  7. ]

 

RESULT:

[

{

“aIndex”: 0,

“bIndex”: 0

},

{

“aIndex”: 0,

“bIndex”: 1

},

{

“aIndex”: 0,

“bIndex”: 2

},

{

“aIndex”: 1,

“bIndex”: 0

},

{

“aIndex”: 1,

“bIndex”: 1

},

{

“aIndex”: 1,

“bIndex”: 2

}

]

Hope this article from Massil Technologies was useful for you. If you have any further queries on this topic, please reach out to info@massil.tech.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Get in touch with us

Scroll to top