Contact Us 1-800-596-4880

DataWeave Types

DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends.

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.

DataWeave functions operate on data of many types, including arrays, Booleans, objects, dates, and times. The types that DataWeave provide are bundled into modules that also contain the related functions.

Types in the dw::Core module (Core types) are available without having to import the Core module. Other modules need to be imported for their functions and types to be available.

This topic provides an introduction to some DataWeave types, and descriptions of common patterns, including:

Array (dw::Core Type)

An array can hold elements of any supported type. Here is an example of an array:

Example: DataWeave Array
%dw 2.0
output application/json
var x = "words"
---
[ "My", "three", x ]

Conditional Elements

Arrays can define elements that appear (or not) based on a condition.

Conditional elements take the form (value) if condition, for example:

Example: if Condition
%dw 2.0
output application/json
---
[(1) if true, (2) if false]
Output
[1]

Boolean (dw::Core Type)

A Boolean is defined by the keywords true and false.

CData (dw::Core Type)

XML defines a custom type named CData, which inherits from and extends String. It is used to identify a CDATA XML block. It can be used to tell the writer to wrap the content inside CDATA or to check if the input string arrives inside a CDATA block.

Transform
%dw 2.0
output application/xml encoding="UTF-8"
---
{
  users:
  {
    user : "Mariano" as CData,
    age : 31 as CData
  }
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user><![CDATA[Mariano]]></user>
  <age><![CDATA[31]]></age>
</users>

Date and Time (dw::Core Types)

Dates in DataWeave follow the ISO-8601 standard and literals are defined between | characters.

The language has the following native date types:

  • Date

  • DateTime

  • LocalDateTime

  • LocalTime

  • Period

  • Time

  • TimeZone

Date

A Date represented by Year, Month, and Day, specified as |yyyy-MM-dd|. The Date type has no time component.

Example
|2003-10-01|

DateTime

A Date and Time within a TimeZone. It is the conjunction of Date + Time + TimeZone.

Example
|2003-10-01T23:57:59-03:00|

LocalDateTime

A DateTime in the current TimeZone.

Example
|2003-10-01T23:57:59|

LocalTime

A Time in the current TimeZone.

Period

Periods have the form:

  • P[n]Y[n]M[n]DT[n]H[n]M[n]S

  • P<date>T<time>`

Where the [n] is replaced by the value for each of the date and time elements that follow the [n].

P is the duration designator placed at the start of the duration representation.

  • Y is the year designator (e.g. |P1Y|)

  • M is the month designator (e.g. |P1M|)

  • D is the day designator (e.g. |P1D|)

T is the time designator that precedes the time components of the representation.

  • H is the hour designator (e.g. |PT1H|)

  • M is the minute designator (e.g. |PT1M|)

  • S is the second designator (e.g. |PT1S|)

Transform
%dw 2.0
output application/json
---
a: |20:00:00| + |PT1M1S|
Output
{
  "a": "20:01:01"
}

Time

A time in a specific TimeZone, specified as |HH:mm:ss.SSS|.

Example
|23:59:56|

TimeZone

The Time relative to Greenwich Mean Time (GMT). A TimeZone must include a + or a -. For example, |03:00| is a time, while |+03:00| is a TimeZone.

Example
|-08:00|

Date Decomposition

To access the different parts of the date, special selectors must be used.

Transform
%dw 2.0
output application/json
var myDate = |2003-10-01T23:57:59.700-03:00|
---
{
  year: myDate.year,
  month: myDate.month,
  day: myDate.day,
  hour: myDate.hour,
  minutes: myDate.minutes,
  seconds: myDate.seconds,
  milliseconds: myDate.milliseconds,
  nanoseconds: myDate.nanoseconds,
  quarter: myDate.quarter,
  dayOfWeek: myDate.dayOfWeek,
  dayOfYear: myDate.dayOfYear,
  offsetSeconds: myDate.offsetSeconds
}
Output
{
  "year": 2003,
  "month": 10,
  "day": 1,
  "hour": 23,
  "minutes": 57,
  "seconds": 59,
  "milliseconds": 700,
  "nanoseconds": 700000000,
  "quarter": 4,
  "dayOfWeek": 3,
  "dayOfYear": 274,
  "offsetSeconds": -10800
}

Changing the Format of a Date

To format dates and times, DataWeave supports some formatting characters that are based on the Java 8 java.time.format package, such as the y, M, and d in the date format yyyy-MM-dd. The following example formats the output of the now DataWeave function to show supported letters:

DataWeave Script
%dw 2.0
output application/json
---
[
  { "dateTime" : now() },
  { "era-G" : now() as String { format: "G"} },
  { "year-u" : now() as String {format: "u"} },
  { "year-uu" : now() as String {format: "uu"} },
  { "year-y" : now() as String { format: "y"} },
  { "year-yy" : now() as String { format: "yy"} },
  { "dayOfYear-D" : now() as String { format: "D"} },
  { "monthOfYear-MMMM": now() as String { format: "MMMM"} },
  { "monthOfYear-MMM": now() as String { format: "MMM"} },
  { "monthOfYear-MM": now() as String { format: "MM"} },
  { "monthOfYear-M": now() as String { format: "M"} },
  { "monthOfYear-LL": now() as String { format: "LL"} },
  { "monthOfYear-L": now() as String { format: "L"} },
  { "dayOfMonth-d" : now() as String {format: "d"} },
  { "quarterOfYear-qqq" : now() as String {format: "qqq"} },
  { "quarterOfYear-qq" : now() as String {format: "qq"} },
  { "quarterOfYear-q" : now() as String {format: "q"} },
  { "quarterOfYear-QQQQ" : now() as String {format: "QQQQ"} },
  { "quarterOfYear-QQQ" : now() as String {format: "QQQ"} },
  { "quarterOfYear-QQ" : now() as String {format: "QQ"} },
  { "quarterOfYear-Q" : now() as String {format: "Q"} },
  // Understand "Y" and "YY" thoroughly before using it.
  { "weekBasedYear-YY" : now() as String { format: "YY"} },
  { "weekBasedYear-Y" : now() as String { format: "Y"} },
  { "weekInYear-w" : now() as String {format: "w"} },
  { "weekInMonth-W" : now() as String {format: "W"} },
  { "dayOfWeekAbbreviatedName-E" : now() as String {format: "E"} },
  { "dayOfWeekFullName-EEEE" : now() as String {format: "EEEE"} },
  { "localizedDayOfWeek-eeee" : now() as String {format: "eeee"} },
  { "localizedDayOfWeek-eee" : now() as String {format: "eee"} },
  { "localizedDayOfWeek-ee" : now() as String {format: "ee"} },
  { "localizedDayOfWeek-e" : now() as String {format: "e"} },
  { "localizedDayOfWeek-cccc" : now() as String {format: "cccc"} },
  { "localizedDayOfWeek-ccc" : now() as String {format: "ccc"} },
  { "localizedDayOfWeek-c" : now() as String {format: "c"} },
  { "weekOfMonth-F" : now() as String {format: "F"} },
  { "amORpm-a" : now() as String {format: "a"} },
  // "h" outputs 12 o'clock as 12. Other hours match "K" output.
  { "hourOfDay1to12-h" : now() as String {format: "h"} },
  // "K" outputs 12 o'clock as 0. Other hours match "h" output.
  { "hourOfDay0to11-K" : now() as String {format: "K"} },
  { "clockHourOfAmPm-k" : now() as String {format: "k"} },
  { "hourOfDay0to23-H" : now() as String {format: "H"} },
  { "minuteOfHour-m" : now() as String {format: "m"} },
  { "secondOfMinute-s" : now() as String {format: "s"} },
  { "fractionOfSecond-S" : now() as String {format: "S"} },
  { "millisecondOfDay-A" : now() as String {format: "A"} },
  { "nanosecondCountOfSecond-n" : now() as String {format: "n"} },
  { "nanosecondCountOfDay-N" : now() as String {format: "N"} },
  { "timeZoneID-VV" : now() as String {format: "VV"} },
  { "timeZoneName-zz" : now() as String {format: "zz"} },
  { "localizedZoneOffset-OOOO" : now() as String {format: "OOOO"} },
  { "localizedZoneOffset-O" : now() as String {format: "O"} },
  { "timeZoneOffsetZforZero-XXX" : now() as String {format: "XXX"} },
  { "timeZoneOffsetZforZero-XX" : now() as String {format: "XX"} },
  { "timeZoneOffsetZforZero-X" : now() as String {format: "X"} },
  { "timeZoneOffset-xxx" : now() as String {format: "xxx"} },
  { "timeZoneOffset-xx" : now() as String {format: "xx"} },
  { "timeZoneOffset-x" : now() as String {format: "x"} },
  { "timeZoneOffset-Z" : now() as String {format: "Z"} },
  { "timeZoneOffset-ZZZZ" : now() as String {format: "ZZZZ"} }
 ]

Notice the use of the syntax to format the date or time.

Output
[
  { "dateTime": "2019-04-10T14:20:14.271-07:00" },
  { "era-G": "AD" },
  { "year-u": "2019" },
  { "year-uu": "19" },
  { "year-y": "2019" },
  { "year-yy": "19" },
  { "dayOfYear-D": "100" },
  { "monthOfYear-MMMM": "April" },
  { "monthOfYear-MMM": "Apr" },
  { "monthOfYear-MM": "04" },
  { "monthOfYear-M": "4" },
  { "monthOfYear-LL": "04" },
  { "monthOfYear-L": "4" },
  { "dayOfMonth-d": "10" },
  { "quarterOfYear-qqq": "2" },
  { "quarterOfYear-qq": "02" },
  { "quarterOfYear-q": "2" },
  { "quarterOfYear-QQQQ": "2nd quarter" },
  { "quarterOfYear-QQQ": "Q2" },
  { "quarterOfYear-QQ": "02" },
  { "quarterOfYear-Q": "2" },
  { "weekBasedYear-YY": "19" },
  { "weekBasedYear-Y": "2019" },
  { "weekInYear-w": "15" },
  { "weekInMonth-W": "2" },
  { "dayOfWeekAbbreviatedName-E": "Wed" },
  { "dayOfWeekFullName-EEEE": "Wednesday" },
  { "localizedDayOfWeek-eeee": "Wednesday" },
  { "localizedDayOfWeek-eee": "Wed" },
  { "localizedDayOfWeek-ee": "04" },
  { "localizedDayOfWeek-e": "4" },
  { "localizedDayOfWeek-cccc": "Wednesday" },
  { "localizedDayOfWeek-ccc": "Wed" },
  { "localizedDayOfWeek-c": "4" },
  { "weekOfMonth-F": "3" },
  { "amORpm-a": "PM" },
  { "hourOfDay1to12-h": "2" },
  { "hourOfDay0to11-K": "2" },
  { "clockHourOfAmPm-k": "14" },
  { "hourOfDay0to23-H": "14" },
  { "minuteOfHour-m": "20" },
  { "secondOfMinute-s": "14" },
  { "fractionOfSecond-S": "2" },
  { "millisecondOfDay-A": "51614271" },
  { "nanosecondCountOfSecond-n": "271000000" },
  { "nanosecondCountOfDay-N": "51614271000000" },
  { "timeZoneID-VV": "America/Los_Angeles" },
  { "timeZoneName-zz": "PDT" },
  { "localizedZoneOffset-OOOO": "GMT-07:00" },
  { "localizedZoneOffset-O": "GMT-7" },
  { "timeZoneOffsetZforZero-XXX": "-07:00" },
  { "timeZoneOffsetZforZero-XX": "-0700" },
  { "timeZoneOffsetZforZero-X": "-07" },
  { "timeZoneOffset-xxx": "-07:00" },
  { "timeZoneOffset-xx": "-0700" },
  { "timeZoneOffset-x": "-07" },
  { "timeZoneOffset-Z": "-0700" },
  { "timeZoneOffset-ZZZZ": "GMT-07:00" }
]

You can combine the formatting letters to write supported date an time formats into a string, using as in the following way:

Transform
%dw 2.0
output application/json
---
formattedDate: |2003-10-01T23:57:59| as String {format: "yyyy-MM-dd"}
Output
{ "formattedDate": "2003-10-01" }

For multiple, similar conversions in your script, you might want to define a custom type as a directive in the header and set each date to that type.

Transform
%dw 2.0
output application/json
type Mydate = String { format: "yyyy/MM/dd" }
---
{
  formattedDate1: |2003-10-01T23:57:59| as Mydate,
  formattedDate2: |2015-07-06T08:53:15| as Mydate
}
Output
{
  "formattedDate1": "2003/10/01",
  "formattedDate2": "2015/07/06"
}

Note that type names are case-sensitive.

Enum (dw::Core Type)

This type is based on the Enum Java class. It must always be used with the class property, specifying the full Java class name of the class, as shown in this example.

Transform
%dw 2.0
output application/java
---
"Male" as Enum {class: "com.acme.GenderEnum"}

Iterator (dw::Core Type)

The Iterator type is based on the Iterator Java class, that iterates through arrays. Iterator contains a collection and includes methods to iterate through and filter it.

Note that like the Java class, the iterator is designed to be consumed only once. For example, if you then pass this value to a logger would result in consuming it and it would no longer be readable to further elements in the flow.

Number (dw::Core Type)

There is only one number type that supports both floating point and integer numbers. There is no loss of precision in any operation, the engine always stores the data in the most performant way that does not compromise precision.

Object (dw::Core Type)

Represents any object as a collection of key:value pairs.

Example
%dw 2.0
output application/json
---
{
  name: "Annie"
}

Single Value Objects

If an Object has only one key:value pair, the enclosing curly brackets { } are not required:

Example
%dw 2.0
output application/json
---
name: "Annie"

Conditional Elements

Objects can define conditional key-value pairs based on a conditional expression. Conditional elements have the form (key:value) if condition.

%dw 2.0
output application/xml encoding="UTF-8"
---
file: {
  name: "transform",
  (extension: "zip") if payload.fileSystem?
}

This example outputs an additional field called "extension" only when the fileSystem property is present in payload (this field may contain any value, not just true).

<?xml version="1.0" encoding="UTF-8"?>
<file>
  <name>transform</name>
  <extension>zip</extension>
</file>

If absent:

<?xml version="1.0" encoding="UTF-8"?>
<file>
  <name>transform</name>
</file>

Dynamic Keys

To specify a key through an expression, you need to wrap the expression in parentheses.

Transform
%dw 2.0
output application/json
var dynamicKey = "language"
---
{
  (dynamicKey): "Data Weave"
}
Output
{
  "language": "Data Weave"
}

Dynamic Elements

Dynamic elements allow you to add the result of an expression as key:value pairs of an object. That expression must be either an object or an array of objects.

Transform
%dw 2.0
output application/json
var x = [
  {b: "b"},
  {c: "c", d: "d"}
]
var y = {e: "e"}
---
{
  a: "a",
  (x),
  (y)
}

It is important to note that the expression between the parentheses (such as (x) where x is the variable shown in the header) should return an array of objects. All of objects in that array get merged together. They are also merged with the contained object. So the output looks like this:

Output
{
  "a": "a",
  "b": "b",
  "c": "c",
  "d": "d",
  "e": "e"
}

Conditional XML Attributes

You might want your output to only include certain XML attributes based on a condition. Conditional elements have the form `(key:value) if condition

Transform
%dw 2.0
output application/xml
---
{
  name @(
    (company: "Acme") if false,
    (transform: "Anything") if true
  ): "DataWeave"
}
Output
<?xml version='1.0' encoding='US-ASCII'?>
<name transform="Anything">DataWeave</name>

Dynamic XML Attributes

You might want to include a changing set of key:value pairs in a specific location as XML attributes.

Input
{
  "company": "Mule",
  "product": "DataWeave"
}
Transform
%dw 2.0
output application/xml
---
transformation @((payload)): "Transform from anything to anything"
Output
<?xml version='1.0' encoding='US-ASCII'?>
<transformation company="Mule" product="DataWeave">Transform from anything to anything</transformation>

Regex (dw::Core Type)

Regular Expressions are defined between /. For example /\d+/ represents multiple numerical digits from 0-9. These may be used as arguments in certain operations that act upon strings, like Matches or Replace, or on operations that act upon objects and arrays, such as filters.

String (dw::Core Type)

A string can be defined by the use of double quotes or single quotes.

{
  doubleQuoted: "Hello",
  singleQuoted: 'Hello',
}

String Interpolation

String interpolation allows you to embed variables or expressions directly in a string. The expression need to be enclosed ($( <expression> )), and it should always return String type or something that is coercible to String.

Transform
%dw 2.0
output application/json
var name = "Shoki"
---
{
    Greeting: "Hi, my name is $name",
    Sum: "1 + 1 = $(1 + 1)"
}
Output
{
  "Greeting": "Hi, my name is Shoki",
  "Sum": "1 + 1 = 2"
}

TryResult (dw::Runtime Type)

Evaluates the delegate and returns an object with the result or an error message. See the try example. A successful TryResult contains the result field and a success value of true. An unsuccessful TryResult contains the error field and a success value of false.

Definition
{
  success: Boolean,
  result?: T,
  error?: {
    kind: String,
    message: String,
    stack?: Array<String>,
    location?: String
  }
}

You can find complete data type documentation in the Module reference pages: