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

Top Operators in Dataweave in Mule | Dataweave Operators

  Top Operators in Dataweave in Mule | Dataweave Operators




1) Map Object


Similar to Map, but instead of processing only the values of an object, it processes both keys and values as a tuple. Also instead of returning an array with the results of processing these values through the lambda, it returns an object, which consists of a list of the key:value pairs that result from processing both key and value of the object through the lambda.

The lambda is invoked with two parameters: key and the value. If these parameters are not named, the key is defined by default as $$ and the value as $.

Dataweave code : 

%dw 1.0
%output application/json
%var conversionRate=22
---
priceList: payload.prices mapObject (
  '$$':{
    rupees: $,
    localCurrency: $ * conversionRate
  }
)

Input : 

<prices>
    <delta>10</delta>
    <beta>20</beta>
    <alpha>50</alpha>
</prices>

Output : 

{
    "priceList": {
        "delta": {
            "rupees": "10",
            "localCurrency": 220
        },
        "beta": {
            "rupees": "20",
            "localCurrency": 440
        },
        "alpha": {
            "rupees": "50",
            "localCurrency": 1100
        }
    }
}

2) Pluck

Pluck is useful for mapping an object into an array. Pluck is an alternate mapping mechanism to mapObject,but instead of returning an object, it returns an array, which may be built from either the values or the keys in the object.

Dataweave code :

%dw 1.0
%output application/json
---
price: {
  keys: payload.prices pluck $$,
  values: payload.prices pluck $
}

Input : 

<prices>
    <delta>10</delta>
    <beta>20</beta>
    <alpha>50</alpha>
</prices>

Output :

{
    "price": {
        "keys": [
            "delta",
            "beta",
            "alpha"
        ],
        "values": [
            "10",
            "20",
            "50"
        ]
    }
}

3) Filter

Using Filter on an Array

It filters the array on the basis of $(value)

Dataweave code :

%dw 1.0
%output application/json
---
{
  biggerThanThree: [0, 1, 2, 3, 4, 5] filter $ > 3
}

Output :
{
  "biggerThanThree": [4,5]
}

Using Filter on an Object

Returns an object with the key:value pairs that pass the acceptance criteria defined in the lambda. If these parameters are not named, the index is defined by default as $$ and the value as $.

Dataweave code :

%dw 1.0
%output application/xml
---
filtered: {
  aa: "a", bb: "b", cc: "c", dd: "d"
} filter $ == "d" (1)
1 Filters the all key:value pairs with value "d" ⇒ {dd:d}

Output

<?xml version="1.0" encoding="UTF-8"?>
<filtered>
  <dd>d</dd>
</filtered>


4) Remove
Using Remove on an Array
When running it on an array, it returns another array where the specified indexes are removed.

Dataweave code :

%dw 1.0
%output application/json
---
{
  value: ["x", "y", "z"] - 1
}

Output
{
  "value": [x, z]
}


Using Remove on an Object


When running it on an object, it returns another object where the specified keys are removed.

Dataweave code :

%dw 1.0
%output application/json
---
myObject: {aa: "a", bb: "b"} - "aa"

Output
{
  "myObject": {
    "bb": "b"
  }
}

Remove by Matching Key and Value

Works just like remove on objects, but only removes an element when there is a match of not just the key but of the key + value pair . It returns another object where the specified keys are removed.

Dataweave code :

%dw 1.0
%output application/json
---
myObject: {aa: "a", aa:"b", bb: "c"} -- { aa:"a"}

Output
{
  "myObject": {
    "aa": "b",
    "bb": "c"
  }
}


5) AND
The expression and (in lower case) can be used to link multiple conditions, its use means that all of the linked conditions must evaluate to true for the expression as a whole to evaluate to true.

Dataweave code :

%dw 1.0
%output application/json
---
{
  Eligibility: "eligible for vote"
} when payload.age > 18 and payload.nationality == "Indian"
otherwise
{
      Eligibility: "Not eligible"
}

Input : 
{
"age" : 23,
"nationality" : "Indian"
}

Output :

{
    "Eligibility": "eligible for vote"
}

6) OR

The expression or (in lowercase) can be used to link multiple conditions. Its use means that either one or all of the linked conditions must evaluate to true for the expression as a whole to evaluate to true. This example combines the usage of OR with the when and otherwise expressions.

Dataweave code :

%dw 1.0
%output application/json
---
{
  IndianCities: "city of india"
} when payload.city == "Delhi" or payload.city == "Mumbai"
otherwise
{
      IndianCities: "Not an indian city"
}

Input : 

{
"city" : "Mumbai"
}

Output :

{
    "IndianCities": "Not an indian city"
}

7) IS

Evaluates if a condition validates to true and returns a boolean value. Conditions may include and and or operators.

Dataweave code :

%dw 1.0
%output application/json
---
ROOT: payload.city mapObject (
  city:{
    IndianCity: $ is :string
  }
)

Input : 

{
"city" : "Mumbai"
}

Output :

{
    "ROOT": {
        "city": {
            "IndianCity": true
        }
    }
}


8) Concat

The concat operator is defined using ++ sign. You must have spaces on both sides of them.

Using Concat on an Array

