title: Transforming JSON to XML in DataWeave (Real Example) labels: MuleSoft, DataWeave
Transforming JSON to XML in DataWeave (Real Example)
One of the most common real-world integration tasks: your application receives JSON (from a modern web API, say) but needs to send XML to a legacy system on the other end — or vice versa. This is exactly the kind of format-translation problem DataWeave was built to solve elegantly, without you needing to hand-write parsing logic. This post walks through a complete, realistic example.
The Scenario
Imagine you're integrating a modern e-commerce platform (which sends order data as JSON) with a legacy order-processing system that only accepts XML. Here's a sample incoming JSON payload:
{
"orderId": "ORD-1001",
"customer": {
"name": "Jane Smith",
"email": "jane@example.com"
},
"items": [
{ "sku": "WIDGET-1", "quantity": 2, "price": 19.99 },
{ "sku": "GADGET-5", "quantity": 1, "price": 49.99 }
]
}
We need to transform this into XML for the legacy system.
The Simplest Case: Just Change the Output Directive
The most surprising thing for beginners: DataWeave can often do format conversion with almost no logic at all, purely by changing the output directive. If your target structure maps cleanly, this is genuinely close to enough:
%dw 2.0
output application/xml
---
payload
This alone will produce valid XML from the JSON payload. But real-world XML integrations almost always need a bit more shaping — legacy systems tend to expect specific element names, structures, or attributes that don't map 1:1 from your JSON. Let's build a properly shaped version.
A Properly Shaped Transformation
%dw 2.0
output application/xml
---
{
Order @(id: payload.orderId): {
Customer: {
Name: payload.customer.name,
Email: payload.customer.email
},
Items: {
Item: payload.items map (item) -> {
SKU: item.sku,
Quantity: item.quantity,
Price: item.price
}
}
}
}
This produces:
<?xml version='1.0' encoding='UTF-8'?>
<Order id="ORD-1001">
<Customer>
<Name>Jane Smith</Name>
<Email>jane@example.com</Email>
</Customer>
<Items>
<Item>
<SKU>WIDGET-1</SKU>
<Quantity>2</Quantity>
<Price>19.99</Price>
</Item>
<Item>
<SKU>GADGET-5</SKU>
<Quantity>1</Quantity>
<Price>49.99</Price>
</Item>
</Items>
</Order>
Breaking Down What Just Happened
Order @(id: payload.orderId)— the@()syntax sets an XML attribute on theOrderelement. This is XML-specific — JSON doesn't have attributes, only key-value pairs, so this is one of the genuine structural differences DataWeave has to bridge for you- Renamed fields — notice
orderIdbecame theidattribute, andcustomer.namebecameCustomer > Name. DataWeave doesn't require your output field names to match your input field names at all — you have full control over the shape items map (item) -> {...}— the samemapfunction from the cheat sheet, reshaping each array item into the nestedItemelement the legacy system expects
Going the Other Direction: XML to JSON
The reverse is just as common — a legacy system sends XML, and you need JSON for a modern downstream API. Given this XML:
<Order id="ORD-1001">
<Customer>
<Name>Jane Smith</Name>
</Customer>
</Order>
The transformation:
%dw 2.0
output application/json
---
{
orderId: payload.Order.@id,
customerName: payload.Order.Customer.Name
}
Notice the @id selector — this is how you read an XML attribute back out (the mirror image of the @() syntax used to write one).
A Common Gotcha: Single Item vs Array in XML
XML doesn't have a native concept of "array" the way JSON does — if your source XML has exactly one <Item> element, DataWeave may read it as a single object rather than a one-item array, which can break a map call downstream that expects an array. A defensive pattern:
%dw 2.0
output application/json
---
{
items: (payload.Order.Items.Item default []) filter true
}
Wrapping with a technique like this normalizes both the single-item and multi-item cases into a consistent array, so downstream logic doesn't need special-case handling.
Why This Matters Beyond This One Example
The JSON ↔ XML pattern shown here — reshape field names, handle attributes, normalize arrays — is the same general approach you'll use for basically any format conversion in DataWeave: JSON to CSV, XML to Java objects, CSV to JSON, and so on. Once you're comfortable reshaping between JSON and XML specifically, the same mental model transfers directly.
What's Next
We've used a handful of DataWeave functions so far (map, filter, upper, trim, default) somewhat in passing. Next, we go deeper on the functions you'll reach for constantly, with more real examples.
Next up in this series: DataWeave Functions You'll Use Every Day
visit ebook site - https://techebooks.myinstamojo.com/
0 Comments