This is a continuation of my previous blog regarding DataWeave in the MuleSoft Anypoint Platform, where I gave an introduction about the canonical format of the DataWeave transformation engine. Today, I will be writing about some more features and functions available in DataWeave.
When we work with DataWeave, our complete transformation is covered in a single expression. In the previous blog, I showed you how to write semi-literal expressions to define the outer structure of a transformation as either an object or an array. It is possible to write other expressions of different types inside these expressions.
These are basic expression types in DataWeave:
We saw few of them in my previous blog about DataWeave. Today, I will be talking about selector and comp0und expressions.
Selector expressions are an important part of every transformation that we write. They help us navigate to any part of incoming data — whether it's in the payload, variables, or properties.
An 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 a variable expression such as payload. Selectors are only useful when used in the proper context such as an object or array, so make sure you use them for the object or array. If you use them on a simple type, it will always result in null. In the 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 of the array, with the array beginning with the second element and ending with the last element of the array on which x would be indexed x[1..-1].
Object Selector Expressions
Single/Multiple Key Selector
In DataWeave, objects are a combination of key:value pairs in which the keys can be duplicated. There are situations where 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 changing selectors to get to the value of author deep inside the input object XML. The above example also gives an example of the multi-key selector, which can be selected by
.*key-value . Below is another example of the selector:
While working with XML-type objects, if you need to select XML attributes, you can use
We can use the Map operator on objects to do iterations on the objects. It takes as operands as expressions, 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, the 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 the input XML object. Let’s use most of what we have seen until now to create the expression below.
Here is the explanation of expression written above:
using (bs=payload.bookstore)defines a local variable before the object expression covers our transformation. Variable
bsis local to the object expression to which the declaration is created. The scope of this variable is local to the expression transformation is created.
mapwill iterate on each element in the array returned by
.*bookand 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.
$.@categoryis the selector expression used to access the value of the category attribute.
- For simple string concatenation, we use the
With this, we come to the end of this part of DataWeave transformations. I will continue to explore more about the power of transformation and keep sharing here. Do let me know your thoughts about Dataweave and how it has helped you solve difficult transformations!