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

Selectors in Dataweave in Mule | Dataweave Selectors

  Selectors in Dataweave in Mule | Dataweave Selectors





1) Single Value Selector : 

Dataweave code :

%dw 1.0
%output application/xml
---
{
  info: payload.company.address.name
}

Input : 

{
  "company" : {
  "address" : {
"name" : "Infosys",
      "area" : "Hinjewadi",
      "city" : "Pune"
 
 }
}
}

Output : 

<?xml version='1.0' encoding='UTF-8'?>
<info>Infosys</info>

2) Multi Value Selector : 

Multi Value selector
Multi value selector can either be applied over an :object or an :array.

Over :object
This selector returns an array with all the values whose key matches the expression.

Dataweave code :

%dw 1.0
%output application/json
---
{
  Persons: payload.Persons.*person
}
Input: 

<Persons>
  <person>Darshan</person>
  <person>Rohan</person>
  <person>Tarun</person>
</Persons>

Output : 

{
  "Persons": [
    "Darshan",
    "Rohan",
    "Tarun"
  ]
}

Over :array
The selector is applied on each of the elements of the array that are of type :object and returns an array with all the selected values.

Dataweave code :

%dw 1.0
%output application/xml
---
{
  Persons: payload.Persons[*person]
}

Input : 

<Persons>
  <person>Darshan</person>
  <person>Rohan</person>
  <person>Tarun</person>
</Persons>

Output : 

<?xml version='1.0' encoding='UTF-8'?>
<Persons>
    <person>Darshan</person>
    <person>Rohan</person>
    <person>Tarun</person>
</Persons>

3) Indexed Selector
The index selector returns the element at the specified position, it can be applied over an :array, an :object or a :string

Over :array
This selector can be applied to String literals, Arrays and Objects. In the case of Objects, the value of the key:value pair found at the index is returned. The index is zero based.

If the index is bigger or equal to 0, it starts counting from the beginning.

If the index is negative, it starts counting from the end where -1 is the last element.

Dataweave code : 

%dw 1.0
%output application/json
---
{
  companyInfo: payload.address[1]
}


Input : 

{
  "address" : [{
"name" : "Infosys",
      "area" : "Hinjewadi",
      "city" : "Pune"
 
 },{
      "name" : "wipro",
      "area" : "vikhroli",
      "city" : "Mumbai"
}]
}

Output : 

{
    "companyInfo": {
        "name": "wipro",
        "area": "vikhroli",
        "city": "Mumbai"
    }
}


Over :string

When using the Index Selector with a String, the string is broken down into an array, where each character is an index.

%output application/json
---
{
  name: "Himanshu"[0]
}

Output : 
{
  "name": "H"
}

Over :object

The selector returns the value of the key : value pair at the specified position.

Over :array
Range selectors limit the output to only the elements specified by the range on that specific order. This selector allows you to slice an array or even invert it.

Dataweave code :

%dw 1.0
%output application/json
---
{
  slice: [0,1,2][0 to 1],
  last: [0,1,2][-1 to 0]
}

Output
{
  "slice": [
    0,
    1
  ],
  "last": [
    2,
    1,
    0
  ]
}

Over :string
The Range selector limits the output to only the elements specified by the range on that specific order, treating the string as an array of characters. This selector allows you to slice a string or even invert it.

Dataweave code :

%dw 1.0
%output application/json
---
{
  slice: "DataWeave"[0 to 1],
  last: "DataWeave"[-1 to 0]
}

Output
{
  "slice": "Da",
  "last": "evaeWataD"
}

4) Attribute Selector Expressions

In order to query for the attributes on an Object, the syntax .@<key-name> is used. If you just use .@ (without <key-name>) it returns an object containing each key:value pair in it.

Dataweave code :

%dw 1.0
%output application/json
---
{
  item: {
    type : payload.product.@type,
    name : payload.product.brand,
    attributes: payload.product.@
  }
}


Input

<product id="1" type="tv">
  <brand>Philips</brand>
</product>

Output :

{
  "item:" {
    "type": "tv",
    "name": "Philips",
    "attributes": {
      "id": 1,
      "type": tv
    }
  }
}


5) Descendants Selector

This selector is applied to the context using the form ..<field-name> and retrieves the values of all matching key:value pairs in the sub-tree under the current context. Regardless of the hierarchical structure these fields are organized in, they are all placed at the same level in the output.

Dataweave code :

%dw 1.0
%output application/json
---
{
  companyInfo: payload.company..name
}

Input : 

{
  "company" : {
  "address" : {
"name" : "Infosys",
       "city" : "Pune"
  },
  "address1" : {
"name" : "Wipro",
       "city" : "Mumbai"
  }
  }
}

Output : 

{
    "companyInfo": [
        "Infosys",
        "Wipro"
    ]
}

6) Selectors modifiers

There are two selectors modifiers: ? and !. The question mark returns true or false whether the keys are present on the structures. The exclamation mark evaluates the selection and fails if any key is not present.

Key Present
Returns true if the specified key is present in the object.

Dataweave code :

%dw 1.0
%output application/xml
---
present: payload.name?

Input
{
  "name": "Himanshu"
}

Output:

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


You can also use this validation operation as part of a filter:

Dataweave code :

%dw 1.0
%output application/json
---
{
  companyInfo: payload.company.address.*name[?($ == "Infosys")]
}

Input : 

{
  "company" : {
  "address" : {
"name" : "Infosys",
       "city" : "Pune"
  },
  "address1" : {
"name" : "Wipro",
       "city" : "Mumbai"
  }
  }
}

Output : 

{
    "companyInfo": [
        "Infosys"
    ]
}

Assert Present

Returns an exception if any of the specified keys are not found.

Dataweave code : 

%dw 1.0
%output application/json
---
{
  companyInfo: payload.name!
}

Input
{
"lastname" : "sharma"
}

Output : 

Exception while executing: 
  companyInfo: payload.name!
                       ^
There is no key named 'name'.


7) Calling MEL in Dataweave :

Inbound Properties from a Mule Message
You can take Inbound Properties from the Mule message that arrives to the DataWeave transformer and use them in your transform body. To refer to one of these, simply call it through the matching Mule Expression Language (MEL) expression.

Accessing query parametrs in dataweave : 
%dw 1.0
%output application/json
---
{
  companyInfo: inboundProperties.'http.query.params'.id     //id is query parameter
}

Accessing flow variables in dataweave : 

%dw 1.0
%output application/json
---
{
  companyInfo: flowVars.namevar   //namevar is flow variable 
}

Accessing session variables in dataweave : 

%dw 1.0
%output application/json
---
{
  companyInfo: sessionVars.sessionvarname   //sessionvarname is session variable
}


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