For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

Dataweave 1.0 in Mule | Variables | Functions | Data Types

  Dataweave 1.0 in Mule | Variables | Functions | Data Types





Dataweave 1.0 in Mule

Defining variable in dataweave : 

%dw 1.0
%var rate=5
%output application/json
---
{
variable value : rate
currency: payload.price * rate
}

Defining function in dataweave : 

%dw 1.0
%output application/json
%function name(user){firstName: payload.name}
---
{
 user: name(payload)
}

Data types in mule : 

Simple values can be of the following types:

String : Double quoted ("Hello") or Single quoted ('Hello')

Boolean : Literals true or false

Number : Decimal and Integer values are supported (ex: 2.0)

Date : IS0-8601 enclosed by "|" (ex:|2003-10-01T23:57:59Z|)

Regex : Regex expression enclosed by "/" (ex:/(\d+)-(\d+)/)


Adding data to an existing input payload : 

input : 

{
"id" : 1
}

%dw 1.0
%output application/json

---
{
"id" : payload.id + 3
}


Flow Control Expressions :

1)When Otherwise

The keyword when conditionally evaluates a part of your DataWeave code, depending on if an expression evaluates to true or to false. You can make a single line conditional, or enclose a whole section in curly brackets. In case the when expression evaluates to false, its corresponding part of the code is ignored, and the code that corresponds to the otherwise expression is executed.

%dw 1.0
%output application/json

---
{
"id" : payload when payload.id > 2 otherwise null
}

2) Unless Otherwise

The keyword unless conditionally evaluates a part of your DataWeave code, depending on if an expression evaluates to true or to false. You can make a single line conditional, or enclose a whole section in curly brackets. In case the unless expression evaluates to true, its corresponding part of the code is ignored, and the code that corresponds to the otherwise expression is executed.

%dw 1.0
%output application/json

---
{
"id" : payload unless payload.id > 2 otherwise null
}

3) Default

Assigns a default value in case no value is found in the input field.

%dw 1.0
%output application/json

---
{
"id" : payload.id default 2
}

Matching Patterns in Mule 

1) Expression Pattern

Matches when running a certain expression over the evaluated value returns true.

Transform
%dw 1.0
%output application/json
---
{
  a: payload.string match {
    str when str == "Mariano" -> str ++ " de Achaval",
    str when str == "Emiliano" -> str ++ " Lesende"
  },
  b: payload.number match {
    n when n < 3 -> "lower",
    n when n > 3 -> "higher"
  }
}

Input: JSON
Input
{
  "string": "Emiliano",
  "number": 3.14
}
Output: JSON
Output
{
  "a": "Emiliano Lesende",
  "b": "higher"
}

2) Match Type
Matches when the evaluated value is of the specified type

Transform
%dw 1.0
%output application/json
---
{
  a: payload.a match {
    :object -> "OBJECT",
    :string -> "STRING",
    :number -> "NUMBER",
    :boolean -> "BOOLEAN",
    :array -> "ARRAY",
    :null -> "NULL"
  },
  b: payload.b match {
    y is :object -> { type: "OBJECT", y: y },
    y is :string -> { type: "STRING", y: y },
    y is :number -> { type: "NUMBER", y: y },
    y is :boolean -> { type: "BOOLEAN", y: y },
    y is :array -> { type: "ARRAY", y: y },
    y is :null -> { type: "NULL", y: y }
  }
}
Input: JSON
Input
{
  "a": "Emiliano",
  "b": 3.14
}
Output: JSON
Output
{
  "a": "STRING",
  "b": {
    "type": "NUMBER",
    "y": 3.14
  }
}

3) Match Regex
Matches when the evaluated value fits a given regular expression

Transform
%dw 1.0
%output application/json
---
{
  a: payload.phones map ($ match {
     /\+(\d+)\s\((\d+)\)\s(\d+\-\d+)/ -> { country: $[0], area: $[1], number: $[2] },
     /\((\d+)\)\s(\d+\-\d+)/ -> { area: $[1], number: $[2] }
   }),
 b: payload.phones map ($ match {
   phone: /\+(\d+)\s\((\d+)\)\s(\d+\-\d+)/ -> { country: phone[0], area: phone[1], number: phone[2] },
   phone: /\((\d+)\)\s(\d+\-\d+)/ -> { area: phone[1], number: phone[2] }
 })
}
Input: JSON
Input
{
  "phones": [
    "+1 (415) 229-2009",
    "(647) 456-7008"
  ]
}
Output: JSON
Output
{
  "a": [
    {
      "country": "+1 (415) 229-2009",
      "area": "1",
      "number": "415"
    },
    {
      "area": "647",
      "number": "456-7008"
    }
  ],
  "b": [
    {
      "country": "+1 (415) 229-2009",
      "area": "1",
      "number": "415"
    },
    {
      "area": "647",
      "number": "456-7008"
    }
  ]
}

System Values in Mule

1) Now
Returns the present moment in (:datetime) type.

Transform
%dw 1.0
%output application/json
---
{
  a: now,
  b: now.day,
  c: now.minutes
}

2) Random
Returns a random number of type (:number) between 0 and 1

Transform
%dw 1.0
%output application/json
---
{
  price: random * 1000
}


3) Log
(prefix :string,value :any)
Returns the specified value and also logs the value in the DataWeave representation with the specified prefix.

Transform
%dw 1.0
%output application/json
---
{
  result: log("Logging the array",[1,2,3,4])
}
Output
{
  "result": [1,2,3,4]
}
Logger output :

Logging the array [1,2,3,4]




Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER