Tip number 2 is about converting decimal number into integer one. This may seem tricky at first. You may say that we do not need to do anything special and DataWeave engine will handle it underneath. However there is a nuance that you should be aware of. In transformation to XML this may not actually works. This tip is primary dedicated to DataWeave 1.0 as in DataWeave 2.0 this does not occur.

DataWeave data types

Below you can see 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 true either false
true or false
  • Date – date or/and Time. Similar to newly introduced in Java 8 date and time types from java.time package. Date literals we enclose always with pipe character|
|2017-03-27|
|2003-10-01T23:57:59|

More you can find in Mule documenation for DataWeave 1.0 and DataWeave 2.0.

Contract

We have an array of Transactions witch TransactionValue in Euro. I would like to transform it into an XML array where each value is in Cents.

Input

<br />
{<br />
  &quot;ID&quot;: 5,<br />
  &quot;Transactions&quot;: [<br />
    {<br />
      &quot;TransactionValue&quot;: 1330.5<br />
    },<br />
    {<br />
      &quot;TransactionValue&quot;: 1330<br />
    },<br />
    {<br />
      &quot;TransactionValue&quot;: 999.99<br />
    },<br />
    {<br />
      &quot;TransactionValue&quot;: 1000.0<br />
    }<br />
  ]<br />
}<br />

Output

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

Default behavior

Below transformation multiplies each TransactionValue by a hundred. As I mentioned earlier I accept decimal values however I need to have integers after transformation.

DataWeave 1.0 transformation

We receive an unexpected result:

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

As you may have noticed some values are floating point. However I was expecting integers. Value that had decimal point would be left as is, without transforming to integer.  While in conversion to Json, values would be transformed to integers.

But still I want to have an XML output.

Casting to integer

Fix is fairly simple we need to add floor operator like below

Value: floor transaction.TransactionValue * 100

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

Transformation output

 

Summary

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

DataWeave – Tip #2
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *