DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Required Capabilities in Self-Navigating Vehicle-Processing Architectures
  • What Is Ant, Really?
  • DMN 1.3 Temporal FEEL Expressions
  • An Introduction to Object Mutation in JavaScript

Trending

  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Can We Build Elite Search Agents Without Massive Industrial RL Pipelines?
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Getting Started With Agentic Workflows in Java and Quarkus
  1. DZone
  2. Data Engineering
  3. Data
  4. DataWeave - Tip #1

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.

By 
Patryk Bandurski user avatar
Patryk Bandurski
·
Mar. 26, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
32.2K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Element Data (computing) Property (programming) Blocks Operator (extension) Syntax (programming languages) Object (computer science) Release (computing)

Published at DZone with permission of Patryk Bandurski. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Required Capabilities in Self-Navigating Vehicle-Processing Architectures
  • What Is Ant, Really?
  • DMN 1.3 Temporal FEEL Expressions
  • An Introduction to Object Mutation in JavaScript

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook