Nav

DataWeave Types

The built-in DataWeave functions support a number of data types for elements, including arrays, Booleans, objects, dates, times, and many others described here.

Supported Types

Core types from the dw::Core) are available to most built-in DataWeave function modules. Some of the other modules define their own types.

The remaining sections below provide more detail on data types that are commonly used or sometimes misunderstood. They also cover some important topics that relate to the data type, for example:

Array (dw::Core Type)

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

Example: DataWeave Array

         
      
1
2
3
4
5
%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

          
       
1
2
3
4
%dw 2.0
output application/json
---
[(1) if true, (2) if false]
Output

          
       
1
[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

         
      
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/xml
---
{
  users:
  {
    user : "Mariano" as CData,
    age : 31 as CData
  }
}
Output

         
      
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user><![CDATA[Mariano]]></user>
  <age><![CDATA[31]]></age>
</users>
String {cdata: true}

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

          
       
1
|2003-10-01|

DateTime

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

Example

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

LocalDateTime

A DateTime in the current TimeZone.

Example

          
       
1
|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

          
       
1
2
3
4
%dw 2.0
output application/json
---
a: |20:00:00| + |PT1M1S|
Output

          
       
1
2
3
{
  "a": "20:01:01"
}

Time

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

Example

          
       
1
|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

          
       
1
|-08:00|

Date Decomposition

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

Transform

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%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

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "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

You can specify a date to be in any format you prefer through using as in the following way:

Transform

          
       
1
2
3
4
%dw 2.0
output application/json
---
formattedDate: |2003-10-01T23:57:59| as String {format: "YYYY-MM-dd"}
Output

          
       
1
2
3
{
  "formattedDate": "2003-10-01"
}

If you are doing multiple similar conversions in your transform, you might want to define a custom type as a directive in the header and set each date as being of that type.

Transform

          
       
1
2
3
4
5
6
7
8
%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

          
       
1
2
3
4
{
  "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

         
      
1
2
3
4
%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

         
      
1
2
3
4
5
6
%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

          
       
1
2
3
4
%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.


          
       
1
2
3
4
5
6
7
%dw 2.0
output application/xml
---
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).


          
       
1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<file>
  <name>transform</name>
  <extension>zip</extension>
</file>

If absent:


          
       
1
2
3
4
<?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

          
       
1
2
3
4
5
6
7
%dw 2.0
output application/json
var dynamicKey = "language"
---
{
  (dynamicKey): "Data Weave"
}
Output

          
       
1
2
3
{
  "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

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
%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

          
       
1
2
3
4
5
6
7
{
  "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

          
       
1
2
3
4
5
6
7
8
9
%dw 2.0
output application/xml
---
{
  name @(
    (company: "Acme") if false,
    (transform: "Anything") if true
  ): "DataWeave"
}
Output

          
       
1
2
<?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

          
       
1
2
3
4
{
  "company": "Mule",
  "product": "DataWeave"
}
Transform

          
       
1
2
3
4
%dw 2.0
output application/xml
---
transformation @((payload)): "Transform from anything to anything"
Output

          
       
1
2
<?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.


         
      
1
2
3
4
{
  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

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

          
       
1
2
3
4
{
  "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

         
      
1
2
3
4
5
6
7
8
9
10
{
  success: Boolean,
  result?: T,
  error?: {
    kind: String,
    message: String,
    stack?: Array<String>,
    location?: String
  }
}