Dataweave syntax for importing mappings file in Mulesoft

Importing a Mapping File in a DataWeave Script of Mule

Import mappings in Dataweave


We use mapping files whenever we want to map a value to a field in the incoming payload. To use a mapping file, we need to import it into a DataWeave script using Transform message component and use the main function to access the body of the script in the mapping file.

Consider your mappings file which is mappings.dwl file is located in /src/main/resources/modules that contains this script.


To import and use body expression from the mappings.dwl file (above) in DataWeave Mapping file, you need to do this:
  • Specify the import directive in the header.

  • Invoke the mappings::main function. The function expects an input that follows the structure of the input that the mapping file uses. For example, the body of mappings.dwl expects an object of the form {"key" : "value"}

How to remove objects with null values in Dataweave?

Example: Import and use the Mapping in a DataWeave Script

Dataweave script:

%dw 2.0
import modules::mappings
output application/json
---
mappings::main(payload: { "empName" : "Uday" })

Output
{
"EmpName": "Uday"
}




Post a Comment

0 Comments