%dw 1.0
%output application/json
---
users: ["john", "peter", "matt"] map upper $
DataWeave Operators
In DataWeave you can carry out many different operations on the elements of a DataWeave transform. This document serves as a reference for all of the available operators in the DataWeave language. See all operators sorted by type
-
For an introduction to the essentials of the language, see DataWeave Language Intro
-
For information on DataWeave’s accepted types, see DataWeave Types
-
For an index of all available operators, categorized by the types you need to supply as parameters, see DataWeave Operators Sorted by Type.
-
See Functions and Lambdas to learn how to create your own DataWeave functions.
Check the Precedence Table to see the order in which DataWeave expressions are compiled. |
Map
Using Map to Return an Array
Returns an array that is the result of applying a transformation function (lambda) to each of the elements. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $.
{
"users": [
"JOHN",
"PETER",
"MATT"
]
}
In the following example, custom names are defined for the index and value parameters of the map operation, and then both are used to construct the returned value. In this case, value is defined as firstName and its index in the array is defined as position.
%dw 1.0
%output application/json
---
users: ["john", "peter", "matt"] map ((firstName, position) -> position ++ ":" ++ upper firstName)
{
"users": [
"0:JOHN",
"1:PETER",
"2:MATT"
]
}
Using Map on an Object
Returns an array with the values that result out of applying a transformation function (lambda) to each of the values in the object. The keys of the original object are all ignored by this operation and the object is treated as an array. To have access to the keys, you can use the operation mapObject instead. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $. The index refers to the position of a key:value pair when the object is treated as an array.
See Map Object if what you want is to process both keys and values instead of just values. |
%dw 1.0
%output application/json
%var conversionRate=13.45
---
priceList: payload.prices map (
'$$':{
dollars: $,
localCurrency: $ * conversionRate
}
)
Input: XML
<prices>
<basic>9.99</basic>
<premium>53</premium>
<vip>398.99</vip>
</prices>
Output: JSON
{
"priceList": [
{
"0": {
"dollars": "9.99",
"localCurrency": 134.3655
}
},
{
"1": {
"dollars": "53",
"localCurrency": 712.85
}
},
{
"2": {
"dollars": "398.99",
"localCurrency": 5366.4155
}
}
]
}
Note that when you use a parameter to populate one of the keys of your output, as with the case of in this example, you must either enclose it in quote marks or brackets. '' or ($$) are both equally valid. |
In the example above, as key and value are not defined, they’re identified by the placeholders $$ and $. For each key:value pair in the input, an object is created and placed in an array of objects. Each of these objects contains two properties: one of these directly uses the value, the other multiplies this value by a constant that is defined as a directive in the header.
The mapping below performs exactly the same transform, but it defines custom names for the properties of the operation, instead of using $ and $$. Here, position
is defined as referring to the array index, and money
to the value in that index. The reference to the array index (named position
, in this case) is optional.
%dw 1.0
%output application/json
%var conversionRate=13.45
---
priceList: payload.prices map ((money, position) ->
'$position':{
dollars: money,
localCurrency: money * conversionRate
}
)
The reference to the array index is optional. This is also a valid example:
.Transform
%dw 1.0
%output application/json
%var conversionRate=13.45
---
priceList: payload.prices map ((money) ->
{
dollars: money,
localCurrency: money * conversionRate
}
)
Note that when you use a parameter to populate one of the keys of your output, as with the case of position in this example, you must either enclose it in brackets or enclose it in quote marks adding a $ to it, otherwise the name of the property is taken as a literal string. '$position' or (position) are both equally valid.
|
Map Object
Similar to Map, but instead of processing only the values of an object, it processes both keys and values as a tuple. Also instead of returning an array with the results of processing these values through the lambda, it returns an object, which consists of a list of the key:value pairs that result from processing both key and value of the object through the lambda.
The lambda is invoked with two parameters: key and the value. If these parameters are not named, the key is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
%var conversionRate=13.45
---
priceList: payload.prices mapObject (
'$$':{
dollars: $,
localCurrency: $ * conversionRate
}
)
Input: XML
<prices>
<basic>9.99</basic>
<premium>53</premium>
<vip>398.99</vip>
</prices>
Output: JSON
{
"priceList": {
"basic": {
"dollars": "9.99",
"localCurrency": 134.3655
},
"premium": {
"dollars": "53",
"localCurrency": 712.85
},
"vip": {
"dollars": "398.99",
"localCurrency": 5366.4155
}
}
}
Note that when you use a parameter to populate one of the keys of your output, as with the case of in this example, you must either enclose it in quote marks or brackets. '' or ($$) are both equally valid. |
In the example above, as key and value are not defined, they’re identified by the placeholders $$ and $. For each key:value pair in the input, the key is preserved and the value becomes an object with two properties: one of these is the original value, the other is the result of multiplying this value by a constant that is defined as a directive in the header.
The mapping below performs exactly the same transform, but it defines custom names for the properties of the operation, instead of using $ and $$. Here, 'category' is defined as referring to the original key in the object, and 'money' to the value in that key.
%dw 1.0
%output application/json
%var conversionRate=13.45
---
priceList: payload.prices mapObject ((money, category) ->
'$category':{
dollars: money,
localCurrency: money * conversionRate
}
)
Note that when you use a parameter to populate one of the keys of your output, as with the case of category in this example, you must either enclose it in brackets or enclose it in quote marks adding a $ to it, otherwise the name of the property is taken as a literal string. '$category' or (category) are both equally valid. |
Pluck
Pluck is useful for mapping an object into an array. Pluck is an alternate mapping mechanism to mapObject. Like mapObject, pluck executes a lambda over every key:value pair in its processed object as a tuple, but instead of returning an object, it returns an array, which may be built from either the values or the keys in the object.
The lambda is invoked with two parameters: key and the value. If these parameters are not named, the key is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
---
result: {
keys: payload.prices pluck $$,
values: payload.prices pluck $
}
Input: XML
<prices>
<basic>9.99</basic>
<premium>53</premium>
<vip>398.99</vip>
</prices>
Output: JSON
{
"result": {
"keys": [
"basic",
"premium",
"vip"
],
"values": [
"9.99",
"53",
"398.99"
]
}
}
Filter
Using Filter on an Array
Returns an array that only contains those that pass the criteria specified in the lambda. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
---
{
biggerThanTwo: [0, 1, 2, 3, 4, 5] filter $ > 2
}
{
"biggerThanTwo": [3,4,5]
}
Using Filter on an Object
Returns an object with the key:value pairs that pass the acceptance criteria defined in the lambda. If these parameters are not named, the index is defined by default as $$ and the value as $.
%dw 1.0
%output application/xml
---
filtered: {
aa: "a", bb: "b", cc: "c", dd: "d"
} filter $ == "d" (1)
1 | Filters the all key:value pairs with value "d" ⇒ {dd:d} |
<?xml version="1.0" encoding="UTF-8"?>
<filtered>
<dd>d</dd>
</filtered>
If you require to filter by key, you need to use mapObject and when. For example, to filter the last example by key:
|
Remove
Using Remove on an Array
When running it on an array, it returns another array where the specified indexes are removed.
%dw 1.0
%output application/json
---
{
aa: ["a", "b", "c"] - 1
}
{
"aa": [a, c]
}
Using Remove on an Object
When running it on an object, it returns another object where the specified keys are removed.
%dw 1.0
%output application/json
---
myObject: {aa: "a", bb: "b"} - "aa"
{
"myObject": {
"bb": "b"
}
}
The above example removes the key value pair that contains the key 'aa' from {aa: "a", bb: "b"} ⇒ {bb: "b"}
Remove by Matching Key and Value
Works just like remove on objects, but only removes an element when there is a match of not just the key but of the key + value pair . It returns another object where the specified keys are removed.
%dw 1.0
%output application/json
---
myObject: {aa: "a", aa:"c", bb: "b"} -- { aa:"a"}
{
"myObject": {
"aa": "c",
"bb": "b"
}
}
The above example removes the key value pair that contains both the key 'aa' and value "a", but not the one that contains only a matching key but not value.
AND
The expression and (in lower case) can be used to link multiple conditions, its use means that all of the linked conditions must evaluate to true for the expression as a whole to evaluate to true.
%dw 1.0
%output application/json
---
{
currency: "USD"
} when payload.country == "USA" and payload.currency == "local"
otherwise
{
currency: "EUR"
}
In the example above, currency is "EUR", unless the payload has BOTH conditions met.
Check the Precedence Table to see what expressions are compiled before or after this one. |
OR
The expression or (in lowercase) can be used to link multiple conditions. Its use means that either one or all of the linked conditions must evaluate to true for the expression as a whole to evaluate to true. This example combines the usage of OR with the when and otherwise expressions.
%dw 1.0
%output application/json
---
{
currency: "EUR"
} when payload.country == "Italy" or payload.country == "Germany" or payload.country == "Spain" or payload.country == "Portugal" or payload.country == "France" or payload.country == "Greece"
otherwise
{
currency: "USD"
}
In the example above, currency is "EUR", only when one of the conditions evaluates to true.
Check the Precedence Table to see what expressions are compiled before or after this one. |
IS
Evaluates if a condition validates to true and returns a boolean value. Conditions may include and
and or
operators.
%dw 1.0
%output application/xml
---
ROOT: payload.root.*order mapObject (
ORDER:{
itemsCollectionPresent: $ is :object and $.items?
}
)
Input: XML
<root>
<order>
<items> 155 </items>
</order>
<order>
<items> 30 </items>
</order>
<order>
null
</order>
</root>
Output: XML
<?xml version='1.0' encoding='UTF-8'?>
<ROOT>
<ORDER>
<itemsCollectionPresent>true</itemsCollectionPresent>
</ORDER>
<ORDER>
<itemsCollectionPresent>true</itemsCollectionPresent>
</ORDER>
<ORDER>
<itemsCollectionPresent>false</itemsCollectionPresent>
</ORDER>
</ROOT>
Concat
The concat operator is defined using double plus signs. You must have spaces on both sides of them.
Using Concat on an Array
When using arrays, it returns the resulting array of concatenating two existing arrays.
%dw 1.0
%output application/json
---
{
a: [0, 1, 2] ++ [3, 4, 5]
}
{
"a": [0, 1, 2, 3, 4, 5]
}
Using Concat on a String
Strings are treated as arrays of characters, so the operation works just the same with strings.
%dw 1.0
%output application/json
---
{
name: "Mule" ++ "Soft"
}
{
"name": MuleSoft
}
Using Concat on an Object
Returns the resulting object of concatenating two existing objects.
%dw 1.0
%output application/xml
---
concat: {aa: "a"} ++ {cc: "c"}
<?xml version="1.0" encoding="UTF-8"?>
<concat>
<aa>a</aa>
<cc>c</cc>
</concat>
The example above concatenates object {aa: a} and {cc: c} in a single one ⇒ {aa: a , cc: c}
Contains
Evaluates if an array or list contains in at least one of its indexes a value that validates to true and returns a boolean value. You can search for a literal value, or match a regex too.
Using Contains on an Array
You can evaluate if any value in an array matches a given condition:
%dw 1.0
%output application/json
---
ContainsRequestedItem: payload.root.*order.*items contains "3"
Input: XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<order>
<items>155</items>
</order>
<order>
<items>30</items>
</order>
<order>
<items>15</items>
</order>
<order>
<items>5</items>
</order>
<order>
<items>4</items>
<items>7</items>
</order>
<order>
<items>1</items>
<items>3</items>
</order>
<order>
null
</order>
</root>
Output: JSON
{
"ContainsRequestedItem": true
}
Using Contains on a String
You can also use contains to evaluate a substring from a larger string:
%dw 1.0
%output application/json
---
ContainsString: payload.root.mystring contains "me"
Input: XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>some string</mystring>
</root>
Output: JSON
{
"ContainsString": true
}
Instead of searching for a literal substring, you can also match it against a regular expression:
%dw 1.0
%output application/json
---
ContainsString: payload.root.mystring contains /s[t|p]ring/`
Input: XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<mystring>A very long string</mystring>
</root>
Output: JSON
{
"ContainsString": true
}
Type Coercion using as
Coerce the given value to the specified type.
DataWeave by default attempts to convert the type of a value before failing, so using this operator to convert is sometimes not required but still recommended. |
Check the type coercion table to see what conversions between what types are allowed in DataWeave. |
Coerce to string
Any simple types can be coerced to string. If formatting is required (such as for a number or date) the format schema property can be used.
Date and number format schemas are based on Java DateTimeFormatter and DecimalFormat.
%dw 1.0
%output application/json
---
{
a: 1 as :string {format: "##,#"},
b: now as :string {format: "yyyy-MM-dd"},
c: true as :string
}
{
"a": "1",
"b": "2015-07-07",
"c": "true"
}
Coerce to number
A string can be coerced to number. If the given number has a specific format the schema property can be used.
Any format pattern accepted by DecimalFormat is allowed.
%dw 1.0
%output application/json
---
{
a: "1" as :number
}
%dw 1.0
%output application/json
---
{
"a": 1
}
Coerce a date to number
When coercing a date to a number, there is an extra parameter you can add – 'unit' – to specify what unit of time to use,
%dw 1.0
%output application/json
---
{
mydate1: |2005-06-02T15:10:16Z| as :number {unit: "seconds"},
mydate2: |2005-06-02T15:10:16Z| as :number {unit: "milliseconds"}
}
{
"mydate1": 1117725016,
"mydate2": 1117725016000
}
Only the values 'seconds' and 'milliseconds' are valid for using in the 'unit' parameter. |
Coerce to date
Date types can be coerced from string or number.
Any format pattern accepted by DateTimeFormatter is allowed.
%dw 1.0
%output application/json
---
{
a: 1436287232 as :datetime,
b: "2015-10-07 16:40:32.000" as :localdatetime {format: "yyyy-MM-dd HH:mm:ss.SSS"}
}
{
"a": "2015-07-07T16:40:32Z",
"b": "2015-10-07 16:40:32.000"
}
Through this operator you can also take a value that’s already structured as a date, and transform it into a differently formatted date, for example:
%output application/json
---
{
myDate: ((payload as :string) as :date {format: "yyyyMMdd"}) as :string {format: "MM-dd-yyyy"}
}
Coerce to Object
You can coerce your input into a custom object type of whatever class you want.
%dw 1.0
%output application/json
---
{
payload as :object {class : "soa.sfabs.SOAResponseInfoType\$ServiceInfo"}
}
Keep in mind that if the class name contains any '$' characters, they must be escaped with a backslash (\). |
Type Of
Returns the type of a provided element (eg: '":string"' , '":number"' )
%dw 1.0
%output application/json
---
isString: typeOf payload.mystring
Input: JSON
{
"mystring":"a string"
}
Output: JSON
{
"isString": ":string"
}
Flatten
If you have an array of arrays, this function can flatten it into a single simple array.
%dw 1.0
%output application/json
---
flatten payload
Input: JSON
[
[3,5],
[9,5],
[154,0.3]
]
Output: JSON
[
3,
5,
9,
5,
154,
0.3
]
Size Of
Returns the number of elements in an array (or anything that can be converted to an array such as a string).
%dw 1.0
%output application/json
---
{
arraySize: sizeOf [1,2,3],
textSize: sizeOf "MuleSoft",
objectSize: sizeOf {a:1,b:2}
}
{
"arraySize": 3,
"textSize": 8,
"objectSize": 2
}
Array Push
Pushes a new element to the end of an array.
%dw 1.0
%output application/json
---
aa: [0, 1, 2] + 5
{
"aa": [0, 1, 2, 5]
}
Remove from Array
Removes an element from an array when it matches the specified value. If multiple elements in the array match the value, they will all be removed.
%dw 1.0
%output application/json
---
{
a: [0, 1, 1, 2] - 1,
b: [{a: "a"}] - {a: "a"}
}
{
"a": [0,2],
"b": []
}
Remove Matching from Array
Removes a set of elements from an array when an element in the base array matches one of the values in the substracted array. If multiple elements in the array match a value, they will all be removed.
%dw 1.0
%output application/json
---
a: [0, 1, 1, 2] -- [1,2]
{
"a": [0],
}
Average of Array
Creates an average of all the values in an array and outputs a single number. The array must of course contain only numerical value in it.
%dw 1.0
%output application/json
---
{
a: avg [1..1000],
b: avg [1, 2, 3]
}
{
"a": 500.5,
"b": 2.0
}
Reduce
Apply a reduction to the array using just two parameters: the accumulator ($$), and the value ($). By default, the accumulator starts at the first value of the array.
%dw 1.0
%output application/json
---
sum: [0, 1, 2, 3, 4, 5] reduce($$ + $)
{
"sum": 15
}
%dw 1.0
%output application/json
---
concat: ["a", "b", "c", "d"] reduce($$ ++ $)
{
"concat": "abcd"
}
In some cases, you may not want to use the first element of the array as an accumulator. To set the accumulator to something else, you must define this in a lambda.
%dw 1.0
%output application/json
---
concat: ["a", "b", "c", "d"] reduce ((val, acc = "z") -> acc ++ val)
{
"concat": "zabcd"
}
Join By
Merges an array into a single string value, using the provided string as a separator between elements.
%dw 1.0
%output application/json
---
aa: ["a","b","c"] joinBy "-"
{
"aa": "a-b-c"
}
Split By
Performs the opposite operation as Join By. It splits a string into an array of separate elements, looking for instances of the provided string and using it as a separator.
%dw 1.0
%output application/json
---
split: "a-b-c" splitBy "-"
{
"split": ["a","b","c"]
}
Order By
Returns the provided array (or object) ordered according to the value returned by the lambda. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
---
orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy $.letter
{
"orderByLetter": [
{
"letter": "a"
},
{
"letter": "b"
},
{
"letter": "c"
},
{
"letter": "d"
},
{
"letter": "e"
}
]
}
The orderBy function doesn’t have an option to order in descending order instead of ascending. What you can do in these cases is simply invert the order of the resulting array. Transform
Output
|
Group By
Partitions an array into a Object that contains Arrays, according to the discriminator lambda you define. The lambda is invoked with two parameters: index and the value. If these parameters are not named, the index is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
---
"language": payload.langs groupBy $.language
Input: JSON
{
"langs": [
{
"name": "Foo",
"language": "Java"
},
{
"name": "Bar",
"language": "Scala"
},
{
"name": "FooBar",
"language": "Java"
}
]
}
Output: JSON
{
"language": {
"Scala": [
{"name":"Bar", "language":"Scala"}
],
"Java": [
{"name":"Foo", "language":"Java"},
{"name":"FooBar", "language":"Java"}
]
}
}
Distinct By
Returns only unique values from an array that may have duplicates. The lambda is invoked with two parameters: index and value. If these parameters are not defined, the index is defined by default as $$ and the value as $.
%dw 1.0
%output application/json
---
{
book : {
title : payload.title,
year: payload.year,
authors: payload.author distinctBy $
}
}
Input: JSON
{
"title": "XQuery Kick Start",
"author": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Kurt Cagle",
"Kurt Cagle",
"Kurt Cagle",
"Vaidyanathan Nagarajan"
],
"year":"2000"
}
Output: JSON
{
"book": {
"title": "XQuery Kick Start",
"year": "2000",
"authors": [
"James McGovern",
"Per Bothner",
"Kurt Cagle",
"James Linn",
"Vaidyanathan Nagarajan"
]
}
}
Zip Arrays
Given two or more separate lists, the zip function can be used to merge them together into a single list of consecutive n-tuples. Imagine two input lists each being one side of a zipper: similar to the interlocking teeth of a zipper, the zip function interdigitates each element from each input list, one element at a time.
%dw 1.0
%output application/json
---
{
a: [0, 1, 2, 3] zip ["a", "b", "c", "d"],
b: [0, 1, 2, 3] zip "a",
c: [0, 1, 2, 3] zip ["a", "b"]
}
{
"a": [
[0,"a"],
[1,"b"],
[2,"c"],
[3,"d"]
],
"b": [
[0,"a"],
[1,"a"],
[2,"a"],
[3,"a"]
],
"c": [
[0,"a"],
[1,"b"]
]
}
Note that in example b, since only one element was provided in the second array, it was matched with every element of the first array. Also note that in example c, since the second array was shorter than the first, the output was only as long as the shortest of the two.
Here is another example of the zip function with more than two input lists.
%dw 1.0
%output application/json
---
payload.list1 zip payload.list2 zip payload.list3
Input: JSON
{
"list1": ["a", "b", "c", "d"],
"list2": [1, 2, 3],
"list3": ["aa", "bb", "cc", "dd"],
"list4": [["a", "b", "c"], [1, 2, 3, 4], ["aa", "bb", "cc", "dd"]]
}
Output: JSON
[
[
"a",
1,
"aa"
],
[
"b",
2,
"bb"
],
[
"c",
3,
"cc"
]
]
Unzip Array
Performs the opposite function of Zip Arrays, that is: given a single array where each index contains an array with two elements, it outputs two separate arrays, each with one of the elements of the pair. This can also be scaled up, if the indexes in the provided array contain arrays with more than two elements, the output will contain as many arrays as there are elements for each index.
%dw 1.0
%output application/json
---
{
a: unzip [[0,"a"],[1,"b"],[2,"c"],[3,"d"]],
b: unzip [ [0,"a"], [1,"a"], [2,"a"], [3,"a"]],
c: unzip [ [0,"a"], [1,"a","foo"], [2], [3,"a"]]
}
{
"a":[
[0, 1, 2, 3],
["a", "b", "c", "d"]
],
"b": [
[0,1,2,3],
["a","a","a","a"]
],
"c": [
[0,1,2,3]
]
}
Note even though example b can be considered the inverse function to the example b in Zip Arrays, the result is not analogous, since it returns an array of repeated elements instead of a single element. Also note that in example c, since the number of elements in each component of the original array is not consistent, the output only creates as many full arrays as it can, in this case just one.
Replace
Replaces a section of a string for another, in accordance to a regular expression, and returns a modified string.
%dw 1.0
%output application/json
---
b: "admin123" replace /(\d+)/ with "ID"
{
"b": "adminID"
}
Matches
Matches a string against a regular expression, and returns true or false.
%dw 1.0
%output application/json
---
b: "admin123" matches /(\d+)/
{
"b": false
}
For more advanced use cases where you need to output or conditionally process the matched value, see Pattern Matching. |
Starts With
Returns true or false depending on if a string starts with a provided substring.
%dw 1.0
%output application/json
---
{
a: "Mariano" startsWith "Mar",
b: "Mariano" startsWith "Em"
}
{
"a": true,
"b": false
}
Ends With
Returns true or false depending on if a string ends with a provided substring.
%dw 1.0
%output application/json
---
{
a: "Mariano" endsWith "no",
b: "Mariano" endsWith "to"
}
{
"a": true,
"b": false
}
Find
Given a string, it returns the index position within the string at which a match was matched. If found in multiple parts of the string, it returns an array with the various idex positions at which it was found. You can either look for a simple string or a regular expression.
%dw 1.0
%output application/json
---
{
a: "aabccde" find /(a).(b)(c.)d/,
b: "aabccdbce" find "a",
c: "aabccdbce" find "bc"
}
{
"a": [[0,0,2,3]],
"b": [0,1],
"c": [2,6]
}
Match
Match a string against a regular expression. Match returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex.
It can be applied to the result of any evaluated expression, and can return any evaluated expression. See the Match operator in the DataWeave Language Introduction.
%dw 1.0
%output application/json
---
hello: "anniepoint@mulesoft.com" match /([a-z]*)@([a-z]*).com/
{
"hello": [
"anniepoint@mulesoft.com",
"anniepoint",
"mulesoft"
]
}
In the example above, we see that the search regular expression describes an email address. It contains two capture groups, what’s before and what’s after the @. The result is an array of three elements: the first is the whole email address, the second matches one of the capture groups, the third matches the other one.
Scan
Returns an array with all of the matches in the given string. Each match is returned as an array that contains the complete match, as well as any capture groups there may be in your regular expression.
%dw 1.0
%output application/json
---
hello: "anniepoint@mulesoft.com,max@mulesoft.com" scan /([a-z]*)@([a-z]*).com/
{
"hello": [
[
"anniepoint@mulesoft.com",
"anniepoint",
"mulesoft"
],
[
"max@mulesoft.com",
"max",
"mulesoft"
]
]
}
In the example above, we see that the search regular expression describes an email address. It contains two capture groups, what’s before and what’s after the @. The result is an array with two matches, as there are two email addresses in the input string. Each of these matches is an array of three elements, the first is the whole email address, the second matches one of the capture groups, the third matches the other one.
Similar
Evaluates if two values are similar, regardless of their type. For example, the string "1234" and the number 1234 aren’t equal, but they are recognized as similar.
%dw 1.0
%output application/json
---
{
a: "1234" == 1234,
b: "1234" ~= 1234,
c: "true" == true,
d: "true" ~= true
}
{
"a": false,
"b": true,
"c": false,
"d": true
}
Upper
Returns the provided string in uppercase characters.
%dw 1.0
%output application/json
---
{
name: upper "mulesoft"
}
{
"name": MULESOFT
}
Lower
Returns the provided string in lowercase characters.
%dw 1.0
%output application/json
---
{
name: lower "MULESOFT"
}
{
"name": mulesoft
}
Camelize
Returns the provided string in camel case. All underscores are deleted, including any underscores at the beginning of the string.
%dw 1.0
%output application/json
---
{
a: camelize "customer",
b: camelize "customer_first_name",
c: camelize "customer name",
d: camelize "_name_starts_with_underscore"
}
{
"a": "customer",
"b": "customerFirstName",
"c": "customer name",
"d": "nameStartsWithUnderscore"
}
Capitalize
Returns the provided string with every word starting with a capital letter and no underscores. It also replaces underscores with spaces and puts a space before each capitalized word.
%dw 1.0
%output application/json
---
{
a: capitalize "customer",
b: capitalize "customer_first_name",
c: capitalize "customer NAME",
d: capitalize "customerName",
}
{
"a": "Customer",
"b": "Customer First Name",
"c": "Customer Name",
"d": "Customer Name"
}
Dasherize
Returns the provided string with every word separated by a dash.
%dw 1.0
%output application/json
---
{
a: dasherize "customer",
b: dasherize "customer_first_name",
c: dasherize "customer NAME"
}
{
"a": "customer",
"b": "customer-first-name",
"c": "customer-name"
}
Underscore
Returns the provided string with every word separated by an underscore.
%dw 1.0
%output application/json
---
{
a: underscore "customer",
b: underscore "customer-first-name",
c: underscore "customer NAME"
}
{
"a": "customer",
"b": "customer_first_name",
"c": "customer_NAME"
}
Pluralize
Returns the provided string transformed into its plural form.
%dw 1.0
%output application/json
---
{
a: pluralize "box",
b: pluralize "wife",
c: pluralize "foot"
}
{
"a": "boxes",
"b": "wives",
"c": "feet"
}
Singularize
Returns the provided string transformed into its singular form.
%dw 1.0
%output application/json
---
{
a: singularize "boxes",
b: singularize "wives",
c: singularize "feet"
}
{
"a": "box",
"b": "wife",
"c": "foot"
}
Trim
Removes any excess spaces at the start and end of a string.
%dw 1.0
%output application/json
---
{
"a": trim " my long text "
}
{
"a": "my long text"
}
Substring
Extracts a set of characters out of a string, based on the position that the first and last character of the desired substring occupy in the character array. If you use negative numbers, you can also inverse the order in which characters are set.
%dw 1.0
%output application/json
---
{
"a": "abcdefg"[0..4],
"b": "abcdefg"[-1..-4]
}
{
"a": "abcde"
"b": "gfed"
}
Ordinalize
Returns the provided numbers set as ordinals.
%dw 1.0
%output application/json
---
{
a: ordinalize 1,
b: ordinalize 8,
c: ordinalize 103
}
{
"a": "1st",
"b": "8th",
"c": "103rd"
}
Max
Returns the highest number in an array or object.
%dw 1.0
%output application/json
---
{
a: max [1..1000],
b: max [1, 2, 3],
d: max [1.5, 2.5, 3.5]
}
{
"a": 1000,
"b": 3,
"d": 3.5
}
Min
Returns the lowest number in an array or object.
%dw 1.0
%output application/json
---
{
a: min [1..1000],
b: min [1, 2, 3],
d: min [1.5, 2.5, 3.5]
}
{
"a": 1,
"b": 1,
"d": 1.5
}
Round
Rounds the value of a number to the nearest integer
%dw 1.0
%output application/json
---
{
a: round 1.2,
b: round 4.6,
c: round 3.5
}
{
"a": 1,
"b": 5,
"c": 4
}
Sqrt
Returns the square root of the provided number
%dw 1.0
%output application/json
---
{
a: sqrt 4,
b: sqrt 25,
c: sqrt 100
}
{
"a": 2.0,
"b": 5.0,
"c": 10.0
}
Pow
Returns the result of the first number a
to the power of the number following the pow
operator.
%dw 1.0
%output application/json
---
{
a: 2 pow 3,
b: 3 pow 2,
c: 7 pow 3
}
{
"a": 8,
"b": 9,
"c": 343
}
Ceil
Rounds a number upwards, returning the first full number above than the one provided.
%dw 1.0
%output application/json
---
{
a: ceil 1.5,
b: ceil 2.2,
c: ceil 3
}
{
"a": 2,
"b": 3,
"c": 3
}
Floor
Rounds a number downwards, returning the first full number below than the one provided.
%dw 1.0
%output application/json
---
{
a: floor 1.5,
b: floor 2.2,
c: floor 3
}
{
"a": 1,
"b": 2,
"c": 3
}
Abs
Returns the absolute value of a number,
%dw 1.0
%output application/json
---
{
a: abs -2,
b: abs 2.5,
c: abs -3.4,
d: abs 3
}
{
"a": 2,
"b": 2.5,
"c": 3.4,
"d": 3
}
Mod
Returns the remainder after division of the first number by the second one
%dw 1.0
%output application/json
---
{
a: 3 mod 2,
b: 4 mod 2,
c: 2.2 mod 2
}
{
"a": 1,
"b": 0,
"c": 0.2
}
Now
Returns a datetime object with the current date and time.
%dw 1.0
%output application/json
---
{
current_time: now
}
{
"current_time": "2016-10-20T17:15:06.196Z"
}
Date Time Operations
There are several operators that deal with date related types, which include date, time, localtime, datetime, localdatetime, period, timezone.
Get Time Unit
You can extract a particular time unit from any date related type as shown below:
%dw 1.0
%output application/json
---
{
a: |2003-10-01|.day,
b: |2003-10-01|.month,
c: |2003-10-01|.year,
d: |2003-10-01T23:57:59Z|.hour,
e: |2003-10-01T23:57:59Z|.minutes,
f: |2003-10-01T23:57:59Z|.seconds,
g: |2003-10-01T23:57:59-03:00|.offsetSeconds,
h: |23:57:59Z|.hour,
i: |23:57:59.700|.nanoseconds,
j: |23:57:59.700|.milliseconds,
k: |2003-10-01T23:57:59Z|.dayOfWeek,
l: |2003-10-01T23:57:59Z|.dayOfYear,
m: |P3Y2M10D|.years
}
{
"a": 1,
"b": 10,
"c": 2003,
"d": 23,
"e": 57,
"f": 59,
"g": -10800,
"h": 23,
"i": 700000000,
"j": 700,
"k": 3,
"l": 274,
"m": 3
}
Shift Time Zone
Shift a date time to the specified timezone.
%dw 1.0
%output application/json
---
a: |2014-01-01T14:00-03:00| >> |-08:00|
{
"a": "2014-01-01T09:00-08:00"
}
Append Time
You can append a date to a time (or localtime) object so as to provide a more precise value.
%dw 1.0
%output application/json
---
{
a: |2003-10-01| ++ |23:57:59|,
b: |2003-10-01| ++ |23:57:59Z|
}
{
"a": "2003-10-01T23:57:59",
"b": "2003-10-01T23:57:59Z"
}
Note that the order in which the two objects are appended is irrelevant, so logically a ':date' + ':time' will result in the same as a '#:time' + ':date'.
Append Time Zone
Appends a time zone to a date type value.
%dw 1.0
%output application/json
---
a: |2003-10-01T23:57:59| ++ |-03:00|
{
"a": "2003-10-01T23:57:59-03:00"
}
Adding a Period of Time
Add or subtract a period of time from a given date or time type object.
%dw 1.0
%output application/json
---
a: |2003-10-01T23:57:59Z| + |P1Y|
{
"a": "2004-10-01T23:57:59Z"
}
Subtracting a Period of Time
The same logically applies to subtracting time periods from a date or time type object.
%dw 1.0
%output application/json
---
{
a: |2003-10-01| - |P1Y|,
b: |2003-10-01T23:57:59Z| - |P1Y|
}
{
"a": "2002-10-01",
"b": "2002-10-01T23:57:59Z"
}
Note that when a subtraction operation includes a time object and a period, the order in which both elements are placed is indiferent since it would be impossible to subtract a date from a period, so |2003-10-01| - |P1Y|
returns the same as |P1Y| - |2003-10-01|
.
Subtracting two Dates
When subtracting one date or time type object from another, what we logically get is the difference between these times expressed as a time period.
%dw 1.0
%output application/json
---
{
a: |23:59:56-03:00| - |22:59:56-00:00|,
b: |2003-10-01| - |2002-09-23|
}
{
"a": "PT-4H",
"b": "P-1Y-8D"
}
Date Coercion
You can change the format of a date to fit another standard, see Coerce to date.
Next Steps
-
For a high level overview about the language, see DataWeave Language Introduction
-
For a listing and details about all of the types you can use, see DataWeave Types
-
For details on how to create and use your own functions, see Functions and Lambdas
-
View complete example projects that use DataWeave in the Anypoint Exchange