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

  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • MuleSoft Integration With RabbitMQ
  • How to Use IBM App Connect's ‘Mapping Assist’ to Automatically Map Your Integration Flows
  • Implementing Real-Time Datadog Monitoring in Deployments

Trending

  • Hosted Code Interpreter in Spring AI: Letting OpenAI Write and Run Python via the Responses API
  • Age Verification Is Becoming One of Tech’s Hardest QA Problems
  • Integration Testing With MongoDB in Java: Finding the @Sql Equivalent With Liquibase/Flyway or Jamolingo
  • Don't Adopt Anything Announced at re:Invent for 90 Days

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. Testing, Deployment, and Maintenance
  3. Deployment
  4. DataWeave Dynamic Evaluate Component Explained

DataWeave Dynamic Evaluate Component Explained

By using Dynamic Evaluate Component, this integration can be achieved by writing one flow and the transformation can be applied dynamically.

By 
Gary Liu
·
Sep. 09, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.2K 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

Introduction

In Dataweave 2.0, we can execute the transformation of data (payload, attributes, variables, etc) dynamically. This component is called "Dynamic Evaluate Component" which is available since Mule 4.1. This component is extremely useful when we have the integration like the following:

execute report consumer

As shown above, the Execute Report Consumer is a Workday Custom Web Service, the operation is the same but the requests and responses are different. There could be up to 30 this kind of integrations. Without using Dynamic Evaluate Component, we have to write 30 similar flows. But by using Dynamic Evaluate Component, this integration can be achieved by writing one flow and the transformation can be applied dynamically.

Now the question is how can we achieve this?

Dynamic Evaluate Component

The gist of the dynamic evaluation is two steps as shown in the following diagram.

  • Step One: load the script
  • Step Two: Execute script

DWL script

Load Dataweave Scripts

XML
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/java
3
var dwlFilePath = 'classpath://dwl/' ++ (vars.myApp  default " ") ++ '.dwl'
4
---
5
readUrl(dwlFilePath, 'text/plain')



As shown above, the first step is to load the script to a variable, say, dwlScript. The xml form of the code is the following:

XML
 




xxxxxxxxxx
1
11


 
1
        <ee:transform doc:name="Read DWL Script" doc:id="3f00b2d7-3c64-48ed-ab89-9e67a278fe36" >
2
            <ee:message >
3
            </ee:message>
4
            <ee:variables >
5
                <ee:set-variable variableName="dwlScript" ><![CDATA[%dw 2.0
6
output application/java
7
var dwlFilePath = 'classpath://dwl/' ++ (vars.myApp  default " ") ++ '.dwl'
8
---
9
readUrl(dwlFilePath, 'text/plain')]]></ee:set-variable>
10
            </ee:variables>
11
        </ee:transform>



Note line 7, we predefine the application name. And the DataWeave script file name is dwl/${appName}.dwl.

Evaluate DataWeave Scripts

The Dynamic Evaluate Component takes the following form:

dataweave script

Note: the Parameters section contains objects as you do in var definition. As shown by the script. email and myDataType are refereed exactly like var.

JavaScript
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
{
5
    name: 'john doe',
6
    ssn: '123-45-3567',
7
    email: email,
8
    dataType: myDataType
9
}



Project Layout

The following is the same project layout. 

customer JSON

The source code can be found at GitHub: dynamic-evaluate-component

Here is the line of thinking:

  1. API Client pass application name
  2. Based on application name, we build dwl file path. The mapping is stored in yaml file myApp.to-json --> customer-to-json --> dwl/custom-to-json.dwl

Best Practices

  1. The naming convention of the dwl file must be clear and precise
  2. The dwl script file should contain application name dwl/${domain}-${application}.dwl, where domain could be Workday, Salesforce, etc if it is Workday or Salesforce integration.
  3. The dynamic scripts of transformation code must well be documented, otherwise, it could make later maintenance and bug fixing very challenging.
application Integration Flow (web browser) Form (document) XML Web Service Data (computing) GitHub

Opinions expressed by DZone contributors are their own.

Related

  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • MuleSoft Integration With RabbitMQ
  • How to Use IBM App Connect's ‘Mapping Assist’ to Automatically Map Your Integration Flows
  • Implementing Real-Time Datadog Monitoring in Deployments

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