HowTo – Implement logic handling in DataWeave

November 27 2017

0 comments 0
dataweave howto

Logic handling using DataWeave is essential for simple mediums and highly complex transformations, in which the mapping requirements necessitate generating outputs based on values provided in the input payload.  

This could be as simple as checking whether certain field(s)/attribute(s) in the input payload are empty or null, then setting a corresponding field(s)/attribute(s) to a certain value. It could also involve the ability to set the structure/values of the output payload based on whether a collection/array in the input payload is empty or not. In addition, it could be as complex as having different set of mappings, conditionally based on the value of certain fields/attributes.

This HowTo guide will provide examples of how to do the following:

  1. Build if/else logic based on logical conditions on input payload fields
  2. Check fields for nulls
  3. Handle empty XML nodes

Step 1: Build If/Else Logic

1. In this step, you will build if/else logic based on logical conditions on input payload fields. In order to implement conditional mappings in DataWeave with if/else logic, you will provide the when/or/otherwise syntax. To illustrate this, we will look at two DataWeave snippets for an airline flight scheduling scenario.

The following is a simple if/else scenario, where we check the input for a specific value and set the output accordingly. Otherwise, we default the value to something else when the input doesn’t match the condition:

2. Below is a more complex case in which there are many mappings between input and output values. Here, we are setting the value of a field called “FlightState” to 6 possible values, which are based on 11 possible values that are sent in a field called “payload.flightDetail.state”:

Step 2: Checking fields for nulls

1. In DataWeave, when a field in the input payload has a value and you append a question mark (“?”) at the end of the field, it will return true.  On the other hand, if the field is null, then it would return false.  Going back to our airline flight scheduling scenario, below is an example in which the airline needs to generate an output field called “OperEstDepTime,” but only when a particular input field is not null.  

Flight Details:

Step 3: Handling empty XML nodes

1. Transforming XML payloads can be tricky if you need to handle scenarios where you are mapping an XML collection, which is potentially empty. To illustrate this, consider a scenario where you have an XML payload that represents an invoice.  This object has a set of header levels at the root node, referred to as “Invoice-header.” It also has a collection of “Invoice-lines,” which represent individual line items on the invoice.  Within each “Invoice-line,” there could be either no Account allocation fields or many of them.

Input:

2. If we were to simply map the fields in DataWeave as follows – and we do not handle the possibility that <account-allocations> is in fact null – then the code would involve the following:

3. You will then see the following exception on the map operator:

ERROR 2017-07-01 16:03:39,059 [[responsetimetracker].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy:

***************************************************************

Message               : Exception while executing:

Account-Allocations: payload.invoice-header.invoice-lines.account-allocations map

                    ^

Type mismatch for ‘map’ operator

    found :null, :function

 required :array, :function.

***************************************************************

To handle the fact that <account-allocations> is empty in the sample input above, before we map the Account related fields that can potentially be sent in the input payload we need to use the when/otherwise structure to check if <account-allocations> is not null, blank, or contains a blank collection represented by “[]”:

 

4. After these steps, we can correctly generate the output payload, even when the <account-allocations> collection in the input payload is blank as shown above.  DataWeave will then generate the following output successfully:

Output:

 

In providing logic handling features, DataWeave enables Mule developers to easily build complex mappings in order to implement a myriad of use cases in a quick and intuitive fashion.  With logic handling, teams can effectively shorten development time and, in turn, achieve faster time to market.

For more information, check our Mule User Guide for DataWeave and discover the role of DataWeave in Mule 4.


 


We'd love to hear your opinion on this post