---
schema_version: "1"
id: 5375f3b82b3df9fdec955fa4066c7d775403e912e56213adcb18f1c0ca98040e
title: Type Coercion with DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dataweave-types-coercion.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dataweave-types-coercion
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0344e8a246b4fe5948988bf65d8614fb76cddb3ea8a71220cf71fa456461aff5
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
keywords: studio, anypoint, esb, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Type Coercion with DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

In DataWeave, types can be coerced from one type to other using the `as` operator. Type coercion
takes place at runtime.

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

```xml
<items>
    <item>
        <price>22.30</price>
    </item>
    <item>
        <price>20.31</price>
    </item>
</items>
```

DataWeave

```dataweave
%dw 2.0
output application/json
type Currency = String { format: "\$#,###.00"}
---
books: payload.items.*item map
    book:
        price: $.price as Currency
```

Output

```json
{
  "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.

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Source</th><th>Target</th><th>Property</th></tr></thead><tbody><tr><td><p><code>Range</code></p></td><td><p><code>Array</code></p></td><td></td></tr><tr><td><p><code>Number</code></p></td><td><p><code>Binary</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Binary</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Boolean</code></p></td><td></td></tr><tr><td><p><code>Number</code></p></td><td><p><code>DateTime</code></p></td><td><p><code>unit</code></p></td></tr><tr><td><p><code>LocalDateTime</code></p></td><td><p><code>DateTime</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>DateTime</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>LocalDate</code></p></td><td></td></tr><tr><td><p><code>LocalDateTime</code></p></td><td><p><code>LocalDate</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>LocalDate</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>LocalDateTime</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>LocalDateTime</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>LocalTime</code></p></td><td></td></tr><tr><td><p><code>LocalDateTime</code></p></td><td><p><code>LocalTime</code></p></td><td></td></tr><tr><td><p><code>Time</code></p></td><td><p><code>LocalTime</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>LocalTime</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>Number</code></p></td><td><p><code>unit</code></p></td></tr><tr><td><p><code>Period</code></p></td><td><p><code>Number</code></p></td><td><p><code>unit</code></p></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Number</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Period</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Regex</code></p></td><td></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>LocalDateTime</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>LocalTime</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>LocalDate</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>Time</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>Period</code></p></td><td><p><code>String</code></p></td><td></td></tr><tr><td><p><code>TimeZone</code></p></td><td><p><code>String</code></p></td><td></td></tr><tr><td><p><code>Number</code></p></td><td><p><code>String</code></p></td><td><p><code>format</code> / <code>locale</code></p></td></tr><tr><td><p><code>Boolean</code></p></td><td><p><code>String</code></p></td><td></td></tr><tr><td><p><code>Range</code></p></td><td><p><code>String</code></p></td><td><p>Returns a string with all the values of the range using <code>,</code> as the separator</p></td></tr><tr><td><p><code>Type</code></p></td><td><p><code>String</code></p></td><td></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>Time</code></p></td><td></td></tr><tr><td><p><code>LocalDateTime</code></p></td><td><p><code>Time</code></p></td><td></td></tr><tr><td><p><code>LocalTime</code></p></td><td><p><code>Time</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>Time</code></p></td><td><p><code>format</code></p></td></tr><tr><td><p><code>DateTime</code></p></td><td><p><code>TimeZone</code></p></td><td></td></tr><tr><td><p><code>Time</code></p></td><td><p><code>TimeZone</code></p></td><td></td></tr><tr><td><p><code>String</code></p></td><td><p><code>TimeZone</code></p></td><td></td></tr></tbody></table>

### Properties for Type Coercion

<table><colgroup><col> <col></colgroup><thead><tr><th>Property</th><th>Description</th></tr></thead><tbody><tr><td><p><code>class</code></p></td><td><div><div><p>Accepts Java classes for Object types.</p></div></div></td></tr><tr><td><p><code>format</code></p></td><td><div><div><p>Accepts Java <code>DecimalFormat</code> patterns to format numbers and dates.</p></div></div></td></tr><tr><td><p><code>locale</code></p></td><td><div><div><p>Accepts Java locales. A Java <code>Locale</code> object represents a region (geographical, political, or cultural).</p></div></div></td></tr><tr><td><p><code>mode</code></p></td><td><div><div><p>Parsing mode for date and time values. Valid values: <code>SMART</code>, <code>STRICT</code>, <code>LENIENT</code></p></div><div><p>Note that <code>LENIENT</code> and <code>SMART</code> autocorrect invalid dates in different ways, but <code>STRICT</code> returns an error on the date. See <a href="#modeExamples">examples</a> that use <code>mode</code>.</p></div></div></td></tr><tr><td><p><code>unit</code></p></td><td><div><div><p>Value can be <code>milliseconds</code> or <code>seconds</code>. These are used for Number to DateTime, DateTime to Number, and Period to Number conversions.</p></div></div></td></tr></tbody></table>

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:

```dataweave
%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:

```json
%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'}`:

```txt
"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:

```dataweave
%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:

```json
"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:

```dataweave
%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:

```json
"08/30/2020"
```

This example converts a number to a string with a decimal representation:

DataWeave script:

```dataweave
%dw 2.0
output application/json
---
{
     data: 22 as String {format: ".00"}
}
```

Output:

```json
{
  "data": "22.00"
}
```

> [!NOTE] To maintain consistency across all formats, starting with DataWeave 2.5, writing number
> values directly doesn’t preserve formatting and strips trailing zeroes. Format metadata is only
> used to parse the provided string into the correct number representation. To retain this behavior,
> you can re-enable it by using the `stripTrailingZeroes` property. See
> [DataWeave System Properties](https://docs.mulesoft.com/dataweave/latest/dataweave-system-properties).

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:

```dataweave
%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:

```json
{
  "enNumber": "12.3",
  "esNumber": "12,3",
  "esDate": "31-dic.-20",
  "enDate": "31-Dec-20"
}
```

## See Also

- [Format Dates and Times](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-format-dates)

- [Supported Data Formats](https://docs.mulesoft.com/dataweave/latest/dataweave-formats)
