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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

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

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Dynamically Evaluate Dataweave Scripts
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Rust’s Ownership and Borrowing Enforce Memory Safety

Trending

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  1. DZone
  2. Coding
  3. Java
  4. Import a Function/Module in Dataweave

Import a Function/Module in Dataweave

This tutorial explains how many ways we can import Function or Module in Dataweave - Mule 4. Read the article below to find out how!

By 
$$anonymous$$ user avatar
$$anonymous$$
·
Nov. 17, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
19.7K Views

Join the DZone community and get the full member experience.

Join For Free

Why Should We Use Import in the First Place?

In Dataweave, whenever we need to use some function/module either that is not part of the core or custom-made by you or your fellow developer you need to use the import keyword to import the respective function/module.

Before going into the details we need to understand that a Function is part of the Module and there are many other modules available in Dataweave for our use.

As an example in this article, we will be focusing on specific functions called SubString and reverse from a module Strings which is not imported to Dataweave by default. The same process is applicable to any other module.

For interactive learning use this platform by Mulesoft to execute the below code snippets. 


How Do You Import Any Function?

We can import any function into Dataweave using 3 types of approaches.

1. Import Module

This approach is recommended when we know we will be using functions from a specific module but are not sure which functions will be needed. 

This way we can simply import the module without specifying the functions’ names just by giving import dw::core::Strings in the header (before ---). When we do this, we need to refer to the imported module every time we want to use a function from the module. 

As we are using the substring function in the Strings module first we need to refer to the Strings module and then the function we are gonna use by keeping the scope resolution operator (::)  in between them Strings::substring.

Java
 
%dw 2.0
output application/json
import dw::core::Strings
---
{
   sub_string :  Strings::substring("I am Max Mule",5,13),
   reverse_string : Strings::reverse("tfoseluM")
}


Output: 

Output screenshot.


Dataweave will give the following error if we do not use the module name before the function name:

Error screenshot.


2. Import Everything from a Module

If you are going to use a function in 100 places or 100 functions from a module, it’s an extra effort to keep the module name prefixed to the function name every time we use it. But, by using this way we can import all functions into Dataweave and eliminate referencing of the module.

Here we use an asterisk (*) in the import statement to import all functions from module eg.  import * from dw::core::Strings. Now, we can directly use the respective function without referring to the module that is imported from.

Java
 
%dw 2.0
output application/json
import * from dw::core::Strings
---
{
   sub_string :  substring("I am Max Mule",5,13),
   reversed_string : reverse("tfoseluM")
}


Output:

Output screenshot.

Vice versa to the first approach Dataweave will give the following error if we use the module name before the function name.

Error 2 screenshot.


3. Import What You Need 

This approach is recommended when we know exactly which functions we are going to use from the module. Instead of an asterisk (*) we will specify the function names that we intend to use eg. import substring, reverse from dw::core::Strings

A point to remember while using this approach is if we can not use any other function of the imported module unless it is explicitly mentioned in the import statement.

Java
 
%dw 2.0
output application/json
import substring,reverse from dw::core::Strings
---
{
   sub_string :  substring("I am Max Mule",5,13),
   reversed_string : reverse("tfoseluM")
}


Output:

Output screenshot.

Dataweave will throw the following error if you call any function which is not defined in the import. Here we have deleted the reverse from the import statement while keeping the function call as it is.

Error screenshot.

Conclusion

In conclusion, it’s up to the developer to choose which approach is best suited based on the requirements.

Interactive Learning Strings Java (programming language) Asterisk (PBX) IT EGS (program) dev Snippet (programming) Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • Dynamically Evaluate Dataweave Scripts
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Java String: A Complete Guide With Examples
  • Rust’s Ownership and Borrowing Enforce Memory Safety

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!