When using arrays, it returns the resulting array of concatenating two existing arrays.

Dataweave code :

%dw 1.0
%output application/json
---
{
  c: [0, 1, 2] ++ [6, 7, 8]
}

Output
{
  "c": [0, 1, 2, 6, 7, 8]
}

Using Concat on a String

Strings are treated as arrays of characters, so the operation works just the same with strings.

Dataweave code :

%dw 1.0
%output application/json
---
{
  name: "Mule" ++ "Soft"
}

Output
{
  "name": MuleSoft
}
  
Using Concat on an Object

Returns the resulting object of concatenating two existing objects.

Dataweave code :

%dw 1.0
%output application/xml
---
concat: {aa: "a"} ++ {cc: "c"}

Output

<?xml version="1.0" encoding="UTF-8"?>
<concat>
  <aa>a</aa>
  <cc>c</cc>
</concat>  
  
9) Contains
Evaluates if an array or list contains in at least one of its indexes a value that validates to true and returns a boolean value. You can search for a literal value, or match a regex too.

Using Contains on an Array

You can evaluate if any value in an array matches a given condition:

Dataweave code :

%dw 1.0
%output application/json
---
{
ContainsCity: payload.city contains "Delhi"
}

Input :

[{
"city" : "Mumbai"
},
{
"city" : "Delhi"
},
{
"city" : "Pune"
}]

Output :

{
    "ContainsCity": true
}


Using Contains on a String

You can also use contains to evaluate a substring from a larger string:

Dataweave code :

%dw 1.0
%output application/json
---
{
ContainsCity: payload.city contains "Mu"
}

Input :

{
"city" : "Mumbai"
}

Output :

{
    "ContainsCity": true
}

10) Type Of

Returns the type of a provided element (eg: '":string"' , '":number"' )

Dataweave code :

%dw 1.0
%output application/json
---
isString: typeOf payload.mystring

Input: 

{
  "mystring":"Mumbai"
}
Output:

{
  "isString": ":string"
}

11) Flatten

If you have an array of arrays, this function can flatten it into a single simple array.

Dataweave code :

%dw 1.0
%output application/json
---
flatten payload

Input:

[
   [3,5],
   [9,5],
   [100,2.5]
]

Output:

[
  3,
  5,
  9,
  5,
  100,
  2.5
]

12) Size Of

Returns the number of elements in an array (or anything that can be converted to an array such as a string).

Dataweave code :

%dw 1.0
%output application/json
---
{
  arraySize: sizeOf [7,8,9],
  textSize: sizeOf "Himanshu",
  objectSize: sizeOf {a:1,b:2}
}
Output
{
  "arraySize": 3,
  "textSize": 8,
  "objectSize": 2
}

13) Group By

Partitions an array into a Object that contains Arrays, according to the discriminator lambda you define. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $.

Dataweave code :

%dw 1.0
%output application/json
---
"language": payload.info groupBy $.city

Input : 

{
"info" : [{  

"name" : "Kapil",
"city" : "Mumbai"
},
{   "name" : "Satish",
"city" : "Delhi"
},
{   "name" : "Mahesh",
"city" : "Mumbai"
}]
}

Output :

{
    "language": {
        "Mumbai": [
            {
                "name": "Kapil",
                "city": "Mumbai"
            },
            {
                "name": "Mahesh",
                "city": "Mumbai"
            }
        ],
        "Delhi": [
            {
                "name": "Satish",
                "city": "Delhi"
            }
        ]
    }

}

14) Distinct By

Returns only unique values from an array that may have duplicates. The lambda is invoked with two parameters: index and value. If these parameters are not defined, the index is defined by default as $$ and the value as $.

Dataweave code :

%dw 1.0
%output application/json
---
"language": payload.info distinctBy $.city

Input :

{
"info" : [{  

"name" : "Kapil",
"city" : "Mumbai"
},
{   "name" : "Satish",
"city" : "Delhi"
},
{   "name" : "Mahesh",
"city" : "Mumbai"
}]
}

Output :

{
    "language": [
        {
            "name": "Kapil",
            "city": "Mumbai"
        },
        {
            "name": "Satish",
            "city": "Delhi"
        }
    ]

}

Some operators are shown below in dataweave code :

%dw 1.0
%output application/json
---
{
aa: [0, 1, 2] + 5,   //arraypush
a: [0, 1, 1, 2] - 1,   //remove from array
     b: [{a: "a"}] - {a: "a"}, //remove an object
     a: [0, 1, 1, 2] -- [1,2],   //removes matching from array
      a: avg [1..500],  //average of array
      b: avg [1, 2, 9],  //avaerage of array
      aa: ["a","b","c"] joinBy "-", //join by -
      split: "a-b-c" splitBy "-",   //splity by
      orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy $.letter,   // order by
      replace: "admin123" replace /(\d+)/ with "ID", //replace a string with another string
      matches: "admin123" matches /(\d+)/,  //matches a string with given string
       "trim": trim "   my long text     ",  //trim a string
       "substring": "abcdefg"[0..4],  //substring
        absolute: abs 2.5      

}

Some string operators like starts with, ends with,upper,lower,etc are also used. Math operators like min,max,avg,ceil,etc are also available that you can find on mulesoft documentation page.


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