2. • Variables :
– Constants :
• these can then be referenced as variables in any part of your transform body
– Scoped Variables
• to declare and initialize a variable with a limited scope, you can do so in any part of the
transform’s body
• Constants :
– In the DataWeave header, you define constants as directives
– these can then be referenced as variables in any part of your transform body, just as you
do with input variables.
– Example :
DataWeave – Body -Variables
Transform Output
%dw 1.0
%output application/xml
%var language='Español'
---
{
document: {
language: language,
text: "Hola mundo"
}
}
<?xml version="1.0"
encoding="UTF-8"?>
<document>
<language>Español</language>
<text>Hola Mundo</text>
</document>
3. • Scoped Variables :
– Variables declared in the Transform’s header always have a global
scope.
– To declare and initialize a variable with a limited scope, you can do so
in any part of the transform’s body.
– You can initialize these variables using :
• literal expressions
• variable reference expressions
• functional expressions
– You cannot reference a variable outside its scope.
– To declare a variable in the DataWeave body, the following syntax is
supported: using (<variable-name> = <expression>)
– It must be written before defining the contents of the literal that it
exists in.
– To reference an already initialized variable, you can just call it by the
name you defined for it as with any other variable
– you can also write it in the form $<variable-name>.
DataWeave – Body -Variables
4. • Examples :
– Scoped to Simple Value :
– Scoped to Array literal :
DataWeave – Body -Variables
Transform Output
%dw 1.0
%output application/json
---
using (x = 2) 3 + x
Result is simply 5
Transform Output
%dw 1.0
%output application/json
---
using (x = 2) [1, x, 3]
5. • Examples :
– Scoped to Object literal :
DataWeave – Body -Variables
Transform Comments
%dw 1.0
%output application/xml
---
{
person: using (user = "Greg",
gender = "male") {
name: user,
gender: gender
}
}
user is a valid reference because
it is within the object person for
which it was declared.
6. • Examples :
– Invalid Reference outside of Scope :
DataWeave – Body -Variables
Transform Comments
%dw 1.0
%output application/xml
---
entry: using (firstName = "Annie",
lastName = "Point") {
person: using (user = firstName,
gender = "male") {
name: user,
gender: gender
},
sn: lastName,
gen: gender
}
The reference lastName is valid
because it is within scope.
The reference gender is invalid
because gender was declared in
the person object, and this
reference exists outside the
scope of that object.