Over a million developers have joined DZone.

The Power of Transformation, Part II: Selector Expressions

DZone's Guide to

The Power of Transformation, Part II: Selector Expressions

Selector expressions are an important part of every transformation that we write. They help us navigate to any part of incoming data.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

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:

  • Literal.

  • Variable reference.

  • Semi-literal.

  • Function call.

  • Selector.

  • Flow invocation.

  • Compound.

We saw few of them in my previous blog about DataWeave. Today, I will be talking about selector and comp0und expressions.

Selector 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.

Image title

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].

Image title

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.

Image title

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:

Image title

While working with XML-type objects, if you need to select XML attributes, you can use .@attribute-name.

Image title

Map Operator

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.

Image title

Here is the explanation of expression written above:

  • using (bs=payload.bookstore) defines 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 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!

Read the WSO2 Methodology for Agility to see how you can transform your integration projects from semi-agile to a scalable continuous agile approach.

mulesoft ,anypoint ,integration ,dataweave

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}