ProstDev ProstDev
Guides Feb 14, 2023 · 5 min read

DataWeave 2.0 scopes for local variables: 'using' vs. 'do' operators

In this post, I'll go through some of the main differences between these two operators so you decide which one to use in your scripts!

By Alex Martinez
DataWeave 2.0 scopes for local variables: 'using' vs. 'do' operators

I had previously written a post with my top 5 DataWeave tips to make your life easier where I talked about the do operator. However, I’ve seen a lot of people asking about the using operator since it was inherited from DataWeave 1.0.

In this post, I’ll go through some of the main differences between these two operators so you decide which one to use in your DataWeave scripts!

Note

By this point, I’m assuming you already have the basic DataWeave understanding. If this is not the case, I recommend you read these tutorials first.

What are scopes?

If you come from a different programming paradigm, this concept might be easy to understand. To keep it simple, let’s just say that scopes are these imaginary boxes where you can keep variables, functions, and so on. These variables (or functions) are not available to anyone else outside of these boxes, only inside. We will see some examples in a bit.

In DataWeave, you can have global and local variables (and functions, annotations, etc.). The global variables are the ones that appear over the three dashes (---) that separate your script. For example

%dw 2.0
output application/json
var globalVar = "this is my global variable"
---
globalVar

These variables are accessible from anywhere in your script. They don’t have to exist inside a specific scope to be used.

Let’s talk about how to create local variables with the using or do operators so we can put together the meaning of scopes.

Creating scopes and local variables with the do operator

Try out the following example. What age do you think will be outputted from this script?

%dw 2.0
output application/json
var age = 21
---
{
  person: do { 
    var user = "Robin"
    var age = 5
    ---
    {
        name: user, 
        age: age
    }
  }
}

The answer is 5. We used the do operator in line 6 to create a local scope. Notice how we created the variables user and age inside this scope. This new scope will work very similarly to a regular script in the sense that it can have its variables, functions, etc.

Notice how we have to add three dashes (---) in line 9 to separate the variable declaration from the localized script. This is because when you use the do operator, you can use this syntax for your new local scope.

Below is a graphical representation of how these two scopes are separated.

DataWeave script with the global scope outlined in green and the do-operator local scope in red

The age in the output is 5 because it will take the “closest” variable. In other words, the more local the variable, the more precedence it takes.

What do you think will be the output if you use the following code? Try to figure it out! The answer will be at the end of the post 🙂

%dw 2.0
output application/json
var age = 21
---
{
  person: do { 
    var user = "Robin"
    var age = 5
    ---
    {
        name: user, 
        age: age + do {
            var age = 10
            ---
            age
        }
    }
  }
}

You can create as many local scopes as you want, but I wouldn’t recommend having more than 1 unless completely necessary. Mainly to avoid spaghetti code. You can read more about do in this post: My top 5 DataWeave tips to make your life easier.

Creating local variables with the using operator

Now, I have to be completely honest here. I haven’t used the using operator. I did work on DataWeave 1.0 for a while, but it was confusing for me to understand how this syntax worked, so I never really used it. Here is an awesome post if you’re still developing with DataWeave 1.0: How to use the “Using” Operator in DataWeave, the MuleSoft Mapping Tool. Otherwise, I’ll explain how to create local variables in DataWeave 2.0 here.

Let’s follow the previous example to demonstrate.

%dw 2.0
output application/json
var age = 21
---
{
  person: using (user = "Robin", age = "5") { 
    name: user, 
    age: age
  }
}

We will have the same functionality as before but see how this syntax is different than do.

We can also create variables with lambda expressions to use them as functions. Like in the following example.

%dw 2.0
output application/json
var age = 21
---
{
  person: using (
        user = "Robin", 
        age = "5", 
        echo = (t) -> t
    ) { 
    name: user, 
    age: age,
    test: echo(123)
  }
}

However, this operator is considered deprecated since DataWeave 2.0. I personally don’t like the way the syntax works. It makes more sense for me to use do instead. But you can still use using if you’re really into it 😀- although you might get a huge warning in your scripts telling you to stop using this operator!

Anypoint Studio warning that the using operator is deprecated, suggesting replacing it with do

My advice for you

The using operator is deprecated for a reason. The fact that the team didn’t remove it from DataWeave 2.0 doesn’t mean that it’s the best way to create local variables. Yes, you can still use it if that’s the way you’ve been doing it since DataWeave 1.0, but who knows when they’ll remove it? It might be risky to keep it that way.

I also feel it’s more intuitive to use do because of the three-dashes syntax that we already know how to use. We can easily declare local variables, functions, annotations, or namespaces the same way we do global ones. However, it can be tricky to use at first because of the curly brackets ( { } ). But I swear, the more you use it, the less time you take to do it automatically!

You can also check out the docs to read more about these two:

Oh! And the answer to the question is this:

{
  "person": {
    "name": "Robin",
    "age": 15
  }
}

Try it out if you don’t believe me :-)

Let me know if you have any questions!

-alex

FAQs

Frequently asked questions about this post.

  • What is a scope in DataWeave?

    A scope is like an imaginary box where you can keep variables, functions, and so on; those variables or functions are not available to anyone outside the box, only inside it. In DataWeave you can have global variables (the ones declared above the three dashes that separate your script, accessible from anywhere) and local variables created inside a scope with the do or using operators.

  • What's the difference between the `do` and `using` operators for local variables?

    Both create local variables with the same functionality, but the syntax differs: with do you open a new local scope and add three dashes to separate the variable declarations from the localized script, just like a regular script, while using declares the variables in parentheses (such as using (user = "Robin", age = "5")) before the body. The post's author finds do more intuitive because of the familiar three-dashes syntax, and notes using is deprecated since DataWeave 2.0.

  • Why does the `do` example output an age of 5 instead of 21?

    Because DataWeave takes the closest variable: the more local the variable, the more precedence it takes. Even though a global var age = 21 exists, the local var age = 5 declared inside the do scope is the one used in the output.

  • Can I use lambda expressions as functions inside the `using` operator?

    Yes, you can create variables with lambda expressions to use them as functions inside using, for example declaring echo = (t) -> t alongside the other variables and then calling echo(123) in the body.

  • Should I still use the `using` operator?

    The post advises against it: using is deprecated since DataWeave 2.0 and you might get a large warning in your scripts (such as in Anypoint Studio) telling you to stop using it. The author recommends do instead because it is more intuitive, and warns that since the team kept using in DataWeave 2.0 there's no guarantee they won't remove it later, which makes relying on it risky.

Search

Loading search…