Import a Function/Module in Dataweave

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.

 

 

 

 

Top