Below are some examples of DataWeave functions along with their input and output.
1. upper
Converts a string to uppercase.
Input:
{ "text": "hello world"}DataWeave Code:
%dw 2.0output application/json---upper(payload.text)Output:
"HELLO WORLD"2. lower
Converts a string to lowercase.
Input:
{ "text": "HELLO WORLD"}DataWeave Code:
%dw 2.0output application/json---lower(payload.text)Output:
"hello world"3. sizeOf
Returns the size of an array or string.
Input:
{ "array": [1, 2, 3, 4, 5]}DataWeave Code:
%dw 2.0output application/json---sizeOf(payload.array)Output:
54. filter
Filters an array based on a condition.
Input:
{ "numbers": [1, 2, 3, 4, 5]}DataWeave Code:
%dw 2.0output application/json---payload.numbers filter ((item) -> item > 2)Output:
[3, 4, 5]5. map
Transforms each element in an array.
Input:
{ "numbers": [1, 2, 3]}DataWeave Code:
%dw 2.0output application/json---payload.numbers map ((item) -> item * 2)Output:
[2, 4, 6]6. join
Joins elements of an array into a string.
Input:
{ "words": ["Hello", "World"]}DataWeave Code:
%dw 2.0output application/json---payload.words joinBy(" ")Output:
"Hello World"7. mapObject
Transforms each key-value pair in an object.
Input:
{ "object": {"a": 1, "b": 2}}DataWeave Code:
%dw 2.0output application/json---payload.object mapObject((value, key) -> (key ++ "_new"): value * 2)Output:
{"a_new": 2, "b_new": 4}8. pluck
Extracts the values of a specific key from an array of objects.
Input:
{ "people": [{"name": "Alice"}, {"name": "Bob"}]}DataWeave Code:
%dw 2.0output application/json---payload.people pluck("name")Output:
["Alice", "Bob"]9. contains
Checks if an array contains a specific element.
Input:
{ "array": [1, 2, 3, 4, 5], "value": 3}DataWeave Code:
%dw 2.0output application/json---payload.array contains(payload.value)Output:
true10. reduce
Reduces an array to a single value using a provided accumulator function.
Input:
{ "numbers": [1, 2, 3, 4]}DataWeave Code:
%dw 2.0output application/json---payload.numbers reduce ((item, acc = 0) -> acc + item)Output:
1011. distinct
Returns distinct elements from an array.
Input:
{ "numbers": [1, 2, 2, 3, 4, 4]}DataWeave Code:
%dw 2.0output application/json---payload.numbers distinct($)Output:
[1, 2, 3, 4]12. sum
Returns the sum of an array of numbers.
Input:
{ "numbers": [1, 2, 3, 4]}DataWeave Code:
%dw 2.0output application/json---sum(payload.numbers)Output:
1013. find
Finds the first element that matches a condition.
Input:
{ "numbers": [1, 2, 3, 4, 5]}DataWeave Code:
%dw 2.0output application/json---payload.numbers find 3Output:
2These examples demonstrate how to use core functions in DataWeave to manipulate and transform data effectively. If you have a specific function or use case in mind, feel free to comment!
]]>