DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • DMN 1.3 Temporal FEEL Expressions
  • DataWeave Dynamic Evaluate Component Explained
  • Data Governance Essentials: Glossaries, Catalogs, and Lineage (Part 5)
  • Implement Hibernate Second-Level Cache With NCache

Trending

  • Kullback–Leibler Divergence: Theory, Applications, and Implications
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  1. DZone
  2. Data Engineering
  3. Data
  4. Variables in DataWeave

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.

By 
Swati Deshpande user avatar
Swati Deshpande
·
Dec. 26, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
42.0K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Flow (web browser) Input and output (medicine) XML Literal (computer programming) Data (computing) Metadata Property (programming) Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • DMN 1.3 Temporal FEEL Expressions
  • DataWeave Dynamic Evaluate Component Explained
  • Data Governance Essentials: Glossaries, Catalogs, and Lineage (Part 5)
  • Implement Hibernate Second-Level Cache With NCache

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!