Skip to content

DataWeave 2.0 Functions Guide

Introduction

This guide provides multiple examples of DataWeave code functions.

Example 1: Map Function

The map function is used to transform each item in an array.

Map Function
%dw 2.0
output application/json
---
payload map ((item, index) -> {  // (1)!
  id: item.id,
  name: item.name 
})
  1. Maps over each item in the payload array.

Example 2: Filter Function

The filter function is used to filter an array using a specific condition.

Filter Function
%dw 2.0
output application/json
---
payload filter ((item, index) -> item.age > 18)  // (1)!
  1. Filters the payload array by the condition that the age is greater than 18.

Example 3: Reduce Function

The reduce function is used to transform an array into a single value.

Reduce Function
%dw 2.0
output application/json
---
payload reduce ((item, accumulator = 0) -> accumulator + item.value) // (1)!
  1. Reduces the payload array into a single value by adding the value of each item to the accumulator.