The document discusses the key directives used in DataWeave transformations, including:
- The %dw directive specifies the DataWeave version.
- The %output directive specifies the output type such as application/xml.
- Other directives include %input, %namespace, %var to define constants, and %function to define reusable functions.
These directives provide metadata about the transformation and allow defining constants and functions that can be referenced within the DataWeave code. The directives are specified in the header, while the transformation logic is defined in the body.
2. • Below are the 6 directives, which are used as the key
aspects of the transformation.
– DataWeave version :
• Example : %dw 1.0
– Output type :
• Example : %output application/xml
– Input type :
• Example : %input payload application/xml
– Namespaces :
• Used to import into the transform
• Example : %namespace ns0http://mulesoft.org/tshirt-service/
– Constants :
• Constants which can be referenced through the body.
• Example : %var conversionRate=13.15
– Functions :
• Functions which can be called through the body.
• Example : %var toUser = (user) → {firstName: user.name, lastName:
user.lastName}
DataWeave – Header Directives
3. • Version Directive :
– Specify the version of the DataWeave syntax that is used to
interpret the transformation using the version directive.
– Example : %dw 1.0
• Namespace Directive :
– Use this directive to specify an alias for a URI, which is
specified after the alias.
– The directive is relevant only when either the input or the
output is of type XML.
– Example : %namespace mes
http://www.mulesoft.com/anypoint/SOA/message/v1.0
DataWeave – Header Directives
4. • Output Directive:
– Specify the transformation output type in the following
format:
<content>/<type>
– Only one output type can be specified.
– The structure of this output is further specified in the
DataWeave body.
– Example : %output application/xml
DataWeave – Header Directives
6. • Input Directive :
– Optionally specify an input source and its input type in the
following format:
<content>/<type>
– Example : %input payload application/xml
– It is not necessary to declare input directives for any of the
components of the Mule Message that arrive at the DataWeave
transformer (payload, flow variables, and input/outbound
properties), nor for any system variables.
– You likely will not need this feature as any data arriving inside
the incoming Mule message gets implicitly recognized as input
based on the accompanying metadata that passes along with it
through the flow components.
– As a result, the data can be referenced and acted upon easily
enough anywhere within the DataWeave body without a need
to include them in the header.
DataWeave – Header Directives
8. • Define Constant Directive :
– You can define a constant in the header, and reference it
(or its child elements, if any exist) in the DataWeave
body.
– Example :
%dw 1.0
%var conversionRate=13.15
%output application/json
---
{
price_dollars: payload.price,
price_localCurrency: payload.price * conversionRate
}
DataWeave – Header Directives
9. • Define Function Directive :
– You can define a function in the header, you can then call it
in any part of the DataWeave body, including arguments.
– Example 1:
%dw 1.0
%output application/json
%var toUser = (user) -> {firstName: user.name, lastName:
user.lastName}
---
{
user: toUser(payload)
}
DataWeave – Header Directives
10. • Define Function Directive :
– A function directive can be defined via %var as in the
Example 1, in previous slide , or via %function.
– Example 2:
%dw 1.0
%output application/json
%function toUser(user){firstName: user.name, lastName:
user.lastName}
---
{
user: toUser(payload)
}
DataWeave – Header Directives