Importing a Mapping File in a DataWeave Script of Mule

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 ofmappings.dwl
expects an object of the form{"key" : "value"}
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"
}
0 Comments