Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Power of Transformation, Part III: Filtering

DZone's Guide to

Power of Transformation, Part III: Filtering

Filter operators in DataWeave allow us to only see the elements or keys that we want to, based the criteria that we choose to specify.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90 day trial 3Scale by Red Hat

In continuation of my previous blogs about DataWeave in Mule ESB, I will be writing today about filters in DataWeave. If you are interested in reading previous blogs, you can read them here:

  1. The Power of Transformation, Part I: An Introduction to DataWeave.
  2. The Power of Transformation, Part II: Selector Expressions.

So, let's get started with the filters in DataWeave.

Filters

In DataWeave, we can chain many different expressions together to form compound expressions while using various kind of operators. Many times there are requirements during our transformation that we want to filter some data before applying next operator. To do this, we can use filter operator. Filter operator iterates through elements in an array or keys in an object and results in an array that only contains the elements that match the criteria specified by the filter operator.

Let's take an example of below XML data.

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>

  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>

  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>

</bookstore>

So, we want to filter the above XML data and only operate on books that have a price above $35.00. We can do this very easily by using filter operator on our transformation.

First, I will show you the output without filter operator and then the output with filter operator applied on the transformation.

Without:

Image title

With filter operator:

Image title

Notice the difference in the output; now, the output is being filtered with the books that have their price above $35.00.

A point to note here is the filter condition expressed in the right-hand operand makes reference to $.priceThis key is the key that is the output of the map operation of transformation, it not the same that of input, I could have easily named it cost in my map transformation and could have used it instead of price. The example is below; take a note of $.cost:

Image title

The filter operator is being applied to the output produced by the Map operator. Map produces an array that becomes the left-hand operand of the filter operator, which iterates through the array and produces an array of objects filtering out those objects which fail the criteria.

Conditional Filtering

Many times during your transformation, you will face a situation where you only want a type of data in your output if it matches some kind of criteria. Doing this in DataWeave is very simple. We call it conditional filtering since the output is based on some condition.

Taking the same XML in our example above, suppose we only want the name of the author in our output if the book category is of children — else, we don't want to give author name in our output data.

Image title

Note that I have surrounded the entire author key:value pair in parentheses, this is the left-hand operand to the when operator. The right-hand operand is a boolean expression. Only when this evaluates to true is the wind key:value pair included in the output.

These are the two ways you can do filtering in DataWeave. I will continue to write about more complex DataWeave transformation in my future blogs. Do let me know your thoughts about it.

Discover how you can achielve enterpriese agility with microservices and API management

Topics:
mule esb ,integration ,dataweave ,tutorial ,filters

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}