Intermediate 15 min read

DataWeave Basics

By the end of this lesson, you'll be able to:

  • Write a simple DataWeave transformation script
  • Explain DataWeave's role in a Mule flow

Prerequisites: The Salesforce Connector

What DataWeave does

Two systems rarely agree on data shape — one calls it first_name, another firstName; one wants JSON, another XML. DataWeave sits between processors in a flow, transforming the in-flight message's structure, field names, and format without writing general-purpose code for it.

Basic DataWeave syntax

A script starts with %dw 2.0, declares an output format, then an expression (often an object literal) building the new shape — payload refers to the incoming data, and field access uses dot notation just like many other languages.

Reshaping a payload with DataWeave

%dw 2.0
output application/json
---
{
    fullName: payload.firstName ++ " " ++ payload.lastName,
    email: payload.emailAddress,
    isActive: payload.status == "Active"
}

This takes an incoming payload with firstName/lastName/emailAddress/status fields and reshapes it into a differently-named, differently-structured JSON object — a common shape when two systems disagree on field naming.

Exercise

Write a DataWeave script that takes payload.accountName and payload.revenue, and outputs JSON with keys 'name' and 'annualRevenue'.

Show hint

%dw 2.0, output application/json, then an object literal mapping the renamed keys.

JAVASCRIPT

DataWeave Basics — Quick Check

1. What problem does DataWeave primarily solve in a Mule flow?

2. A DataWeave script's output format (e.g. application/json) must be explicitly declared.

3. What does 'payload' refer to inside a DataWeave script?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

DataWeave is MuleSoft's functional data transformation language — it reshapes data from one format or structure into another (JSON to XML, one field naming convention to another) between the systems a Mule flow connects.