2. • DataWeave allows to put logic in the script using
expression values.
• All expressions in DataWeave return a value.
• Expressions can be categorized into :
– Operators
– Selectors
– Flow Control Expressions
DataWeave – Body - Expressions
3. • Operators :
– An operator applies a specific logic/transformation over a
data-structure.
– Operators can be classified based on their arity as :
• Unary
• Binary
• Ternary
– Example :
DataWeave – Body - Expressions
Transform Output
%dw 1.0
%output application/json
---
{
name: upper "mulesoft"
}
{ "name": MULESOFT }
4. • Selectors :
– A selector allows for the navigation and querying the
multiple levels of a data-structure to reference a certain
value or set of values
– Example :
DataWeave – Body - Expressions
Transform Input Output
%dw 1.0
%output
application/json
---
{
users:
payload.users.*user
}
<users>
<user>Mariano</user>
<user>Martin</user>
<user>Leandro</user>
</users>
{
"users": [
"Mariano",
"Martin",
"Leandro"
]
}
5. • Flow Control Expressions :
– Flow control expressions can be subcategorized
into below :
• When Otherwise
• Unless Otherwise
• Default
• Pattern matching
DataWeave – Body - Expressions
6. • When Otherwise :
– The keyword when conditionally evaluates a part of the DataWeave
code, depending on if an expression evaluates to true or to false
– A single line can be made as conditional, or a whole section can be
enclosed in curly brackets.
– In case the when expression evaluates to false, its corresponding part
of the code is ignored, and the code that corresponds to
the otherwise expression is executed.
– Example 1:
%dw 1.0
%output application/json
---
{
currency: "USD"
} when payload.country == "USA"
otherwise
{
currency: "EUR"
}
DataWeave – Body - Expressions
7. • When Otherwise :
– Several otherwise expressions can be chained together, like in the
example below:
– Example 2:
%dw 1.0
%output application/json
---
{
currency: "USD"
} when payload.country =="USA"
otherwise
{
currency: "GBP"
} when payload.country =="UK"
otherwise
{
currency: "EUR"
}
DataWeave – Body - Expressions
8. • Unless Otherwise :
– The keyword unless conditionally evaluates a part of the DataWeave
code, depending on if an expression evaluates to true or to false.
– A single line can be made conditional, or a whole section can be
enclosed in curly brackets.
– In case the unless expression evaluates to true, its corresponding part
of the code is ignored, and the code that corresponds to
the otherwise expression is executed.
– Example :
%dw 1.0
%output application/json
---
{
currency: "EUR"
} unless payload.country == "USA"
otherwise
{
currency: "USD"
}
DataWeave – Body - Expressions
9. • Default :
– Assigns a default value in case no value is found in the input field.
– Example :
%dw 1.0
%output application/json
---
{
currency: payload.currency default "USD"
}
DataWeave – Body - Expressions