As the name suggests, this post is to capture few tips while using DataWeave for easy transformation of incoming payload into desired payload:
If this then that
Conditional logic in DataWeave is achieved by using when and otherwise. Below example shows two usecases of this:
Line 5, assigings the “valueToUse” node based on the incoming field or default value specified in property file
Line6, causes the node itself to be removed/added based on the incoming field
Filtering
filter keyword is extremely useful and convenient when you want to get object(s) out of an array or a single value out of an array of objects
Filtering object(s) from array of objects
With below expression, you can filter out data to list all the employee names with role as “dev
Filtering single value from array of objects
If your need is to retrieve only the first value out of the array of objects, DW expression can be modified as below:
Functions in DataWeave
There are multiple approaches you can take to define a function to be used inside DataWeave expression.
- Defining a function inside DataWeave
- Defining a global MEL functions
- Defining a function in Java
It depends on individual scenario for which one should be used. My take on this would be to define anything very simple inside DataWeave, a re-usable and not complex function can be defined as a global spring function while a medium/high complex function should be defined in Java. I choose this approach for test ability reasons. If you have a complex function or a function doing too many things, make sure you can write tests against it.
Defining a function inside DataWeave
Functions can be defined in two ways within DataWeave code, either by using %function or %var. Below snippet shows use of both of these approaches. Note that %var directive is actually using lambda expression to accept parameters and evaluate the result.
Another thing to note is after the map operator, I am using $ to get reference to the object inside mapping. The same can also be written as
Defining a global MEL function
Functions can also be defined within global configuration element in an xml file within the project and invoked from DataWeave. These are MEL functions which gives ability to use MEL features inside the function.
Above functions also show that you can use values defined in property files or message property similar to how you would within a MEL expression.
Defining a java function
In this technique, you will be writing a java code and invoking that from within global MEL function which can then be called from DataWeave.
Mapping
String to Map
Imagine you want to store a mapping collection(key:value pair) in the property file(which is really a string) and then be able to access it as a real collection during DataWeave transformations. This is pretty useful when you want to retrieve the value based on the keys from the collection. To explain it a bit more, lets say you have a collection of keys based on roles which have different level of access to your api. Ex: “dev”: “level1access”; “mgr”: “level2access”;”default”:”level0access”. Now you want to get the value of this key based on the role of incoming request.
There are multiple ways to achieve this,
- Define a variable like dev.roleaccess=level1access, mgr.roleaccess=level2access and pick the value at runtime based on the role of the user.
- Another approach will be to maintain this as key:value pair separated by comma/semicolon in one variable and late in the code split to form a MappedList.
If you take first approach you will either need to write a java/groovy script to get the property value at runtime by using System.getProperty or you can also use DataWeave to evaluate and fetch the property value by simply using p(employee.role ++ ‘.roleaccess’)
Second approach is little tricky, you will need to split the string twice and form an object out of the split string. Below is example
The second approach has a fallback value of assigning default value in case we can’t find the mapped role in the incoming payload. Below is the output for above dataweave expressionHoping this was useful. If you need more, there are tons of articles on dzone.com related to DataWeave which may be helpful for your need in one way or other.. Happy Weaving!!