raw is not working incase of retrieving the value form a variable in dataweave 2 mule

I’m just trying to convert the numeric values to strings without losing their decimal values, including zeros.

This can be done using the raw operator in DataWeave 2.0. In below I converted a 123.0 numeric value to a “123.0” string value.

enter image description here

But when I tries the same using mule variable, the expression throws an error like “Cannot coerce Null to String”.

enter image description here

I tried code using raw operator for a variable in DataWeave 2.0.

%dw 2.0
output application/json
var inputValue = payload.message
---
inputValue.^raw as String

Any ideas, please, on how to use the raw operator on a variable or how to convert numeric values to string without losing their decimal values?

eg: 123.0 to “123.0”, 123.00 to “123.00”, 123.63 to “123.63”

  • This is an incorrect usage of raw. Also note that a DataWeave variable is not a Mule variable.

    – 




  • @aled, so how to convert numeric values to string without losing their decimal values without using raw?

    – 

  • In the JSON standard numbers don’t have a representation of trailing zeros nor format.

    – 

  • In case If user would like to see trailing zeros as string, then how we can do this?

    – 

  • I added a response with an extended explanation. Also see @Karthik answer.

    – 

Trailing zeros in numbers with a decimal part are not part of the number in JSON, like in many other languages. When a number is parsed from a JSON document by a JSON parsing library you should not expect it to contain a specific format. Just the numeric values. Since trailing zeros don’t add any value in most implementations they are lost. Using raw in DataWeave is returning the actual input string that the parser read. In your first example there is no need to actually convert to String since it is being transformed to a String in the output. If you instead change the input to { a:123.00} you will see the output is "{ a:123.00}\n", preserving even the spacing. Note that this input is not even valid JSON. Raw is not converting the to a formatted number, which is a concept that doesn’t exists in JSON, DataWeave nor Java.

When you use in any way that numeric value then the raw value from the input is lost. Only the numeric value remains. Since 123.00 or 123.0 are really the number 123 it should be expected that is the value preserved. Although you may have a different result with other implementation, that is outside of the JSON standard.

If you want the numeric value to have a specific format you to format it explicitly into a String. If you want to consider the number the decimals from the input the you should receive it from the input, in a separate field or receiving the value as a String, which may have other issues (see https://stackoverflow.com/a/74289642/721855).

The way that DataWeave has to convert a number to a formatted string with decimals is to use a format in the conversion:

%dw 2.0
output application/json
---
payload.a as String {format:"0.00"}

That will return a String that will have two decimal places. For example "123.00" for inputs { "a":123.00 }, { "a":123.0 } or { "a":123 }

Leave a Comment