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.@
}
}
<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!
}
{
"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
}