Complex Dataweave Example in Mule





Complex Dataweave Example in Mule

Here is an dataweave example including some operators, selectors and other dataweave elements.

flow :


Dataweave code :




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

Input :

{
"num" : 12,
"name" : "Himanshu",
"age" : 19,
"city" : "Pune",
"employee" :{
"designation" : "Manager",
"department" : "IT"

}

}
}

Output :

{
    "value": 22,
    "variablevalue": 20,
    "name": true,
    "age": "higher",
    "city": "Pune",
    "currenttimestamp": "2019-11-21T19:28:56.907+05:30",
    "day": 21,
    "minutes": 28,
    "random": 7.393803705049566,
    "log": "Himanshu",
    "payload": {
        "num": 12,
        "name": "Himanshu",
        "age": 19,
        "city": "Pune",
        "employee": {
            "designation": "Manager",
            "department": "IT"
        }
    },
    "Department": "Manager",
    "firstElement": "M",
    "slice": [
        0,
        1
    ],
    "last": [
        2,
        1,
        0
    ],
    "header": null,
    "biggerThanTwo": [
        5
    ]
}

XML project code :


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

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 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="test_dataweaveFlow">
        <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
%var x = 20
---
{
 value : payload.num + 10,
 variablevalue : x, // variable access in dataweave
 name : upper payload.name, //upper case in mulesoft
 name : payload.name when payload.name == 'Himanshu' otherwise "name is not Himanshu",
 age : payload.age unless payload.age < 18 otherwise "age is less than 18",
 city : payload.city default Mumbai,
 name: payload.name match {  //string matching in dataweave
   str: "Himanshu" -> { "matches": true, value: payload.name }},
 age: payload.age match {
    age when age < 18 -> "lower",  //number pattern matching
    age when age > 18 -> "higher"
  },
  currenttimestamp: now,
  day: now.day,
  minutes: now.minutes, 
  random: random*10, //returns random number between 0 to 1 of type number
  log : log('Himanshu'),
  payload : payload,
  Department : payload.employee.designation,
  firstElement : "Mulesoft"[0],
  slice: [0,1,2][0 to 1],
  last: [0,1,2][-1 to 0],
  name: payload.name?,
  header: inboundProperties['userName'], //read header
  biggerThanTwo: [0, 1, 2, 3, 4, 5] filter $ > 4  
}]]></dw:set-payload>
        </dw:transform-message>
    </flow>
</mule>