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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Import a Function/Module in Dataweave
  • Java: A Time-Tested Programming Language Still Going Strong
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Pragmatic Premature Optimization

Trending

  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption
  • Why AI Agents Need a Shared Enterprise Ontology
  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • Adding Graph Retrieval to a RAG Pipeline With Python

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Dynamically Evaluate Dataweave Scripts

Dynamically Evaluate Dataweave Scripts

This article demonstrates how to use Dynamic Evaluate Component in Mule 4 to execute dataweave scripts dynamically.

By 
Vikalp Bhalia
·
Updated Jul. 20, 20 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article demonstrates how to use Dynamic Evaluate Component in Mule 4 to execute dataweave scripts dynamically.

It is a very common use case where a developer may have to execute different dwl scripts (account.dwl/customer.dwl  in this case) depending on the input parameter.

dw/customer.dwl:

Java
 




x


 
1
{
2
 "customer":{
3
   "id":payload.customerId,
4
   "name": payload.customerName,
5
   "address": payload.customerAddress,
6
   "mobile": payload.customerMobile,
7
   "accountId": payload.customerAccountId,
8
   "category": payload.customerCategory
9
 } 
10
}



dw/account.dwl:

Java
 




x


 
1
{
2
  "account":{
3
    "id": payload.accountId,
4
    "name": payload.accountName,
5
    "address": payload.accountAddress,
6
    "customerId": payload.accountOwnerId,
7
    "startDate": payload.accountStartDate,
8
    "renewalDate": payload.accountRenewalDate,
9
    "subscriptionType": payload.accountSubscription
10
  }
11
}



The First thing that will hit our mind is a Choice router. But is it an optimized way? What if in the future we may have more objects? We will have to update the code if we use a choice router.

But using Dynamic Component Evaluator or dataweave custom module we can avoid it.

Using Dynamic Component Evaluator

Below are the steps:

Create a dwl file that sets the script name in a variable based on the input parameter.

Java
 




xxxxxxxxxx
1
11


 
1
<ee:transform>
2
  <ee:variables ><ee:set-variable variableName="script" >
3
   <![CDATA[
4
      %dw 2.0
5
      output application/java 
6
      var fileName= "dw/$(vars.inputParameter).dwl"
7
      ---
8
      readUrl("classpath://" ++ fileName,"text/plain")
9
    ]]>
10
  </ee:set-variable> </ee:variables>
11
</ee:transform>



This step should give you the dwl file full path in the script variable. Eg. dw/account.dwl or dw/customer.dwl.

  1. Use the script variable in the Dynamic Evaluate Component
    <ee:dynamic-evaluate doc:name="Dynamic Evaluate"

expression="#[vars.script]"/>

In the future, if we have more objects then add the <object>.dwl file in the resource/dw folder.

Sample flow:

sample flowd

Using Dataweave Custom Module

Below are the steps:

1. Define a custom module for customer and account object. Refer below code snippet:

Java
 




xxxxxxxxxx
1
12


 
1
%dw 2.0
2
var account = (acc: Object) -> {
3
    account: {
4
        id: acc.accountId,
5
        name: acc.accountName,
6
        address: acc.accountAddress,
7
        customerId: acc.accountOwnerId,
8
        startDate: acc.accountStartDate,
9
        renewalDate: acc.accountRenewalDate,
10
        subscriptionType: acc.accountSubscription
11
    }
12
}



2. Import the modules in the transform message component and invoke the function we created:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
import dw::account as Acc
4
import dw::customer as Cust
5
---
6
if (vars.inputParameter == "customer") Cust::customer(payload) else Acc::account(payload)



Happy coding!

Java (programming language) Use case Object (computer science) EGS (program) Snippet (programming) Coding (social sciences) dev optimization

Opinions expressed by DZone contributors are their own.

Related

  • Import a Function/Module in Dataweave
  • Java: A Time-Tested Programming Language Still Going Strong
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Pragmatic Premature Optimization

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook