Contact Us 1-800-596-4880

Type Coercion with DataWeave

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

In DataWeave, types can be coerced from one type to other using the as operator. Type coercion takes place at runtime. Before you begin, note that DataWeave version 2 is for Mule 4 apps. For a Mule 3 app, refer to the DataWeave 1.0 documentation set in the Mule 3.9 documentation. For other Mule versions, you can use the version selector for the Mule Runtime table of contents.

Note that when you provide an operator with properties that do not match the expected types, DataWeave automatically attempts to coerce the provided property to the required type.

Defining DataWeave Types For Type Coercion

The DataWeave example defines the type Currency using the String type, formats the value with the Java DecimalFormat pattern (##), and then uses as to coerce the price values to the Currency type.

Input
<items>
    <item>
        <price>22.30</price>
    </item>
    <item>
        <price>20.31</price>
    </item>
</items>
DataWeave
%dw 2.0
output application/json
type Currency = String { format: "\$#,###.00"}
---
books: payload.items.*item map
    book:
        price: $.price as Currency
Output
{
  "books": [
    {
      "book": {
        "price": "22.30"
      }
    },
    {
      "book": {
        "price": "20.31"
      }
    }
  ]
}

Type Coercion Table

This table shows the possible combinations and the properties from the schema that are used in the transformation.

Source Target Property

Range

Array

Number

Binary

String

Binary

String

Boolean

Number

DateTime

unit

LocalDateTime

DateTime

String

DateTime

format / locale

DateTime

LocalDate

LocalDateTime

LocalDate

String

LocalDate

format / locale

DateTime

LocalDateTime

String

LocalDateTime

format / locale

DateTime

LocalTime

LocalDateTime

LocalTime

Time

LocalTime

String

LocalTime

format / locale

DateTime

Number

unit

String

Number

format / locale

String

Period

String

Regex

DateTime

String

format / locale

LocalDateTime

String

format / locale

LocalTime

String

format / locale

LocalDate

String

format / locale

Time

String

format / locale

Period

String

TimeZone

String

Number

String

format / locale

Boolean

String

Range

String

Returns a string with all the values of the range using , as the separator

Type

String

DateTime

Time

LocalDateTime

Time

LocalTime

Time

String

Time

format

DateTime

TimeZone

Time

TimeZone`

String

TimeZone

Properties for Type Coercion

Property Description

class

Accepts Java classes for Object types.

format

Accepts Java DecimalFormat patterns to format numbers and dates.

locale

Accepts Java locales. A Java Locale object represents a region (geographical, political, or cultural).

unit

Value can be milliseconds or seconds. These are used for Number to DateTime conversions.

Using a Java Bridge

DataWeave enables you to call any Java static function or constructor by using the Java bridge. This feature is useful when you need to reuse business logic written in Java or to instantiate any Java object that does not have an empty public constructor.

To use a Java bridge, transform a fully qualified Java name to a DataWeave name:

  1. Add the prefix !java.

  2. Replace the dots in the fully qualified name with ::.

When invoking the Java function, DataWeave transforms each argument to the expected type in the function parameter.

The following example shows how to create a new instance of a java.lang.NullPointerException:

%dw 2.0
output application/json
---
java!java::lang::NullPointerException::new("foo")

The following example invokes the function java.lang.String.valueOf:

%dw 2.0
output text/plain
import valueOf from java!java::lang::String
---
valueOf(true)