%dw 2.0
output application/json
var applyMapping = (input, mappingsDef) -> (
mappingsDef map (def) -> {
(def.target) : input[def.source] default def."default"
}
)
---
payload.sfdc_users.*sfdc_user map (user) -> (
applyMapping(user, vars.mappings)
)
Dynamically Map Based on a Definition
|
DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
You can create a transformation that can dynamically change what it does depending on a definition input. This DataWeave example receives both a payload input, and a variable named mapping that specifies how to rename each field and what default value to use in each.
The example uses the following:
-
A
mapfunction to go through all of the elements in the input array. Also a secondmapfunction to go through each field in each element. -
A custom function that applies the changes specified in the
mappingvariable. -
defaultto set a default value, that comes from themappingvariable.
<sfdc_users>
<sfdc_user>
<sfdc_name>Mariano</sfdc_name>
<sfdc_last_name>Achaval</sfdc_last_name>
<sfdc_employee>true</sfdc_employee>
</sfdc_user>
<sfdc_user>
<sfdc_name>Julian</sfdc_name>
<sfdc_last_name>Esevich</sfdc_last_name>
<sfdc_employee>true</sfdc_employee>
</sfdc_user>
<sfdc_user>
<sfdc_name>Leandro</sfdc_name>
<sfdc_last_name>Shokida</sfdc_last_name>
</sfdc_user>
</sfdc_users>
mappings[
{
"source": "sfdc_name",
"target": "name",
"default": "---"
},
{
"source": "sfdc_last_name",
"target": "lastName",
"default": "---"
},
{
"source": "sfdc_employee",
"target": "user",
"default": true
}
]
[
[
{"name": "Mariano"},
{"lastName": "Achaval"},
{"user": "true"}
],
[
{"name": "Julian"},
{"lastName": "Esevich"},
{"user": "true"}
],
[
{"name": "Leandro"},
{"lastName": "Shokida"},
{"user": true}
]
]



