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

DataWeave - Tip #2

DZone's Guide to

DataWeave - Tip #2

In this post, we look at how developers can better manipulate the data they are working with while coding in DataWeave. Read on for more!

· Integration Zone ·
Free Resource

The new Gartner Critical Capabilities report explains how APIs and microservices enable digital leaders to deliver better B2B, open banking and mobile projects.

Tip number 2 is about converting decimal numbers into integers. This may seem tricky at first. You may say that we do not need to do anything special and the DataWeave engine will handle it underneath. However, there is a nuance that you should be aware of. When working on the transformation to XML, this may not actually work. This tip is primarily dedicated to DataWeave 1.0 as this does not occur in DataWeave 2.0.

DataWeave Data Types

Below you can see the available data types that you can use in DataWeave 1.0 and 2.0

  • Array – Ordered sequence of elements, either evaluated or literals.
["Item1", "Item2", "2.0" as :number]
  • Object – Key/value collection.
{
  Key1: "Value",
  Key2: "2.0" as :number
}
  • String – Text enclosed with quotations.
"Simple String"
  • Number – Floating point or integer value.
 1.5 or 1
  • Boolean –  Logical value, must be either true or false.
true or false
  • Date – Date and/or time. Similar to what was introduced in Java 8, we can use date and time types from java.time package. We always enclose date literals with the pipe character |.
|2017-03-27|
|2003-10-01T23:57:59|

You can find more in the Mule documentation for DataWeave 1.0 and DataWeave 2.0.

Contract

We have an array of transactions with the TransactionValue set to the Euro. I would like to transform it into an XML array where each value is in cents.

Input


{
  "ID": 5,
  "Transactions": [
    {
      "TransactionValue": 1330.5
    },
    {
      "TransactionValue": 1330
    },
    {
      "TransactionValue": 999.99
    },
    {
      "TransactionValue": 1000.0
    }
  ]
}

Output

<?xml version='1.0' encoding='windows-1250'?>
<Transactions>
  <ID>5</ID>
  <Values>
    <Value>133050</Value>
    <Value>133000</Value>
    <Value>99999</Value>
    <Value>100000</Value>
  </Values>
</Transactions>

Default Behavior

The below transformation multiplies each TransactionValue by a hundred. As I mentioned earlier, I accept decimal values, however, I need to have integers as the output of the transformation.

Image title

We receive an unexpected result:

<?xml version='1.0' encoding='windows-1250'?>
<Transactions>
  <ID>5</ID>
  <Values>
    <Value>133050.0</Value>
    <Value>133000</Value>
    <Value>99999.00</Value>
    <Value>100000.0</Value>
  </Values>
</Transactions>

As you may have noticed, some values are floating points. However, I was expecting integers. The value that had a decimal point would be left as is, without transforming it to an integer. While, in conversion to JSON, values would be transformed to integers.

But still, I want to have an XML output.

Casting to Integers

The fix is fairly simple; we need to add a floor operator, like below:

Value:floor transaction.TransactionValue * 100

After that small change I have received output that I was expecting:

Image title

Summary

In conclusion, when we would like to transform floating point numbers into integers and we are transforming to XML we need to use the floor operator to remove decimal points. This obstacle does not occur in DataWeave 2.0.

The new Gartner Critical Capabilities for Full Lifecycle API Management report shows how CA Technologies helps digital leaders with their B2B, open banking, and mobile initiatives. Get your copy from CA Technologies.

Topics:
integration ,dataweave ,data transformation ,xml ,json

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}