Transforming Data with DataWeave
Script anatomy
A DataWeave file starts with %dw 2.0, optionally import/include statements, then declarations (var, fun, type) separated from the output expression by ---. Use fun for reusable logic and type for custom schemas. The expression sees payload, vars, attributes, and p('property').
Selectors and operators
Select with .key, ."key with space", [index], and .*key for repeated keys. ++ concatenates strings, arrays, and objects; -- removes keys or values; default supplies a fallback when the left side is null; as coerces to a type.
Core functions, by output shape
- Same number of elements:
map(element$, index$$),mapObject(value$$, key$),filterObject. - Fewer elements:
filter. - One value:
reduce. - One per group:
groupBythenmapObjectorpluck. - Objects to arrays:
pluck,entriesOf; arrays to objects:zip,flatten,distinctBy,orderBy,sizeOf.
Traps
- In
mapObject,$$is the key and$the value - the reverse of common intuition. mapcannot iterate an object; usemapObjectorpluck.groupByreturns an object of arrays with no guaranteed ordering.- Coercion failures raise errors; guard with
defaultor pattern matching (case/if).
Worked example
payload groupBy $.customerId pluck ((orders, id) -> { id: id, count: sizeOf(orders), total: orders reduce ((o, acc=0) -> acc + o.amount) }) returns one summary object per customer, with the group array as the first lambda argument and the key second.
Rule of thumb
Choose the function from the output shape you need, not from habit: many-in/many-out is map, many-in/fewer-out is filter, many-in/one-out is reduce.