The Art of DataWeave

Sri Uday Kumar Dhanala
5 min readOct 2, 2023

--

Break it down in simple terms

Imagine you have a big box of colorful building blocks. Sometimes, you want to change these blocks into a different shape or color, or maybe you want to put some blocks together to make something new, like a cool robot.

MuleSoft DataWeave is like a magic wand that helps you do this with your blocks. It can turn red blocks into blue ones, stack them in a new way to build a tower, or even pick out only the green blocks from your box.

You tell DataWeave what you want to do with your blocks by giving it special instructions, like saying, “Turn all the round blocks into square ones.” DataWeave follows your instructions and makes your blocks look or behave just the way you want.

So, in simple words, DataWeave is like a magical tool for changing and organizing your colorful building blocks (data) in any way you like!

DataWeave is a powerful expression language in MuleSoft that allows you to transform, query, and manipulate data with ease. In this blog post, we’ll explore various DataWeave functions, including map, mapObject, pluck, filter, and, or, concat, contains, typeOf, flatten, sizeOf, reduce, joinBy, splitBy, orderBy, distinctBy, groupBy, replace, startsWith, endsWith, upper, lower, and trim. We'll provide examples of each function using payload data, DataWeave code, and the resulting response.

Input Data (Payload):

Let’s assume we have the following JSON data in our payload:

{
"employees": [
{
"name": "John",
"age": 30,
"department": "HR"
},
{
"name": "Alice",
"age": 25,
"department": "Finance"
},
{
"name": "Bob",
"age": 35,
"department": "IT"
}
],
"numbers": [1, 2, 3, 4, 5],
"text": "Hello, World!",
"email": "example@email.com",
"arrays":[1, [2, 3], [4, [5]]]
}

DataWeave Examples:

Let’s dive into various DataWeave functions and their usage.

map: input and output are arrays

%dw 2.0
output application/json
---
payload.employees map {
"Name": $.name,
"Age": $.age * 2
}

Response:

[
{
"Name": "John",
"Age": 60
},
{
"Name": "Alice",
"Age": 50
},
{
"Name": "Bob",
"Age": 70
}
]

mapObject: input and output are objects:

%dw 2.0
output application/json
---
payload mapObject {
employees: $
}

Response:

{
"employees": [
{
"name": "John",
"age": 30,
"department": "HR"
},
{
"name": "Alice",
"age": 25,
"department": "Finance"
},
{
"name": "Bob",
"age": 35,
"department": "IT"
}
],
"employees": [
1,
2,
3,
4,
5
],
"employees": "Hello, World!",
"employees": "example@email.com"
}

pluck: Extract values from an array of objects:

%dw 2.0
output application/json
---
((payload pluck ((value, key, index) -> value))[0]).name

Response:

[
"John",
"Alice",
"Bob"
]

filter: Filter elements in an array based on a condition:

%dw 2.0
output application/json
---
payload.employees filter $.age > 30

Response:

[
{
"name": "Bob",
"age": 35,
"department": "IT"
}
]

and, or: Logical operations:

%dw 2.0
output application/json
---
{
AND : payload.employees filter $.department == "HR" and $.age == 30,
OR: payload.employees filter $.department == "IT" or $.age == 30
}

Response:

{
"AND": [
{
"name": "John",
"age": 30,
"department": "HR"
}
],
"OR": [
{
"name": "John",
"age": 30,
"department": "HR"
},
{
"name": "Bob",
"age": 35,
"department": "IT"
}
]
}

concat: Concatenate strings:

%dw 2.0
output application/json
---
payload.text ++ " Welcome!"

Response:

"Hello, World! Welcome!"

contains: Check if a string contains a substring:

%dw 2.0
output application/json
---
payload.text contains "World"

Response:

true

typeOf: Check the data type of a value:

%dw 2.0
output application/json
---
typeOf(payload.numbers)

Response:

"Array"

flatten: Flatten nested arrays:

%dw 2.0
output application/json
---
flatten(payload.arrays)

Response:

[
1,
2,
3,
4,
[
5
]
]

sizeOf: Get the size of an array:

%dw 2.0
output application/json
---
sizeOf(payload.employees)

Response:

3

reduce: Perform a reduction operation on an array:

%dw 2.0
output application/json
---
payload.numbers reduce $+$$

Response:

15

joinBy: Join array elements into a string:

%dw 2.0
output application/json
---
payload.numbers joinBy ", "

Response:

"1, 2, 3, 4, 5"

splitBy: Split a string into an array:

%dw 2.0
output application/json
---
payload.text splitBy ", "

Response:

[
"Hello",
"World!"
]

orderBy: Sort elements in an array:

%dw 2.0
output application/json
---
payload.employees orderBy $.age

Response:

[
{
"name": "Alice",
"age": 25,
"department": "Finance"
},
{
"name": "John",
"age": 30,
"department": "HR"
},
{
"name": "Bob",
"age": 35,
"department": "IT"
}
]

DataWeave Operators

Now, let’s explore some essential DataWeave operators used in the script:

Arithmetic Operators: Operators like +, -, *, and / are used for performing mathematical operations.

Parentheses (): Used to control the order of operations, similar to mathematical expressions.

Logical Operators: Operators like and, or, and not are used for logical operations.

Comparison Operators: Operators like ==, !=, >, <, >=, and <= are used for comparing values.

Conditional Operators: Operators like if, else, and match enable conditional logic in DataWeave.

Type Casting Operators: Operators like as, is, and null are used for handling data types.

Collection Operators: Operators like map, filter, and reduce are used for working with collections (arrays and objects).

String Operators: Operators like ++ for concatenation and string interpolation using '$' are available for manipulating strings.

Coalescing Operator ??: Used for providing default values.

Function Call Operator (): Used to invoke functions.

Thank you for taking the time to delve into the Art of DataWeave in MuleSoft with me. I hope you found this article informative and insightful.

Be sure to stay tuned for the continuation of this exciting journey in the next article, “The Art of DataWeave 2.0.” There’s more to explore and learn, so don’t miss out on the next installment!

If you’re eager to expand your knowledge further, don’t forget to check out my other articles on MuleSoft:

  1. Mastering Batch Processing Techniques in MuleSoft
  2. RAML: A Comprehensive Guide to MuleSoft’s API Specification
  3. Deep Dive into MuleSoft API-Led Connectivity
  4. Demystifying OAuth 2.0: A Comprehensive Guide for MuleSoft Developers
  5. Unlocking Business Potential: Why MuleSoft Leads the Integration Revolution
  6. From Blunders to Brilliance: A Deep Dive into MuleSoft Error Handling

Each of these articles explores different aspects of MuleSoft and can help you gain a more comprehensive understanding of this powerful integration platform. Happy reading, and feel free to reach out if you have any questions or need further clarification on any MuleSoft topic!

--

--

Sri Uday Kumar Dhanala

I write to educate, inspire, and connect with diverse readers. Dive into my articles to experience a blend of expertise, innovation, and the art of teaching.