Transforming JSON to XML in DataWeave: Real-World MuleSoft Example
One of the most common requirements in MuleSoft projects is transforming JSON data into XML. This is especially useful when integrating modern REST APIs with legacy systems, SOAP services, ERP applications, and enterprise platforms that require XML.
In this practical example, we will transform a JSON employee payload into a structured XML document using DataWeave 2.0.
📥 Input JSON
{
"id": 101,
"name": "John Doe",
"department": "IT",
"skills": [
"MuleSoft",
"DataWeave",
"API"
],
"active": true
}
🎯 Expected XML Output
<employee>
<id>101</id>
<name>John Doe</name>
<department>IT</department>
<skills>
<skill>MuleSoft</skill>
<skill>DataWeave</skill>
<skill>API</skill>
</skills>
<active>true</active>
</employee>
💻 DataWeave 2.0 Solution
%dw 2.0
output application/xml
---
{
employee: {
id: payload.id,
name: payload.name,
department: payload.department,
skills: payload.skills map (item) -> {
skill: item
},
active: payload.active
}
}
🔍 How It Works
1. Define DataWeave Version
%dw 2.0
This tells MuleSoft that the transformation uses DataWeave 2.0.
2. Define XML as the Output
output application/xml
The output MIME type tells DataWeave to generate XML instead of JSON.
3. Create the Root Element
{
employee: {
}
}
The employee key becomes the root XML element.
4. Map JSON Fields
id: payload.id,
name: payload.name,
department: payload.department,
active: payload.active
Each JSON field is mapped directly into the corresponding XML element.
5. Transform the Array
skills: payload.skills map (item) -> {
skill: item
}
The map function processes every item in the skills array
and creates a skill XML element for each value.
📤 Final XML Structure
<employee>
<id>101</id>
<name>John Doe</name>
<department>IT</department>
<skills>
<skill>MuleSoft</skill>
<skill>DataWeave</skill>
<skill>API</skill>
</skills>
<active>true</active>
</employee>
🏢 Real-World MuleSoft Use Case
Imagine a MuleSoft application receives customer information from a REST API in JSON format, but a downstream legacy system requires XML.
The integration can follow this pattern:
REST API
↓
JSON Payload
↓
MuleSoft
↓
DataWeave Transformation
↓
XML Payload
↓
Legacy / SOAP / ERP System
DataWeave acts as the transformation layer between the two systems.
💡 Important DataWeave Concepts Used
- DataWeave 2.0 — transformation language used by Mule 4
- output application/xml — generates XML output
- payload — accesses the incoming Mule message payload
- map — transforms array elements
- Object construction — creates the desired XML structure
⚠️ Common Mistakes
1. Forgetting the XML output type
Make sure you use:
output application/xml
2. Incorrect array transformation
When an input contains arrays, make sure the desired XML structure is explicitly created.
3. Confusing JSON and XML structure
JSON objects and XML elements are represented differently. Always design the required XML structure before writing the transformation.
🎯 Interview Question
How would you transform a JSON payload into XML in MuleSoft using DataWeave?
Answer: Use DataWeave 2.0 with
output application/xml and construct the required XML structure
using the incoming payload. Functions such as
map can be used to transform arrays.
🚀 Final Takeaway
JSON-to-XML transformation is one of the fundamental DataWeave skills every MuleSoft developer should master.
Once you understand how objects, arrays, and DataWeave functions map to XML, you can handle much more complex enterprise transformations.
📚 Want to Practice More DataWeave?
Take your DataWeave skills from beginner to advanced with 1000 practical DataWeave problems with solutions.

0 Comments