Import a Function/Module in Dataweave

Author: Upendra Thunuguntla

Why Should We Use Import in the First Place?

In Dataweave, whenever we need to use some function/module that is not part of the core or custom-made by you or your fellow developer, we 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 to Import any function?

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

1. Just 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 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 will use by keeping :: in between them“`Strings::substring“`.

“`

%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 : 

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

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, e.g. “`import * from dw::core::Strings“`.  Now, we can directly use the respective function without referring to the Module that is imported from.

“`

%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:

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

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 we intend to use, e.g.,“`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 explicitly mentioned in the import statement.

“`

%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:

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.

In conclusion, it’s up to the developer to choose which approach should be used based on the situation.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.