🚀 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 Syntax Cheat Sheet

 


title: DataWeave Syntax Cheat Sheet labels: MuleSoft, DataWeave

DataWeave Syntax Cheat Sheet

A reference you can bookmark and come back to. This post assumes you've read DataWeave 2.0 for Absolute Beginners — everything here builds on that foundation, just organized for fast lookup while you're actually writing scripts.




Script Header

%dw 2.0
output application/json
---

Common output types: application/json, application/xml, application/csv, application/java, text/plain.

Variables

%dw 2.0
output application/json
var name = "Alice"
var age = 30
---
{ name: name, age: age }

Remember: DataWeave variables are immutable — once assigned, a variable's value can't be reassigned; if you need a different value, declare a new variable instead.

Functions

Define reusable logic with fun:

%dw 2.0
output application/json
fun toUpper(aString) = upper(aString)
---
toUpper("hello")

<cite index="27-1">Functions can also perform type-based pattern matching using the built-in match operation:</cite>

%dw 2.0
output application/json
fun toUpper(aString) =
  aString match {
    case is String -> upper(aString)
    else -> null
  }
---
toUpper("hello")

Selectors

Selector Purpose Example
. Get a single value by key payload.name
[index] Get array item by position payload.items[0]
[start to end] Get a range/slice payload.items[0 to 2]
.. Descendants selector — search at any depth payload..name
? Filter selector — matching key-value pairs payload.entries[?($.active == true)]

String Operations

%dw 2.0
output application/json
---
{
  concat: ("hello" ++ " " ++ "world"),
  upper: upper("hello"),
  lower: lower("HELLO"),
  trimmed: trim("  spaced  "),
  length: sizeOf("hello")
}

Conditionals

%dw 2.0
output application/json
---
{
  status: if (payload.age >= 18) "adult" else "minor"
}

For multiple conditions, use match:

%dw 2.0
output application/json
---
payload.status match {
  case "A" -> "Active"
  case "I" -> "Inactive"
  else -> "Unknown"
}

The default Keyword

Provides a fallback if a value is null or missing — this is one of the most useful patterns in production DataWeave code:

%dw 2.0
output application/json
---
{
  city: payload.address.city default "Unknown"
}

Working with Arrays

map — transform every item in an array

<cite index="23-1">Map is one of the most commonly used functions in DataWeave, used to transform every element in an array into a new format.</cite>

%dw 2.0
output application/json
---
payload.items map (item) -> {
  name: item.name,
  priceWithTax: item.price * 1.08
}

<cite index="24-1">You can also access the index of each item during the map:</cite>

%dw 2.0
output application/json
---
["Max", "the", "Mule"] map (item, index) -> index ++ " - " ++ item

filter — keep only matching items

%dw 2.0
output application/json
---
payload.items filter ($.inStock == true)

reduce — combine array items into a single value

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

Working with Objects

Combining objects with ++

%dw 2.0
output application/json
---
{ name: "Alice" } ++ { age: 30 }

Output: { "name": "Alice", "age": 30 }

Dynamic keys

%dw 2.0
output application/json
var fieldName = "status"
---
{
  (fieldName): "active"
}

Type Coercion

Convert between types using the as keyword:

%dw 2.0
output application/json
---
{
  asNumber: "42" as Number,
  asString: 42 as String,
  asDate: "2026-01-15" as Date
}

Common Beginner Mistakes to Avoid Here

  • Forgetting the --- separator between the header and the transformation body
  • Trying to reassign a variable's value (remember: immutable)
  • Confusing . (single value) with .. (descendant/deep search) — they behave very differently
  • Forgetting default when a field might be missing, causing null errors downstream

Where to Go From Here

This cheat sheet covers the patterns you'll use in the vast majority of everyday transformations. The next post applies these directly to one of the most common real-world tasks: converting between JSON and XML.

Next up in this series: Transforming JSON to XML in DataWeave (Real Example)


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

Post a Comment

0 Comments