Map Operator Example in Mule




Map Operator Example : 

Map operator iterates the array collection and read the keys and 
values.

Below is the example where json array is iterated and output is shown below

 
Dataweave code :

%dw 1.0
%output application/json
%function normalize(date) date replace "/" with "-" replace "." with "-"
---
items: payload.info map {

      price: $.price as :number,
      personal_information: $.personal_information - "ssn",
      title @(lang: "en"): $.currency,
      date1: normalize("26-JUL-2019") as :date {format: "d-MMM-yyyy"} as :string {format: "yyyy-MM-dd"},
      email: $.profile.email,
      name: $.profile.firstname
      
}

URL : localhost:8085/api/dataweave
Method : POST


Input :

{

"info" :[{
"price" : 3000,
"personal_information" : {
"firstname" : "Himanshu",
"lastname" : "Pal",
"ssn" : "22"
},
"currency" : "USD",
"availableSeats" : "4",
"profile" :{
"firstname" : "Himanshu",
"email" : "Himanshu@himanshu.com"
}
}

]

}



Output  :

{
    "items": [
        {
            "price": 3000,
            "personal_information": {
                "firstname": "Himanshu",
                "lastname": "Pal"
            },
            "title": "USD",
            "date1": "2019-07-26",
            "email": "Himanshu@himanshu.com",
            "name": "Himanshu"
        }
    ]
}


XML project code : 


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
    <flow name="complexmapFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/dataweave" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/json
%function normalize(date) date replace "/" with "-" replace "." with "-"
---
items: payload.info map {

      price: $.price as :number,
      personal_information: $.personal_information - "ssn",
      title @(lang: "en"): $.currency,
      date1: normalize("26-JUL-2019") as :date {format: "d-MMM-yyyy"} as :string {format: "yyyy-MM-dd"},
      email: $.profile.email,
      name: $.profile.firstname
      
}]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>