Dataweave is a new data mapping tool which comes with MuleSoft 3.7 run time. Before Mule 3.7 runtime, Datamapper was there for data mapping. Dataweave inherit some of the functionality from Datamapper but due to restriction of complex mapping in Datamapper, Dataweave emerged with Mulsesoft 3.7 runtime.
There are three section of Dataweave.
1) Input
2) Output
3) Data transformation language
Data transformation language is based on JSON like language. If you are familiar with JSON, it is very easy to write Dataweave transformation logic and maintain this logic.
Here are some tips to write and maintain Dataweave transformation logic.
1) Default Output of Dataweave Transformation is set into payload. But you can easily change from dropdown and set this output as variable or session variable
2) Output of transformed data you can easily define in Dataweave. If you are transforming data into XML you just need to define as “ %output application/xml” without touching underline transformation logic. Same way if you are transforming your data into json or any other format you just need to define output like without touching underline transformation logic as “ %output application/json”, “ %output application/java”, “ %output application/csv”..
%dw 1.0
%output application/xml
%namespace ns0 http://schemas.vanrish.com/2010/01/ldaa
—
3) Dataweave transformation logic gives leverage to skip any null field during data transformation. This is only declarative. Here is declaration to skipping null fields everywhere during transformation skipNullOn=”everywhere”
%dw 1.0
%output application/xml skipNullOn=”everywhere”
%namespace ns0 http://schemas.vanrish.com/2010/01/ldaa
—
4) Dataweave transformation allows to access flowVars and sessionVars directly into transformation field.
orderParameter:{
name:“MINOR_LI_ID”,
value:flowVars.payloadVar.minorLiId
}
5) Dataweave transformation reads properties value directly from properties file. You can access properties value in Dataweave like you are accessing during flow.
teamName:“$($.teameName)${teamNameSuffice}“,
teamNameSuffice is defined in properties file.
6) Dataweave transformation allows implementing condition logic for each field. It is very easy to implement and transform your data based on these condition logic. Here is example to implement to condition logic for field partnershipType.
partnershipType:“NEW” when $.partnership-type==”NEW”
otherwise “USED” when $.partnership-type==”USED”
otherwise “EM” when $.partnership-type==”EM”
otherwise “PU” when $.partnership-type==”PU”
otherwise “PN” when $.partnership-type==”PN”
otherwise $.partnership-type,
Here is another example
creativeType:“GRAPHIC” when ($.creative-type == “GRAPHIC” or $.creative-type == null)
otherwise “TEMPLATED_AD” when $.creative-type == “TEMP_AD”
otherwise “AD_TAG” when $.creative-type == “AD_TAG”
otherwise “”,
7) During Dataweave transformation logic you can call global function and transform your field based on these function output. This is one of the ways you can call java object into Dataweave transformation.
Here is example
global-functions is tag for mule-config file where you can define function for entire mule flow
<global-functions>
def toUUID() {
return java.util.UUID.randomUUID().toString()
}
def getImageType(imageName) {
return imageName.substring(imageName.lastIndexOf(‘.’)+1)
}
</global-functions>
Now you can call this function inside Dataweave transformation logic.
imageType:getImageType($.origin-name as :string) when $.origin-name != null otherwise “”,