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.
Join the DZone community and get the full member experience.
Join For Freetip 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.
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.
dataweave 2.0
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:
dataweave 2.0
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:
dataweave 2.0
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.
Published at DZone with permission of Patryk Bandurski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments