Piyush Singh
Piyush Singh

Reputation: 23

Declaration of dynamic dataweave variable in Mule 4 like we have done with using in Mule 3

I have a requirement of creating a runtime variable in Dataweave like we have done in Mule 3 with the using keyword. Can someone let me know how can it be achieved in Mule 4

Upvotes: 1

Views: 2570

Answers (1)

Ryan Carter
Ryan Carter

Reputation: 11606

You can still use using keyword in Mule 4/Dataweave 2.

Local variables are initialized in the body of the DataWeave script and can be referenced by name only from within the scope of the expression where they are initialized.

The syntax for initializing a local variable looks like this: using ( = ) You can combine several local variable definitions as a comma separated list inside the using function. For example: using (firstName='Annie', lastName='Point')

%dw 2.0
output application/json
---
using (x = 2) 3 + x

Here is an example of defining a local variable within an object:

%dw 2.0
output application/xml
---
{
  person: using (user='Greg', gender='male') {
    name: user,
    gender: gender
  }
}

Note thise variables are only scoped to the 'person' object. Accessing them outside of person will throw an error.

Full documentation on this here: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-variables

Upvotes: 1

Related Questions