In continuation of my previous blog regarding Dataweave in Mulesoft Anypoint platform where I gave a introduction about canonical format of dataweave transformation engine, I will be writing about some more features and functions available in dataweave, You can read my previous blog here https://echarish.wordpress.com/2016/11/15/power-of-transformation-dataweave-introduction/
When we work with the dataweave, our complete transformation is covered in a single expression. In previous blog I showed you how to write semi-literal expressions to define the outer structure of transformation as either an object or an array. It is possible to write other expressions of different type inside these expressions.
There are 7 basic expression types in dataweave
- Variable reference
- Function call
- Flow invocation
We saw few of them in my previous blog about dataweave https://echarish.wordpress.com/2016/11/15/power-of-transformation-dataweave-introduction/ today I will be talking about Selector and Comp0und expressions.
Selector expressions are important part of every transfomation we write. They help us navigate to any part of incoming data be it be in the payload, variables or the properties.
Important thing to be aware of while using selector expressions is their context and their result. They can be chained together to go deeper into the objects. The result of each selector in the chain sets the context(object or array) against which the next selector is evaluated.
Typically the first context is the result of variable expression, such as payload. selectors are only useful when used on proper context such as object or array, so make sure you use them for object or array because if you use them on simple type it will always result in null. In case of String type, strings are treated as an array.
Array Element Selector Expressions
Arrays are indexed with the elements of arrays available from [0..n-1], indices in the array can be negative as well, where -1 indicates the last element on the array, with the array beginning with the second element and ending with the last element on the array x would be indexed x[1..-1].
Object Selector Expressions
Single/Multiple Key Selector
In DataWeave objects are combination of key:value pairs in which the keys can be duplicated. There are situations were we need to retrieve particular values from chained within the object. To do this we will need to use the key selector, written in the form .key-name The output will be the value corresponding to the first instance of the key.
In the example above we are chaning selectors to get to the value of author deep inside the input object xml. the above example also gives a example of multi key selector, multi key selector can be selected by .*key-value. below is another example of selector.
While working with XML type object if you need to select xml attributes then you can use .@attribute-name
We can use Map operator on objects to do iterations on the objects. It takes as operands as an expression which must return an array or an object on the left-hand side and any expression on the right. The result of applying map is an array where each element is the result of the right-hand operand. If the left-hand operand is an array, map will iterate through each element and add the result of the right-hand operand to the output array. If the left-hand operand is an object, map will iterate on the sequence of key:value pairs.
Using our above example of books Let’s create a object of books by taking and merging few of the fields of input xml object, Let’s use most to what we have seen till now in crating the expression below.
Here are the explanation of expression written above.
- Using (bs=payload.bookstore) here we are defining a local variable before the object expression covers our transformation. Variable, bs, is local to the object expression to which the declaration is created.The scope of this variable is local to the expression transformation is created.
- Map will iterate on each element in the array returned by .*book and add the object defined as its right operand to the resulting array.
- $ is an alias for the element found at each iteration on the array.
- $.@category is the selector expression used to access the value of the category attribute.
- For simple string concatenation, we use the ++ operator.
With this we come to the end of this part of Dataweave transformation. I will continue to explore more about power of transformation and keep sharing here. Do let me know your thoughts about Dataweave and how it helped you to solve difficult transformations.