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
1 Answers
1
votes
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