Interview Questions & Answers DataWeave (2025)
1. What is DataWeave and why is it used in MuleSoft?
Answer:
DataWeave is a MuleSoft transformation language (DW 2.x in Mule 4) designed to map, transform, enrich, and filter data across formats like JSON, XML, CSV, and Java objects. Typical use cases include API payload conversion, file transformation, and building integration pipelines.
Q: Write a DataWeave script that maps:
{ "name": "John", "age": 30, "email": "john@example.com" }
to:<person>
<name>John</name>
<age>30</age>
<email>john@example.com</email>
</person>
A:%dw 2.0
output application/xml
---
{
person: {
name: payload.name,
age: payload.age,
email: payload.email
}
}
Q: Convert
"John
Doe"
to uppercase.A:
%dw 2.0
output application/json
---
{ name: upper(payload.name) }
Transforms "John Doe"
to {"name":"JOHN DOE"}
Q: Filter items with
value
> 15
.A:
%dw 2.0
output application/json
---
payload filter ((item) -> item.value > 15)
Returns filtered objectsQ: Given
{"firstName":"John","lastName":"Doe"}
,
combine into "fullName"
.A:
%dw 2.0
output application/json
---
{ fullName: payload.firstName ++ " " ++ payload.lastName }
Yields {"fullName":"John
Doe"}
Scenario: Transform CRM JSON
{firstName,lastName,email}
into Marketing XML {GivenName,FamilyName,EmailAddress}
.Solution:
%dw 2.0
output application/xml
---
Customer: {
GivenName: payload.firstName,
FamilyName: payload.lastName,
EmailAddress: payload.email
}
This real example enabled seamless CRM‑to‑platform integrationA. Flatten Nested JSON to CSV
Scenario: Convert complex JSON to flat CSV.
Solution Outline:
· Inspect nested payload structure.
· Use
flatten
,
map
, pluck
.· Convert to CSV via
output application/csv
Scenario: Reformat
"2023-01-01T00:00:00Z"
to "01/01/2023"
.Solution:
%dw 2.0
output application/json
---
payload as Date {format: "yyyy-MM-dd'T'HH:mm:ssz"}
as String {format: "MM/dd/yyyy"}
Result: "01/01/2023"
Use a map or file as lookup table; then
map
+ find
to enrich or merge
data. Handle missing keys carefullyGroup transactions by customer ID using
groupBy
:payload groupBy $.customerId
mapObject ((customerId, txns) -> {
customerId: customerId,
total: sum(txns.*.amount)
})
Computes total per customerStrategies include:
·
default
keyword·
when
or unless
·
flatten
to remove nullsMention error handling implications
· Use streaming mode and batch jobs
· Avoid in-memory ops
· Employ caching for repeated lookups — essential for high‑volume integrations
· Know core functions: map, filter, groupBy, flatten, joinBy, pluck, default, read/write
· Practice scenarios: JSON⇄XML/CSV, lookup, aggregation
· Master patterns: error handling, streaming, batch mode
· Use tools: DataWeave Playground for quick testing
Question |
What to Explain |
Key DW Concepts |
Transform formats |
Show correct input/output script |
input/output directive |
Handle null |
Use default or remove nulls |
default, when, flatten |
Optimize large data |
Streaming, batch, caching |
streaming mode, batch job |
Lookup enrich |
Merge payload with reference |
map, find, pluck |
Group Data |
Summarize by key |
groupBy, sum |
Comments
Post a Comment