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
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Required Capabilities in Self-Navigating Vehicle-Processing Architectures
  • What Is Ant, Really?
  • DMN 1.3 Temporal FEEL Expressions
  • The Complete Guide to Stream API and Collectors in Java 8

Trending

  • Amazon EC2 Deep Dive: Optimizing Workloads With Hardware Insights
  • Smoke Testing and the Spaceship Analogy
  • Performance of ULID and UUID in Postgres Database
  • What Are the Pillars of API Security?
  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.

Patryk Bandurski user avatar by
Patryk Bandurski
·
Mar. 26, 18 · Tutorial
Like (4)
Save
Tweet
Share
31.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 (agency)

Published at DZone with permission of Patryk Bandurski, DZone MVB. 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
  • The Complete Guide to Stream API and Collectors in Java 8

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: