Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Variables in DataWeave

DZone's Guide to

Variables in DataWeave

It is possible to define variables in DataWeave header, DataWeave body, and flow variables before the DataWeave component in a Mule flow in DataWeave code.

· Integration Zone
Free Resource

Discover how Microservices are a type of software architecture where large applications are made up of small, self-contained units working together through APIs that are not dependent on a specific language. Brought to you in partnership with AppDynamics.

Variables are data items that may take on more than one value during the runtime of the Mule flow. We can either define variables in DataWeave header or DataWeave body. We can also reference variables defined before the DataWeave component in a Mule flow.

Variables declared in the DataWeave header have global scope, i.e., we can refer variables declared in the header in any part of code defined in DataWeave body. To declare and initialize a variable with a limited scope, we can define it in any part of the DataWeave body.

We can initialize variables using literal expressions, variable reference expressions, or functional expressions. They may reference any other scoped variables or any of the input variables or constants in their initialization. The declaration and initialization can be prepended to any literal expression.

In this article, we are going to see how we can define variables in different ways. We will use an example of XML to XML transformation for this.

Let us say that input XML has following structure:

<invoice>
    <header>
        <customer_name>ACME, Inc.</customer_name>
        <customer_state>CA</customer_state>
    </header>
    <items>
        <item>
            <description>Product 1</description>
            <quantity>2</quantity>
            <unit_price>10</unit_price>
        </item>
        <item>
            <description>Product 2</description>
            <quantity>1</quantity>
            <unit_price>30</unit_price>
        </item>
    </items>
</invoice>

We will define input and output metadata using this same XML in DataWeave properties and then add few lines of DataWeave code, as shown in the below screenshot:

dataweave1.jpg

Now, let us define a variable called dis in the DataWeave header section:

dataweave2.jpg

We can use this variable in code by using the variable name in DataWeave code as shown in below screenshot:

dataweave3.jpg

This is one way of defining variables. We can also define variables in the DataWeave body with the help of the using operator:

Image title

It is also possible to reference flow variables in DataWeave code. Let's say that we have a flow variable dis defined:

Image title

We can use this flow variable in DataWeave code as shown in below screenshot:

Image title

We have seen that it is possible to define variables in DataWeave header, DataWeave body, and flow variables before the DataWeave component in a Mule flow in DataWeave code.

Discover the six challenges and best practices in managing microservice performance, brought to you in partnership with AppDynamics.

Topics:
mule esb ,dataweave ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}