Author: Abhinav Patel
Welcome all, to the DataWeave series for Mule 4.
If you have not gone through the previous articles, I would recommend you go through them first, to get a basic understanding of DataWeave. In this article, we will try to continue the working of joins in dw 2.2.
In the previous blog, we established the relation between two arrays based on the values that were stored in the object. But in real life project scenarios, this might not be always the case. The data can be stored in any format and now its the role of a developer to dig out the relevant information and build the relationship.
Let’s consider the data mentioned below that describes the details about employee and some access cards, stored in the form of an array.
After analysing the data we can conclude that:
- In empDetails, the employee Id forms the key for on object
- In accessCaerdDetails, the employee ID is stored in one of attribute “empID”
Based on these observations we can build the relationship between the two data set:
Let’s now try to use join operation to build a logical data structure so that we can draw some logics out of it:
This join will help us to fetch the details of all the employees who have been allocated an access card. The details for a given employee will be clubbed together, in a single object as per the format discussed in the previous blog.
Remember this particular format is known as Pair.
The left join will help to retrieve the details of all the employees. This will even have details of employees that haven’t received an access card. This can help us to figure who all haven’t received their access card details yet as shown below.
By using outer join we will try to generate a consolidated result, in such a manner that if the employee has an access card the details will be clubbed together or else individual details of the access card or employee will form the part of the output array. We will now organise these records so as to develop a more human-readable output format as shown below:
Now, this is something that will come handy in your actual project. The keys “l” and “r” would not be able to explain their existence all alone, unless someone is already aware of the existence of join operation. So, I have just made an attempt to replace them with more logical Key names, representing the array from which the record has been picked.
I hope you got to learn something new from this walkthrough. Also if you have any use case, feel free to share it at firstname.lastname@example.org.