Nav

To Import DataWeave Modules and Mappings

In DataWeave code, you can import additional components to extend the available variables and 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.

Importing and Reusing Standard DataWeave Modules

Here is an example of DataWeave code that imports the standard dw::Strings module, then uses the capitalize function from the dw::Strings module:

Example Module

         
      
1
2
3
4
5
6
%dw 2.0
output application/json

import dw::core::Strings
---
(Strings::dasherize ( (payload pluck $$)[0] as String) ) : Strings::capitalize( (payload pluck $)[0] )

If the payload passed to this DataWeave expression is:

Example Input Payload

         
      
1
2
3
4
{
    "some key": "bar"
}
----

then the output of this DataWeave expression will be:

Example Output

         
      
1
2
3
{
  "some-key": "Bar"
}

Writing, Importing, and Reusing Custom DataWeave Modules

You can also store any DataWeave functions and variables in external files and then import them in to any other event processor’s DataWeave expressions.

A DataWeave module file can only contain var, fun, and ns declarations. It cannot contain a body expression and it cannot include an output directive.

Here is an example DataWeave module file:

Example Module

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

Notice this file does not contain an output directive, nor does it include the separator --- nor any body expression after the separator.

Creating, Importing, and Reusing a DataWeave Mapping File as a Function

You can import any valid DataWaeve file in to another DataWeave transformation. If the DataWeave file does not contain an output directive nor a body expression, then the DataWeave file is called a DataWeave Mapping file.

Unlike a DataWeave Module, any functions and variables defined in the header are private and cannot be directly accessed through the DataWeave import declaration. However, unlike a DataWeave Module that cannot have a body expression, with a DataWeave Mapping file, the body expression is available through a function call named main, which references the body expression. The main function must be called with any expected input data, such as the payload, flow variables, or other message attributes.

Here is an example of a DataWeave mapping file:

Example Module

         
      
1
2
3
4
5
6
7
8
%dw 2.0
import dw::core::String

fun capitalizeKey(value:String) = Strings::capitalize(value) ++ "Key"
---
{
  capitalizeKey( (payload pluck $$)[0] as String ) : (payload pluck $)[0]
}

Assume this DataWeave Mapping file is stored in the Mule project’s modules folder in a file named MyMapping.dwl.

Here is an example of importing and reusing the body expression from this DataWeave Mapping file. .Example


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

When you import a DataWeave mapping 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.

Here is the result of this DataWeave mapping:

Output

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

Notice that even though the capitalizeKey function is private, it is still used through the main function call, and the DataWeave Mapping file is also able to import and reuse the dw::core::Strings module.

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"