Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

To Import DataWeave Modules

In DataWeave code, you can import additional components to extend the available functions. This may include additional modules that are packaged with Mule, such as the dw::Crypto module, or custom built modules that may contain any predefined functions you want.

Import a Module

When you import a module, all of the functions and variables that the module includes become available to use in the DataWeave body.

In the example below, the module MyModule is imported in the header. Thanks to that, the function myFunc (that is packaged in that module) can be called, which adds an underscore at the end of the string.

Example

         
      
1
2
3
4
5
%dw 2.0
import modules::MyModule
output application/json
---
MyModule::myFunc("dataweave")
Output

         
      
1
"dataweave_"

In this case, the module that’s being imported is stored in the path modules/MyModule.dwl and contains the following:

Example Module

         
      
1
2
%dw 2.0
fun myfunc(name:String) = name ++ "_"

Import an Element from a Module

You can choose to import only a specific element from a module, like a single function. This also helps you avoid typing the module name each time you refer to the element.

Example

         
      
1
2
3
4
5
%dw 2.0
import myFunc from modules::MyModule
output application/json
---
myFunc("dataweave") ++ "name"
Output

         
      
1
"dataweave_name"

In this case, the module that’s being imported is stored in the path modules/MyModule.dwl and contains the following:

Example Module

         
      
1
2
%dw 2.0
fun myfunc(name:String) = name ++ "_"

You can also invoke an element directly in the DataWeave body, without needing to import it in the header:

Example

         
      
1
2
3
4
%dw 2.0
output application/json
---
modules::MyModule::myFunc("dataweave") ++ "name"

Import All Elements from a Module

As an alternative to importing the module, you can instead import all elements from the module. The advantage of this is that it then saves you from having to type the module name each time you refer to the element.

Example

         
      
1
2
3
4
5
%dw 2.0
import * from modules::MyModule
output application/json
---
myFunc("dataweave") ++ myVar
Output

         
      
1
"dataweave_name"

In this case, the module that’s being imported is stored in the path modules/MyModule.dwl and contains the following:

Example Module

         
      
1
2
3
%dw 2.0
fun myfunc(name:String) = name ++ "_"
var myVar = "Test"

Assign a Local Alias for an Imported Element

You can assign an alternative alias to imported modules or elements from within them to avoid name clashes.

Example

         
      
1
2
3
4
5
6
%dw 2.0
import myFunc as appendDash, myVar as weaveName from modules::MyModule
var myVar = "Mapping"
output application/json
---
appendDash("dataweave") ++ weaveName ++ "_" ++ myVar
Output

         
      
1
"dataweave_name_Mapping"

You can also apply an alias to an imported module.

Example

         
      
1
2
3
4
5
%dw 2.0
import modules::MyModule as WeaveMod
output application/json
---
WeaveMod::myFunc("dataweave")
Output

         
      
1
"dataweave_"

In this case, the module that’s being imported is stored in the path modules/MyModule.dwl and contains the following:

Example Module

         
      
1
2
3
%dw 2.0
fun myfunc(name:String) = name ++ "_"
var myVar = "Test"

Import a Mapping as a Function

When you import a .dwl file, its body is considered to be an additional function named main. This function expects an input that follows the structure of the input that the transformation uses.

Example

         
      
1
2
3
4
5
%dw 2.0
import modules::MyMapping
output application/json
---
MyMapping::main({"payload": {foo: "bar"}}
Output

         
      
1
2
3
{
    "foo": "bar"
}

In this case, the module that’s being imported is stored in the path modules/MyMapping.dwl and contains the following:

Reusable Mapping

         
      
1
2
3
4
5
6
%dw 2.0
output application/json
---
{
  foo: payload.name
}

Note that there are no functions defined in this DataWeave code, yet the transformation described in the body is packaged as a function called main.