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

DataWeave - Tip #1

DZone's Guide to

DataWeave - Tip #1

An integration engineer shows us how to use DataWeave 1.0 and 2.0 to address data existence checks in our applications, and how to make this code more readable.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Tip number one will be about data existence check. There are often situations that nearly the same conditions need to be checked in every line. I have seen many transformations that were really long and complex. Reading them was not only difficult but a lot of repeatable conditional checks were made. Here I will show you an example that will evolve to a point where we can reuse everything that was possible. As a result, we should achieve more concise and readable transformations. MuleSoft is about to release new DataWeave, which is why my examples are both in 1.0 and 2.0.

Contract

Here is the input in XML format and the expected JSON result:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <product description="Cardigan Sweater" product_image="cardigan.jpg">
    <catalog_item gender="Men's">
      <item_number>QWZ5671</item_number>
      <price>39.95</price>
      <size description="Medium">
        <color_swatch image="red_cardigan.jpg">Red</color_swatch>
        <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
      </size>
      <size description="Large">
        <color_swatch image="red_cardigan.jpg">Red</color_swatch>
        <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
      </size>
    </catalog_item>
    <catalog_item gender="Women's">
    </catalog_item>
 </product>
</catalog>
{
  "Product": {
    "Man": {
      "Id": "QWZ5671",
      "Sizes": 2,
      "Price": 39
    },
    "Woman": {
    }
  }
}

First Approach to Transformation

Below I have prepared a transformation without checking if the data is provided or not. For the given input, my transformation will not work as DataWeave cannot apply the cast operator as on a null value. This transformation will only work for an input where all the data is present.

Image title

In Anypoint Studio, we should see a message like the following:

14| Sizes: sizeOf(payload.catalog.product.*catalog_item[1].*size as Array),
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Cannot coerce Null (null) to Array Trace: at 'sizeOf' in (anonymous:14:11) at 'main' in (anonymous:4:1)

Let's fix it.

Default Value

The easiest approach is to use a default operator. Its construction looks as follows:

After the keyword default, we put the value that the DataWeave engine will return if the parameter turns out to be empty. In the transformation below, you may notice that * size is set to an empty array and price is set to 0 only if corresponding parameters are empty.

Image title

DataWeave 2.0

Image title

DataWeave 1.0

And here is the output:

{ 
  "Product": { 
    "Man": { 
      "Id": "QWZ5671", 
      "Sizes": 2, 
      "Price": 39 
    }, 
    "Woman": { 
      "Sizes": 0, 
      "Price": 0 
    } 
  } 
} 

This is a fairly similar result to the expected one. The difference lies in lines 9 and 10. We would prefer to get an empty Woman object without the Sizes and Price properties.

Emptiness Check

The other solution may be a nullcheck. In order to do this, we use conditional elements. Here is the syntax:

If the condition is not met, the transformation will omit the whole element. Here is the transformation:

Image title

DataWeave 2.0

Image title

DataWeave 1.0

Using this mapping, we received the expected outcome. However, as you may see, we have a lot of repetitions. We have at least 4 additional repetitions that can be omitted. How do we remove them?

Complex Match

The last and most readable solution, in my opinion, is using match, which will evaluate the condition once and shorten the call to properties. Here is the syntax:

The Else block is not required. However, when at least one condition (case) could not be met, the transformation will throw an exception if the else block is not present. As you may also notice, match is against context and we refer to this context further using the $ variable.

Here is the transformation:

Image title

DataWeave 2.0

Image title

DataWeave 1.0

Mule will check only one condition for the Man and Woman sections. There is a verification if payload.catalog.product.*catalog_item[0] is either an object or not in line 9 and 19. Furthermore, in lines 10, 11, 20 and 21, we do not repeat the whole path to each property using the $ variable.

Summary

We may omit empty elements using conditional elements with the if keyword. On the other hand, we may use match to check if some conditions have been met. Using match we may omit a lot of the same conditional checks and hence increase readability.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
integration ,dataweave ,mulesoft ,data existence check

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}