🚀 DIGITAL TECH eBOOKS

Learn. Practice. Build.

Practical eBooks, interview questions, real-world projects and free learning resources for developers.

✓ Practical Content    ✓ Interview Focused    ✓ Real-World Examples

DataWeave Functions You'll Use Every Day

 


DataWeave Functions You'll Use Every Day

DataWeave functions are built-in tools that handle everyday tasks — modifying strings, numbers, collections, objects, and dates — without you having to write that logic from scratch. This post is a practical tour of the ones you'll genuinely use in almost every real project, organized by category, with realistic examples for each.





String Functions

%dw 2.0
output application/json
---
{
  upper: upper("hello"),
  lower: lower("WORLD"),
  trimmed: trim("   MuleSoft   "),
  length: sizeOf("hello"),
  contains: contains("hello world", "world"),
  replaced: replace("2026-01-15" with "2026/01/15") // format-style replace
}
  • trim — removes whitespace from both ends of a string, which comes up constantly when cleaning messy input data from spreadsheets or forms.
  • upper / lower — case normalization, useful before comparing strings so "Active" and "active" aren't treated as different values
  • contains — quick substring check, often used inside filter or if conditions

Array Functions (the ones you'll use constantly)

map — transform every element

%dw 2.0
output application/json
---
payload.orders map (order) -> {
  id: order.orderId,
  total: order.price * order.quantity
}

filter — keep only what matches

%dw 2.0
output application/json
---
payload.orders filter (order) -> order.status == "completed"

reduce — collapse an array into one value

%dw 2.0
output application/json
---
payload.orders.total reduce ((total, sum = 0) -> sum + total)

Realistic use case: calculating a cart total from a list of line items.

orderBy — sort an array

%dw 2.0
output application/json
---
payload.orders orderBy (order) -> order.date

distinctBy — remove duplicates

%dw 2.0
output application/json
---
payload.customerIds distinctBy ($)

sizeOf — count elements

%dw 2.0
output application/json
---
{ totalOrders: sizeOf(payload.orders) }

Object Functions

mapObject — transform keys and values

%dw 2.0
output application/json
---
payload mapObject (value, key) -> {
  (upper(key)): value
}

Useful when a downstream system expects differently-cased or renamed keys across an entire object, without manually listing every field.

pluck — extract all values (or keys) from an object

%dw 2.0
output application/json
---
payload.customer pluck $

Date and Time Functions

%dw 2.0
output application/json
---
{
  now: now(),
  formatted: now() as String {format: "yyyy-MM-dd"},
  daysDiff: (payload.endDate as Date) - (payload.startDate as Date)
}

Date handling is a frequent pain point for beginners because source systems format dates inconsistently — this is where as Date {format: "..."} and as String {format: "..."} become essential tools for normalizing dates coming from different systems into one consistent format.

Number Functions

%dw 2.0
output application/json
---
{
  rounded: round(19.987, 2),
  absolute: abs(-42),
  isNumber: isEmpty(payload.price)
}

Custom Functions — When Built-Ins Aren't Enough

<cite index="27-1">You're not limited to built-in functions — you can define your own reusable functions with fun.</cite> This is worth doing whenever you find yourself repeating the same transformation logic in multiple places:

%dw 2.0
output application/json
fun formatPhone(str: String) =
  "(" ++ str[0 to 2] ++ ") " ++ str[3 to 5] ++ "-" ++ str[6 to 9]
---
{
  phone: formatPhone(payload.rawPhoneNumber)
}

<cite index="27-1">This example defines a function that takes a string argument with a type constraint, ensuring the function only accepts valid input, then formats it into a standard phone number pattern.</cite>

A Realistic Combined Example

Pulling several of these together — a script that cleans, filters, and summarizes a list of orders:

%dw 2.0
output application/json
---
{
  activeOrders: payload.orders
    filter ($.status == "active")
    map (order) -> {
      id: order.orderId,
      customer: trim(order.customerName) default "Unknown",
      total: round(order.price * order.quantity, 2)
    }
    orderBy $.total,
  orderCount: sizeOf(payload.orders filter ($.status == "active"))
}

Notice how these functions chain together naturally — filter, then map, then sort — which is a very common pattern once you're comfortable with the basics.

Where to Find the Full Function Reference

This post covers the functions you'll reach for constantly, but DataWeave's standard library is much larger — covering things like array flattening, regex matching, encoding/decoding, and more specialized date math. When you hit a transformation need not covered here, MuleSoft's official DataWeave documentation has the complete function reference, organized by category (Core, Arrays, Objects, Strings, etc.) — worth bookmarking alongside this cheat sheet.

What's Next

We've been writing "correct" DataWeave this whole series — but beginners reliably run into the same handful of mistakes when writing it for real. Next up, we cover them directly so you can recognize and avoid them.

Next up in this series: 10 Common DataWeave Mistakes Beginners Make


visit ebook site - https://techebooks.myinstamojo.com/

Post a Comment

0 Comments