DataWeave 2.0: Dynamic Evaluate

Jyoti Nimbalkar
Another Integration Blog
4 min readApr 8, 2024

After Reading this blog,
you’ll gain proficiency in utilizing the Dynamic Evaluate component within DataWeave 2.0.
Additionally, you’ll learn how to efficiently read multiple DWL files dynamically using a single component.

Introduction to Dynamic Evaluate

The Dynamic Evaluate component in DataWeave 2.0 allows developers to dynamically select and execute DataWeave scripts based on runtime conditions. Instead of statically defining transformation logic within a Transform Message component, the Dynamic Evaluate component enables the evaluation of an expression to determine which DataWeave script to execute. This capability empowers developers to build more adaptive integration solutions, where transformation logic can be determined dynamically at runtime.

Key Features and Benefits

1. Flexibility in Script Selection

Dynamic Evaluate provides a mechanism to dynamically choose the appropriate DataWeave script based on specific conditions. This flexibility eliminates the need for hardcoding transformation logic, enabling applications to adapt to varying requirements without manual intervention.

2. Utilization of Context Variables

The component leverages context variables such as message, payload, vars, and attributes, allowing seamless integration with existing data structures within Mule 4 applications. Additionally, developers can introduce custom context variables by providing key-value pairs, further extending the component’s flexibility and usability.

3. Enhanced Reusability

By decoupling transformation logic from the main flow and encapsulating it within separate DataWeave scripts, Dynamic Evaluate promotes code reusability. Developers can reuse existing scripts across multiple integration scenarios, fostering a modular and efficient development approach.

Practical Use Cases

Dynamic Evaluate can be applied to various integration scenarios across industries. Some common use cases include:

  • Dynamic Routing: Based on incoming data attributes or external conditions, dynamically route messages to different endpoints or processes.
  • Conditional Data Transformation: Select and execute different transformation logic based on the content or structure of incoming data, accommodating diverse data formats or business rules.
  • Dynamic Content Enrichment: Dynamically enrich message payloads with additional data retrieved from external systems or services, enhancing the information available for downstream processing.

Example Implementation:
In this tutorial, we’ll employ the dynamic evaluator to call different DWL files based on the color code received in the header. This process will generate a list of fruits corresponding to that color code.

Step 1.
Create new project which receives a color-code as a inbound parameter (header) as shown below.

First, save the header in a variable. Then, add another Set Variable component to read a DataWeave (DWL) file with the dynamically generated color-code name.

Each DWL fileName contains the color-code in it. And the dwl script looks as shown below.

%dw 2.0
output application/json
---
{
fruite_1 : pref ++ '-' ++ "Guava",
fruite_2 : pref ++ '-' ++ "Pear",
fruite_3 : pref ++ '-' ++ "Grapes",
fruite_4 : pref ++ '-' ++ "Green Apple"
}

set-variable: getdwl.

%dw 2.0
output application/dw
import fromDictionary from module::util
---
readUrl("classpath://dwl/response-" ++ vars.colorcode ++ "-fruite-list.dwl", 'text/plain')

Step 2.
Drag a dynamic evaluate component and refer to a getdwl variable in the Expression section. In order to add a color-code as a prefix to each fruit name, add a parameter called pref as shown below.

Step 3.
Run the application and send the request from postman to see the expected results.

Conclusion

Dynamic Evaluate in DataWeave 2.0 offers a powerful mechanism for enhancing flexibility and adaptability in Mule 4 integration solutions. By enabling dynamic selection and execution of DataWeave scripts based on runtime conditions, developers can build more resilient and scalable applications that effectively handle diverse data transformation requirements. Leveraging this feature empowers organizations to accelerate their digital transformation initiatives and achieve greater agility in the ever-evolving landscape of enterprise integration.

Thank you For Reading….:-)

Feel Free to Connect with me over Linkedin @: https://www.linkedin.com/in/jyoti-nimbalkar-868656154/

--

--

Jyoti Nimbalkar
Another Integration Blog

MuleSoft Ambassador | Meetup Leader|Certified MuleSoft Developer and Architect | Staff Engineer @Nagarro| https://www.linkedin.com/in/jyoti-nimbalkar-868656154/