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.
partial$dataweave1-links.adoc
The example uses the following:
-
A
map
function to go through all of the elements in the input array. Also a secondmap
function to go through each field in each element. -
A custom function that applies the changes specified in the
mapping
variable. -
default
to set a default value, that comes from themapping
variable.
DataWeave Script:
link:partial$cookbook-dw/map-based-on-an-external-definition/transform.dwl[role=include]
Input XML Payload:
link:partial$cookbook-dw/map-based-on-an-external-definition/inputs/payload.xml[role=include]
Input Mule Event Variable (JSON):
link:partial$cookbook-dw/map-based-on-an-external-definition/inputs/vars/mappings.json[role=include]
Output JSON:
link:partial$cookbook-dw/map-based-on-an-external-definition/out.json[role=include]