Reading Time: 8 minutes

In this blog post, I will show you how to generate XML output from a JSON data source while avoiding some of the most common pitfalls and explain how to use encoding, namespaces, fields, and attributes.

Sample JSON data

Throughout this article, I will use the following JSON data in the DataWeave transformation examples.

[
{"lastName":"Simpson",
"firstName":"Homer",
"age":39
},
{"lastName":"Simpson",
"firstName":"Lisa",
"age":8
},
{"lastName":"Simpson",
"firstName":"Bart",
"age":10
},
{"lastName":"Simpson",
"firstName":"Marge",
"age":36
},
{"lastName":"Simpson",
"firstName":"Maggie",
"age":1
}
]

XML as an output

latest report
Learn why we are the Leaders in API management and iPaaS

Let’s see how to convert a JSON document into an XML output. 

Given that the JSON document shown above is the input data to the DataWeave expression below, the XML output will be as shown.

DataWeave Expression:

%dw 2.0
output application/xml

---
family:
lastname: "simpsons"

Output Preview:



  simpsons

Note that the DataWeave expression specifies a root element family for the transformation to XML, I’ll explain more about why this is necessary later.

Encoding type

The output MIME type declaration can be followed by encoding to specify the encoding type for the XML output.

DataWeave Expression:

%dw 2.0
output application/xml encoding="ISO-8859-1"

---
family:
lastname: "simpsons"

Output Preview:



  simpsons

By default, coding is set to UTF-8, however, any of the Java SE 8 supported coding types can be selected. You can find a list of valid encodings on Oracle Documentation site.

XML namespaces

DataWeave supports XML namespaces.

An XML namespace is a W3C recommended mechanism that avoids name conflicts by differentiating XML elements or attributes that may have identical names, but different definitions.

To enable an XML namespace:

  • Declare each namespace with the keyword ns followed by the DataWeave name of the namespace and the URL of the namespace.
  • Use the DataWeave name of the namespace before the element with # as the separator.

In the example below, the namespace simpsons is defined in the header of the DataWeave script and used in the transformation by prefixing the XML element with simpsons#.

DataWeave Expression:

%dw 2.0
output application/xml encoding="ISO-8859-1"

ns simpsons http://training.mulesoft.com/simpsons
---
simpsons#family:
simpsons#lastname: "simpsons"

Output Preview:



  simpsons

In the XML output, you can clearly see the simpsons namespace prefixing all elements.

Fields

  1. When transforming JSON to XML you may run into two difficulties: XML only allows one root element while JSON allows many. 
  2. There is no array concept in XML (only repeated elements), while the JSON data structure is based on arrays and maps.

To overcome these issues DataWeave provides the following solutions:

  1. To avoid multiple root issues, you must create a root element for the XML output.
  2. To transform a JSON array into XML repeated elements, use object constructor curly braces and evaluation parenthesis i.e. “{( )}“.

In the example below, the JSON data structure has multiple repeated roots and is structured as an array. The transformation to XML would fail if the above mentioned considerations were not taken into account. Thus the transformation script declares a root element as examples and wraps the JSON data in  “{( )}“.

DataWeave Expression:

%dw 2.0
output application/xml encoding="ISO-8859-1"

var example=[{"item":1},{"item":2},{"item":3},{"item":4}]
---
examples: {(example)}

Output Preview:



  1
  2
  3
  4

Let’s build on this example and transform the more complex JSON object provided at the beginning of this post. Its structure includes a collection of elements within an array.

To transform this input JSON as a collection of elements we have to iterate over the items using the map function.

DataWeave Expression:

%dw 2.0
output application/xml encoding="ISO-8859-1"

ns simpsons http://training.mulesoft.com/simpsons
---
simpsons#family: {(payload map 
simpsons#member:
{
simpsons#"last-name":  $.lastName,
simpsons#"first-name": $.firstName,
simpsons#"age":$.age
}
)}

Output Preview:



  
    Simpson
    Homer
    39
  
  
    Simpson
    Lisa
    8
  
  
    Simpson
    Bart
    10
  
  
    Simpson
    Marge
    36
  
  
    Simpson
    Maggie
    1
  

XML attributes

The XML specification allows attributes to be defined for elements but the JSON specification does not have a direct equivalent. DataWeave provides a mechanism that allows the insertion of attributes into an XML element using the “@” symbol followed by key-value pairs.

In the DataWeave script below, a function is called that determines if a Simpson is of majority and can therefore vote. The function is called for each element in the JSON array and added as an attribute to the member element.

DataWeave Expression:

%dw 2.0
output application/xml encoding="ISO-8859-1"

ns simpsons http://training.mulesoft.com/simpsons

fun canVote(age:Number) =
(age >= 18)

---
simpsons#family: {(payload map 
simpsons#member @("can_vote": canVote($.age)):
{
simpsons#"last-name":  $.lastName,
simpsons#"first-name": $.firstName,
simpsons#"age":$.age
}
)}

Output Preview:



  
    Simpson
    Homer
    39
  

Conclusion

You now have the techniques necessary to successfully and perfectly transform JSON to XML using the DataWeave scripting language in your Mule applications.

If you want to learn more about DataWeave 2.0, you can register for the DataWeave course. You may be interested in my other DataWeave blog: DataWeave lambdas for Java programmers.