Dataweave 2.0 operations in Mule
- February 19, 2020
What do we mean by dataweave?
Weaving in itself is an action to make a pattern using some elements. Weaving a cloth will involve creating the pattern of passing threads at right angles to one another.
Dataweave is a tool provided by MuleSoft to create a desirable data format from given input data.There are various functionalities that dataweave supports:
- Mapping
- SQL operations
- Message streaming
- Custom Function requirement
Dataweave scripts have two different sections:
- Header: contains of the metadata that could be further utilized in body
- Body: this consists of all the operations required for generation of desirable output. These are separated by “—”
For the entire dataweave tutorial we will be using the input mentioned below:
Input payload:
Let’s give it a try:
Dataweave Expression (read the note for functional requirement)
Expected Output
** To get json output **
%dw 2.0
output application/json
—
Payload
Note: – In this example we are just trying to convert the XML input to json output.
{
“users”: {
“user”: {
“name”: “Alex Jordan”,
“age”: “32”
},
“user”: {
“name”: “Alexa Jordan”,
“age”: “24”
},
“user”: {
“name”: “Ale Jordan”,
“age”: “25”
},
“admin”: {
“name”: “Alexix Jordan”,
“age”: “26”
}
}
}
** For removing null values **
%dw 2.0
output application/json
skipNullOn = ‘everywhere’
—
payload.users.*user
Note:- For a given scenario if we need to fetch the details of all the user from given input
[
{
“name”: “Alex Jordan”,
“age”: “32”
},
{
“name”: “Alexa Jordan”,
“age”: “24”
},
{
“name”: “Ale Jordan”,
“age”: “25”
}
]
** to sort user according to age (doesn’t work for null values) **
%dw 2.0
output application/json
skipNullOn = “everywhere”
—
payload.users.*user orderBy($.age)
Note:- If we need to fetch the details of all the users sorted based on their age from youngest to oldest
[
{
“name”: “Alexa Jordan”,
“age”: “24”
},
{
“name”: “Ale Jordan”,
“age”: “25”
},
{
“name”: “Alex Jordan”,
“age”: “32”
}
]
** to sort user according to age in descending order (doesn’t work for null values) **
%dw 2.0
output application/json
skipNullOn = “everywhere”
—
payload.users.*user orderBy(-$.age)
Note:- If we need to fetch the details of all the users sorted based on their age from oldest to youngest
[
{
“name”: “Alex Jordan”,
“age”: “32”
},
{
“name”: “Ale Jordan”,
“age”: “25”
},
{
“name”: “Alexa Jordan”,
“age”: “24”
}
]
** to use sizeOf function with nested sorted function **
%dw 2.0
output application/json
skipNullOn = “everywhere”
—
payload.users.*user orderBy(sizeOf($.name))
Note:- if we need to sort the details of all the users based on the length of their name
[
{
“name”: “Ale Jordan”,
“age”: “25”
},
{
“name”: “Alex Jordan”,
“age”: “32”
},
{
“name”: “Alexa Jordan”,
“age”: “24”
}
]
** use of splitBy function**
%dw 2.0
output application/json
skipNullOn = “everywhere”
—
payload.users.*user orderBy(sizeOf($.name splitBy(” “)[0]))
Note:- if we need to sort the details of all the users based on the length of their first name
[
{
“name”: “Alex Jordan”,
“age”: “32”
},
{
“name”: “Alexa Jordan”,
“age”: “24”
},
{
“name”: “Ale Jordan”,
“age”: “25”
}
]
** Use of if-else Condition **
%dw 2.0
output application/json
—
payload.users.*user map(a,b) ->
if (a.age <= 25)
{ response : “You age is less than or equal to 25” }
else { response : “Your age is greater than 25” }
Note:- If we need to define different operation based on age
[
{
“response”: “Your age is greater than 25”
},
{
“response”: “You age is less than or equal to 25”
},
{
“response”: “You age is less than or equal to 25”
}
]
** Matches exact value for string**
%dw 2.0
output application/json
—
payload.users.*user map(a,b) ->
if (matches(a.name, /Alex Jordan/))
{ response : “Welcome admin” }
else { response : “You do not have admin privileges” }
Note:- Lets say we need to write logic based on some regex pattern
[
{
“response”: “Welcome admin”
},
{
“response”: “You do not have admin privileges”
},
{
“response”: “You do not have admin privileges”
}
]
** Function Call **
%dw 2.0
output application/json
fun UpperCase(input_value) = upper(input_value)
—
payload.users.*user map(a,b) ->
name : UpperCase(a.name)
Note:- If we have to create a function that change the case of the input string provide
[
{
“name”: “ALEX JORDAN”
},
{
“name”: “ALEXA JORDAN”
},
{
“name”: “ALE JORDAN”
}
]
**Example of pluck**
%dw 2.0
output application/json
—
payload.users.*user map(a,b) ->
a pluck ($$ ++ ” is ” ++ $)
Note:- for a give requirement we need to return back message the is composed of the attribute and its value
[
[
“name is Alex Jordan”,
“age is 32”
],
[
“name is Alexa Jordan”,
“age is 24”
],
[
“name is Ale Jordan”,
“age is 25”
]
]
**Example of filter **
%dw 2.0
output application/json
—
payload.users.*user filter(a,b)->(a.age > 25)
Note:- If we have to retrieve all the records of user whose age is greater than 25
[
{
“name”: “Alex Jordan”,
“age”: “32”
}
]
**Example of Reduce**
%dw 2.0
output application/json
—
(payload.users.*user.age reduce ((val, initial_value = 0) -> initial_value + val) )/3
Note:- If we need to retrieve the average age of the user .
27.0
If you’ve reached this point and understood the operations discussed above, let’s try some basic exercise for self-evaluation.
Requirement: Write a dataweave script that accepts a 2-digit number >20 and prints the table of that number
Expected Output (for input value 21):
[ “21 * 0 = 21”,
“21 * 1 = 42”,
“21 * 2 = 63”,
“21 * 3 = 84”,
“21 * 4 = 105”,
“21 * 5 = 126”,
“21 * 6 = 147”,
“21 * 7 = 168”,
“21 * 8 = 189”,
“21 * 9 = 210”]
Wrapping up
With dataweave your imagination is the only limit. This article deals with basic dataweave operations and their real life implementation.
— By Abhinav Patel