JSON to XML: Bridging Data and Enterprise APIs
In this guide
Modern APIs speak JSON, but enterprise systems, payment gateways, feeds and many configuration formats still expect XML. Developers regularly need to turn one into the other, and the conversion needs a few decisions that the formats themselves do not make for you.
Mapping objects to elements
Each JSON key becomes an XML element name and each value becomes its content. A nested object becomes nested elements. A JSON array has no direct XML equivalent, so the converter repeats the element once per item, for example several item elements inside one parent.
Valid names and a single root
XML requires exactly one root element, but a JSON document can be an object with many keys or even an array. A converter wraps the content in a root element. XML names also cannot start with a digit or contain spaces, so JSON keys such as 2024 or first name need renaming.
Escaping and types
Characters such as ampersand and less-than are escaped in XML text. JSON booleans, numbers and null all become text in XML, so the receiving system must know how to interpret them. Agree on how null is represented, for example by omitting the element or using an empty one.
Attributes
JSON has no attributes. Some converters use a convention, such as keys beginning with @, to produce XML attributes. If the target system requires attributes, follow its schema rather than relying on a default.
Validate the result
Check the output is well-formed, then validate against the target system's schema if it provides one. Do conversions of real customer data in your browser, and test with sample data first.
Worked example: a small object
The JSON {"title":"Pen","price":1.5} went through the JSON to XML converter and returned <root><title>Pen</title><price>1.5</price></root>. Each key became an element, and the whole object was wrapped in a root element because XML needs exactly one.
The root name defaults to root and can be changed in the second box. The tool does not add an XML declaration or indent the output, so run the result through the XML Formatter if you want it readable.
Key Takeaways
- Arrays become repeated elements.
- XML needs one root and valid names.
- Decide how null and types are represented.
- Validate against the receiver's schema.
Frequently Asked Questions
Why does my converted XML have a root element I did not write?
XML requires a single root, so the converter adds one.
How are arrays represented?
As repeated elements with the same name.
Can I choose attribute names?
Only if the converter supports a convention for it. Otherwise adjust the XML manually.
Free tools mentioned in this guide
- JSON to XML Converter
- XML to JSON Converter
- XML Formatter
- JSON Formatter
- JSON Validator & Linter
- XML Minifier
Browse every Converter Tools utility, or see all tools.