Type Coercion with DataWeave | MuleSoft Documentation
# 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.
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  
---|---|---  
`LocalDateTime`  
`format` / `locale`  
`LocalDate`  
`LocalDateTime` | `LocalDate`  
`LocalDate` | `format` / `locale`  
`LocalDateTime`  
`LocalDateTime` | `format` / `locale`  
`LocalTime`  
`LocalDateTime` | `LocalTime`  
`LocalTime` | `format` / `locale`  
`format` / `locale`  
`format` / `locale`  
`LocalDateTime` | `format` / `locale`  
`LocalTime` | `format` / `locale`  
`LocalDate` | `format` / `locale`  
`format` / `locale`  
`format` / `locale`  
Returns a string with all the values of the range using `,` as the separator  
`LocalDateTime`  
### Properties for Type Coercion
Property | Description  
---|---  
Accepts Java classes for Object types.  
Accepts Java `DecimalFormat` patterns to format numbers and dates.  
Accepts Java locales. A Java `Locale` object represents a region (geographical, political, or cultural).  
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`.  
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:
This example converts a number to a string with a decimal representation:
DataWeave script:
```
%dw 2.0
output application/json
---
{
   data: 22 as String {format: ".00"}
}
```

Output:
```
{
 "data": "22.00"
}
```

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.   
---  
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"
}
```

## See Also
  * Format Dates and Times
  * Supported Data Formats