Global & Local Variables In DataWeave 2.0

Defining & using global variables

We can create a global variable in the header using var directive. We need to give it a name and we can assign either a constant value or a lambda expression. Global variables can be referenced by their name from anywhere in the body of the DataWeave script.

Let us see an example. In the exhibit below, we initialize 3 global variables.

Input

{

“firstName”: “max”,

“lastName”: “DataWeave”

}

DataWeave Script:

%dw 2.0

output application/json

var Dname = “the”

var Dname2 = () -> “other”

var Lname = (aString) -> upper(aString)

name: {

first: payload.firstName,

middle1: Dname,

middle2: Dname2(),

last: payload.lastName

}

Output:

{

“name”: {

“first”: “max”,

“middle1”: “the”,

“middle2”: “other”,

“last”: “DataWeave”

}

}

The first one “Dname” is assigned the literal string value “the”. The second one “Dname2” is assigned a lambda expression which returns a literal string value “other”. The third one “Lname” is assigned a lambda expression which takes a string as an input, passes it to the upper function, and returns the string in uppercase.

DataWeave also gives an alternate way to define a variable as a function in a syntax similar to traditional functions. It may be easier to read for some people. We have to use the fun directive in this way. The following exhibit shows the original syntax for defining lname as in the previous example and the alternative for the same using fun directive. The behavior is identical in both cases.

Input

{

“firstName”: “max”,

“lastName”: “DataWeave”

}

DataWeave Script:

%dw 2.0

output application/json

var Lname = (aString) -> upper(aString)

name: {

first: payload.firstName,

last: Lname(payload.lastName)

}

%dw 2.0

output application/json

fun Lname(aString) = upper(aString)

name: {

first: payload.firstName,

last: Lname(payload.lastName)

}

Output:

{

“name”: {

“first”: “max”,

“last”: “DATAWEAVE”

}

}

Defining & using local variables

Local variables are initialized inside the body of the DataWeave script using the keyword “using” with the syntax using(<variable-name> = <expression>) Local variables can only be referenced from within the scope of the expression where they are initialized. You can declare multiple local variables in a “do” scope header with the syntax do{<variable declaration header> — <body>}

Let us see an example of creating a local variable with “do” scope

Input:

{

“firstName”: “max”,

“lastName”: “DataWeave”

}

DataWeave Script:

%dw 2.0

output application/json

var Lname = do {var name = payload.firstName ++  ” ” ++  payload.lastName — name}

Lname

Output:

“max DataWeave”

Input:

{

“firstName”: “max”,

“lastName”: “DataWeave”

}

DataWeave Script:

%dw 2.0

output application/json

var Lname = do {var fname = payload.firstName var lname = payload.lastName — {

person: do {

var user = fname var color = “gray” — {

name1: user, color: color

}

}

}}

Lname

Output:

{

“person”: {

“name1”: “max”,

“color”: “gray”

}

}

DataWeave Script:

%dw 2.0

output application/json

var Lname = do {var fname = payload.firstName var lname = payload.lastName — {

person: do {

var user = fname var color = “gray” — {

name1: user, color: color

},

name2: lname, color: color

}

}}

Lname

Output:

Unable to resolve to reference of color

  • In the first example, a local variable called “name” is declared then used as a transformation expression.
  • The second example defines two local variables “fname” & “lname” in an outer scope and two local variables “user” & “color” in an inner scope. It shows that the inner scope can reference the variable in outer scope by assigning “fname” to “user”.
  • The third example is similar to the second but shows that the outer scope cannot reference the variable defined in the inner scope when it tries to assign color value to a color property. This results in an error.

More Examples:

Input:

{
“id”: “1”,
“employee_name”: “John”,
“employee_salary”: 320800,
“employee_age”: 61,
“profile_image”: “”
}

Global Dataweave Variable:

Dataweave Expression:

%dw 2.0
output application/json
var value = “Hello”
{
message: value ++ vars.name
}

Output:

{
“message”: “HelloJohn”
}

Local Dataweave Variable:

Dataweave Expression:

%dw 2.0
output application/json
{
message: using(value = “Hi”) value ++ payload.employee_name
}

Output:

{
“message”: “HiJohn”
}

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago