MuleSoft Mapping JSON to XML
JSON and XML are the two most common data formats in enterprise integrations. REST APIs use JSON. Legacy systems and SOAP web services use XML. MuleSoft developers frequently convert between the two. DataWeave makes this conversion clean and precise.
JSON vs XML: A Side-by-Side View
JSON uses curly braces and square brackets. XML uses opening and closing tags. The same data looks very different in each format.
Same Data in JSON and XML
JSON:
{
"customer": {
"id": "CUST-001",
"name": "Alice Smith",
"orders": [
{ "id": "ORD-1", "total": 150.00 },
{ "id": "ORD-2", "total": 89.50 }
]
}
}
XML:
<Customer>
<Id>CUST-001</Id>
<Name>Alice Smith</Name>
<Orders>
<Order><Id>ORD-1</Id><Total>150.00</Total></Order>
<Order><Id>ORD-2</Id><Total>89.50</Total></Order>
</Orders>
</Customer>
Converting JSON to XML
Set the output format to application/xml and structure the DataWeave body like an XML document. Each key becomes a tag. Nested objects become child tags.
JSON to XML DataWeave Script
Input (JSON payload):
{
"firstName": "Alice",
"lastName": "Smith",
"age": 32,
"active": true
}
DataWeave:
%dw 2.0
output application/xml
---
{
Customer: {
FirstName: payload.firstName,
LastName: payload.lastName,
Age: payload.age,
Status: if (payload.active) "ACTIVE" else "INACTIVE"
}
}
Output (XML):
<?xml version='1.0' encoding='UTF-8'?>
<Customer>
<FirstName>Alice</FirstName>
<LastName>Smith</LastName>
<Age>32</Age>
<Status>ACTIVE</Status>
</Customer>
Adding XML Attributes
XML attributes appear inside the opening tag like <Order id="ORD-1">. In DataWeave, use the @ symbol to define XML attributes.
XML Attributes in DataWeave
DataWeave:
%dw 2.0
output application/xml
---
{
Order @(id: payload.orderId, currency: "USD"): {
CustomerName: payload.customerName,
Total: payload.total
}
}
Output:
<Order id="ORD-001" currency="USD">
<CustomerName>Alice</CustomerName>
<Total>250.00</Total>
</Order>
Converting an Array to Repeated XML Elements
When JSON has an array, the XML equivalent is a series of repeated child elements. Use the DataWeave map operator to convert each array item into an XML element.
JSON Array to XML Repeated Elements
Input (JSON):
{
"invoiceId": "INV-2024",
"lines": [
{ "sku": "PEN-01", "qty": 10, "price": 1.50 },
{ "sku": "BOK-05", "qty": 2, "price": 12.00 },
{ "sku": "BAG-03", "qty": 1, "price": 25.00 }
]
}
DataWeave:
%dw 2.0
output application/xml
---
{
Invoice @(id: payload.invoiceId): {
(payload.lines map (line) -> {
LineItem @(sku: line.sku): {
Quantity: line.qty,
UnitPrice: line.price,
LineTotal: line.qty * line.price
}
})
}
}
Output (XML):
<Invoice id="INV-2024">
<LineItem sku="PEN-01"><Quantity>10</Quantity><UnitPrice>1.5</UnitPrice><LineTotal>15.0</LineTotal></LineItem>
<LineItem sku="BOK-05"><Quantity>2</Quantity><UnitPrice>12.0</UnitPrice><LineTotal>24.0</LineTotal></LineItem>
<LineItem sku="BAG-03"><Quantity>1</Quantity><UnitPrice>25.0</UnitPrice><LineTotal>25.0</LineTotal></LineItem>
</Invoice>
Converting XML to JSON
Reading XML is the reverse direction. Set the input type to application/xml in the Transform Message component and set the output to application/json. DataWeave reads the XML automatically as a nested object structure.
XML to JSON DataWeave Script
Input (XML payload):
<Employee>
<EmpId>EMP-999</EmpId>
<FullName>Bob Jones</FullName>
<Department>Engineering</Department>
</Employee>
DataWeave:
%dw 2.0
output application/json
---
{
"employeeId": payload.Employee.EmpId,
"name": payload.Employee.FullName,
"dept": payload.Employee.Department
}
Output (JSON):
{
"employeeId": "EMP-999",
"name": "Bob Jones",
"dept": "Engineering"
}
Reading XML Attributes in DataWeave
To read an XML attribute inside DataWeave, use the @ accessor on the element.
XML Input:
<Product id="PROD-007" category="electronics">
<Name>Laptop</Name>
<Price>999.99</Price>
</Product>
DataWeave:
%dw 2.0
output application/json
---
{
"productId": payload.Product.@id,
"category": payload.Product.@category,
"name": payload.Product.Name,
"price": payload.Product.Price as Number
}
Output:
{
"productId": "PROD-007",
"category": "electronics",
"name": "Laptop",
"price": 999.99
}
Handling XML Namespaces
Enterprise XML documents often use namespaces — prefixes that qualify tag names to avoid conflicts between schemas. To work with namespaced XML in DataWeave, declare the namespace in the header and use it in the body.
%dw 2.0
output application/xml
ns ord http://orders.example.com/schema
---
{
ord#Order: {
ord#CustomerId: payload.customerId,
ord#Total: payload.total
}
}
Output:
<ord:Order xmlns:ord="http://orders.example.com/schema">
<ord:CustomerId>CUST-001</ord:CustomerId>
<ord:Total>250.00</ord:Total>
</ord:Order>
Tips for Clean JSON-XML Conversions
- Always check the exact XML tag names — they are case-sensitive.
<CustomerName>and<customerName>are different tags. - Use the DataWeave Preview pane to test with real XML or JSON samples before deploying.
- When converting arrays to XML, wrap the
mapoutput in parentheses so DataWeave flattens the elements correctly inside the parent tag. - Cast numeric values explicitly using
as Numberwhen reading from XML, since XML stores everything as strings by default.
