Contact Us 1-800-596-4880

Type Coercion with DataWeave

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 version 1 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).

mode

Parsing mode for date and time values. Valid values: SMART, STRICT, LENIENT

Note that LENIENT and SMART autocorrect invalid dates in different ways, but STRICT returns an error on the date. See examples that use mode.

unit

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

The following examples show uses of the mode property. Notice that LENIENT and SMART return valid dates when they receive an invalid date such as 02/31/2020, but the resulting dates differ. STRICT returns an error on an invalid date. The examples use uuuu to represent the year instead of yyyy.

DataWeave script:
%dw 2.0
output application/json
---
{
  examples : {

    badDateWithLenient: '02/31/2020' as Date {mode: "LENIENT", format: 'MM/dd/uuuu'},
    badDateWithSmart: '02/31/2020' as Date {mode: "SMART", format: 'MM/dd/uuuu'}
  }
}
Output:
%dw 2.0
output application/json
---
{
  "examples": {
    "badDateWithLenient": "03/02/2020",
    "badDateWithSmart": "02/29/2020"
  }
}

Using STRICT on an invalid date returns an error. The following example is an error returned by badDateWithStrict: '02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}:

"Cannot coerce String (02/31/2020) to Date, caused by: Text '02/31/2020' could not be parsed: Invalid date 'FEBRUARY 31'

9| 	badDateWithSmart:
    '02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The following example converts two strings into a date format and concatenates the result. The first string represents the date, and the second string represents the time. The transformation uses as to coerce the first string to LocalDateTime, and then to a String with the specified format. The transformation also uses ++ to concatenate the result.

DataWeave script:
%dw 2.0
output application/json
var s1= 20201228 // (uuuuMMdd),
var s2= 1608 //(HHMM)
---
(s1 ++ s2)
as LocalDateTime {format:"uuuuMMddHHMM"}
as String {format:"MM-dd-uuuu HH:MM:ss"}
Output:
"12-28-2020 16:08:00"

The following example also converts a string into a date format. First, the transformation uses as to coerce the string into a LocalDateTime type, and then it coerces the result into a String type with the specified output format.

DataWeave script:
%dw 2.0
output application/json
---
"8/30/2020 4:00:13 PM"
    as LocalDateTime {format: "M/dd/uuuu H:MM:ss a"}
    as String {format: "MM/dd/uuuu"}
Output:
"08/30/2020"

The following example converts a string to a number with decimal representation. First, you transform the string value "22" to Number and then to String again because format adds zeros when converting from a number to a string. If the input data is the number 22, it is not necessary to perform the first Number conversion. The latter Number conversion makes the output a number with the decimal representation.

DataWeave script:
%dw 2.0
output application/json
---
{
     data: "22" as Number as String {format: ".00"} as Number
}
Output:
{
  "data": 22.00
}

The following example uses the locale property to format number and date values. First, you coerce a number value into String type to the specified output format that uses the Java DecimalFormat pattern (##) and also the locale property en (English) or es (Spanish). The locale: "en" property, formats the output number decimal representation using a ., while the locale: "es" property, formats the output using a ,.
Then, you coerce a date value into Date type and then to String type with the output format dd-MMM-yy and the locale property en (English), or es (Spanish). The locale: "en" property, formats the month MMM in English, while the locale: "es" property formats the month in Spanish.

DataWeave script:
%dw 2.0
output application/json
---
{
    enNumber: 12.3 as String {format: "##.##", locale: "en"},
    esNumber: 12.3 as String {format: "##.##", locale: "es"},
    esDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "es"},
    enDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "en"}
}
Output:
{
  "enNumber": "12.3",
  "esNumber": "12,3",
  "esDate": "31-dic.-20",
  "enDate": "31-Dec-20"
}