How to Convert JSON to XML (and Back) Correctly
Convert JSON to XML for SOAP and legacy systems and back again: how keys map to elements, arrays to repeated tags, attribute and namespace limits, valid element naming, and round-trip caveats.
Modern services emit JSON, but plenty of enterprise and government systems still require XML — SOAP endpoints, legacy integrations, and XML-based config. Bridging the two is a routine integration task. This guide explains how JSON converts to XML and back, the structural mismatches that make it a mapping rather than a perfect round trip, and how to do it without producing invalid XML.
Why convert JSON to XML
You convert JSON to XML when a consumer only speaks XML: a SOAP web service, a partner API built on XML standards, or a legacy system whose parser predates JSON. The reverse — XML to JSON — is just as common when you receive RSS feeds, SOAP responses, or old config and want to work with it as modern JSON. The two conversions together let JSON and XML systems interoperate.
How keys become elements
The core mapping is straightforward: each JSON key becomes an XML element, and its value becomes the element's text or nested content. A nested JSON object produces nested elements, preserving the hierarchy. So {"user": {"name": "Ada"}} becomes a user element containing a name element with the text Ada.
{ "user": { "name": "Ada", "id": 1 } }<user>
<name>Ada</name>
<id>1</id>
</user>How arrays become repeated tags
XML has no array type, so a JSON array is rendered as repeated elements sharing one tag name — the idiomatic XML way to express a list. A JSON items array of three objects becomes three item elements in sequence. When converting back from XML, a good parser recognizes repeated sibling tags and collapses them into a JSON array so the round trip preserves the list.
{ "items": ["a", "b", "c"] }<items>a</items>
<items>b</items>
<items>c</items>The structural mismatches
- Attributes — XML elements can have attributes, which have no direct JSON equivalent; conversions either drop them or use a convention like an @ prefix.
- Namespaces — XML namespaces and prefixes do not exist in JSON and are typically flattened or lost.
- Order — XML preserves element order strictly; JSON object keys are unordered, so meaning carried by order can be lost.
- Mixed content — text interleaved with child elements is natural in XML but awkward to represent in JSON.
Valid XML element names
XML naming is stricter than JSON keys. An element name cannot contain spaces and cannot begin with a digit, while JSON keys can be almost anything. If your JSON has keys like "first name" or "123", a faithful conversion would produce invalid XML, so rename such keys in the JSON first. This is the most common reason a generated XML document fails to parse.
Converting reliably
Convert with a tool that parses and re-serializes rather than doing text substitution, so structure and escaping are handled correctly. The JSON to XML tool produces well-formed XML for SOAP and legacy consumers, and the XML to JSON tool turns feeds and responses into usable JSON. Validate the JSON side with a validator before converting, since malformed JSON cannot produce valid XML.
Frequently asked questions
How do I convert JSON to XML?
Map each JSON key to an XML element and each value to that element's content; arrays become repeated elements with the same tag. A JSON-to-XML converter handles the structure and escaping and produces well-formed XML for systems that require it.
Is JSON to XML conversion lossless?
No. XML has attributes, namespaces, strict element ordering, and mixed content that JSON cannot represent directly, so the conversion is a structural mapping. Plain data round-trips well, but XML-specific features can be lost.
How are JSON arrays represented in XML?
As repeated elements sharing the same tag name, since XML has no array type. A three-item JSON array becomes three sibling elements, and a good XML-to-JSON parser collapses repeated tags back into an array.
Why is my converted XML invalid?
Usually because a JSON key is not a valid XML element name — XML names cannot contain spaces or start with a digit. Rename keys like "first name" or "123" in the JSON before converting.
How do I convert XML back to JSON?
Use an XML-to-JSON converter that maps elements to object properties and repeated tags to arrays. Remember attributes and namespaces have no direct JSON equivalent, so review attribute-heavy XML after conversion.
Related tools
Try these tools
JSON to XML Converter
Transform JSON objects into XML documents with a custom root element.
XML to JSON Converter
Parse XML documents into clean, structured JSON.
JSON Validator
Validate JSON instantly with precise, human-readable error messages and line numbers.
JSON Formatter & Beautifier
Format, prettify, and beautify messy JSON with configurable indentation and key sorting.