JSON and XML describe the same kind of structured data, but the systems that consume them rarely accept both. An older integration, a SOAP service or a feed specification asks for XML, while your application, API or config file holds JSON. Converting by hand is slow and error-prone, because the two formats do not map onto each other in only one obvious way.
The good news is that a converter does not have to guess. The free JSON to XML Converter on Ovatly uses a documented, deterministic mapping, so you can predict exactly what your JSON will become before you run it. This guide explains the mapping rules, walks through the conversion, and shows a worked example.
Contents
- Quick answer
- Why JSON needs a mapping, not just a translator
- The five mapping rules
- Choosing the root element name
- How key names are repaired
- How to convert JSON to XML, step by step
- Worked example: a person object
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
To convert JSON to XML: paste your JSON into the JSON to XML Converter, set the root element name if the default does not fit, and press Convert to XML. Keys become elements, keys starting with @ become attributes on the parent element, #text becomes text content, arrays repeat their element once per item, and null becomes an empty element. Copy the result or download it as converted.xml. Everything runs in your browser, so nothing is uploaded.
Why JSON needs a mapping, not just a translator
JSON has objects, arrays and plain values. XML has elements, attributes and text. Those building blocks overlap, but not perfectly. A JSON object key could become an element, but should some keys become attributes instead? An array has no direct XML equivalent at all. Text that sits next to nested values needs a defined home.
Because there is no single natural answer, every JSON to XML converter picks a set of rules. What matters is that the rules are fixed and documented, so the same input always produces the same output and you can design your JSON around them. That is the approach this converter takes, and the next section lists its rules in full.
The five mapping rules
Keys become elements. A key named name with the value Ana becomes a name element containing the text Ana. Nested objects become nested elements, following the structure of the JSON.
Keys starting with @ become attributes. Inside an object, a key such as @id is written as an attribute on that object's element rather than as a child element. This is how you control which values land in attributes when a receiving system expects them there.
The #text key becomes text content. When an element needs both attributes or children and a plain text value, put the text under the key #text. It is written as the element's text content instead of as a child element.
Arrays repeat their element. An array under the key tag with two items produces two tag elements, one per item, in order. There is no wrapper list element unless your JSON itself contains one.
Null becomes an empty element. A key whose value is null produces an element with no content, which is the closest honest representation XML has for an absent value.
Choosing the root element name
XML documents have exactly one root element that wraps everything else. The converter supplies a default root name, and the steps below let you change it before converting. Set it to whatever the receiving system expects: many integrations name the root after the document type, such as an order, a catalogue or a response. Getting the root name right matters because strict parsers reject a document whose root does not match their specification, even when everything inside is correct.
How key names are repaired
JSON allows keys that XML would reject as element names. Rather than failing, the converter repairs them. Characters that are not valid in an XML name are replaced with underscores, and a key that starts with a digit gets an underscore prefix, since XML names cannot begin with a number.
Repairs keep the output well-formed, but they also change the name. If a downstream system reads the converted file, check any keys that contained spaces, punctuation or leading digits, because those elements will carry the repaired names, not the originals. Special characters inside values are handled separately: ampersands, angle brackets and quotes are escaped in text and attribute values, so the content itself survives unchanged.
How to convert JSON to XML, step by step
- Open the JSON to XML Converter and paste your JSON into the input box.
- Set the root element name if the default does not fit the system you are feeding.
- Press Convert to XML.
- Review the output against the mapping rules: check that attributes landed where you intended and that arrays repeated as expected.
- Copy the XML, or download it as converted.xml.
Worked example: a person object
Take this JSON, which mixes an attribute, a plain value and an array:
{"person":{"@id":"7","name":"Ana","tag":["a","b"]}}
The conversion produces:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<person id="7">
<name>Ana</name>
<tag>a</tag>
<tag>b</tag>
</person>
</root>
Read it against the rules. The key person becomes the person element inside the root. The key @id becomes the attribute id with the value 7 on that element. The key name becomes a child element holding Ana. The array under tag repeats: two tag elements, one for a and one for b, in the original order. Nothing was dropped and nothing was invented, which is exactly what a deterministic mapping should give you.
Limitations and honest tradeoffs
The output is one consistent dialect of XML. Systems expecting a specific schema, with particular namespaces or element order rules, may need further adjustment after conversion. The converter guarantees well-formed XML that follows the documented mapping; it cannot know the private contract of the system you are sending it to.
Key name repairs also deserve a second look. They keep the document valid, but a repaired name is a changed name, and a strict receiver matching on element names will see the repaired version. Finally, XML built this way carries no schema or type information: numbers, booleans and strings all become text, because XML itself has no other notion of a value.
Common mistakes
- Expecting every key to become an element. Keys starting with
@become attributes by design. If a value came out as an attribute and you wanted an element, rename the key without the@. - Looking for a wrapper around array items. Arrays repeat their element directly. If your receiver expects a container element, include that container as a key in the JSON itself.
- Forgetting the root name. The default root is fine for quick jobs, but integrations often require a specific root element. Set it before converting, not after.
- Ignoring repaired key names. Spaces and leading digits in keys are changed to keep the XML valid. Check those elements before sending the file on.
- Assuming the dialect fits every schema. Namespaces and strict element ordering are receiver-specific. Treat the output as a correct starting point and adjust where a schema demands it.
FAQs
How do I make an attribute instead of an element? Start the key with @. A key named @id inside an object becomes an id attribute on that object's element, while a plain id key would become a child element.
What happens to arrays in the JSON? Each array item becomes its own element with the same tag name, in order. An array of two strings under the key tag produces two tag elements.
Can I convert the XML back to JSON afterwards? Yes. The XML to JSON Converter uses the same @ and #text conventions, so a round trip keeps attributes and text content recognizable instead of losing them.
What if my keys contain spaces or start with a number? Characters that are not valid in XML names are replaced with underscores, and a key starting with a digit gets an underscore prefix, so the output stays well-formed.
Is my JSON uploaded to a server? No. The conversion runs in your browser and the data never leaves your device, which matters when the JSON contains internal names or payloads.
Conclusion
Converting JSON to XML is predictable once the mapping is fixed: elements for keys, attributes for @ keys, #text for loose text, repeated elements for arrays, and an empty element for null. The JSON to XML Converter applies those rules in your browser and hands you the file as converted.xml. When you need to travel in the opposite direction, our companion guide on how to convert XML to JSON covers the same conventions from the other side. For more guides in this series, see our Data and JSON Tools guides.
References
- W3C, Extensible Markup Language (XML) 1.0 specification: https://www.w3.org/TR/xml/
- MDN Web Docs, on XML and how browsers parse it: https://developer.mozilla.org/en-US/docs/Web/XML