How JSON maps to XML
- Each property becomes an element with the property's name.
- Properties starting with the attribute prefix (
@by default) become attributes of the parent element. - The text key (
#text) becomes the element's text, next to its attributes. - An array becomes the same element repeated:
"roles": ["admin", "dev"]gives two<roles>elements. - XML needs exactly one root element. When the JSON has one top-level property, that is the root; otherwise everything is wrapped in a root element (
<root>by default), and a top-level array becomes<item>elements. <,>and&in text are escaped.
How to use the JSON to XML
- Paste JSON, or press Sample.
- Set the root element name, attribute prefix and text key if needed.
- Copy or download the XML.
Examples
Attributes, arrays and an exact number
{"user":{"@id":"7","name":"Ada","roles":["admin","dev"],"balance":12345678901234567890.10}}<?xml version="1.0" encoding="UTF-8"?>
<user id="7">
<name>Ada</name>
<roles>admin</roles>
<roles>dev</roles>
<balance>12345678901234567890.10</balance>
</user>
The balance is copied as written; converting through JavaScript numbers would print 12345678901234567000.
A top-level array
[{"name":"Ada"},{"name":"Grace"}]<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Ada</name>
</item>
<item>
<name>Grace</name>
</item>
</root>
Text with an attribute
{"note":{"@lang":"en","#text":"Tea < coffee"}}<?xml version="1.0" encoding="UTF-8"?>
<note lang="en">Tea < coffee</note>
Common errors and how to fix them
Property names that aren't XML names
JSON property names can be anything; XML names can't contain spaces or start with a digit. Rename the property, for example to first_name.
{"first name":"Ada"}{"first_name":"Ada"}The tool reports: "first name" can't be an XML element or attribute name: names start with a letter or _ and contain no spaces.
Options
- Root element
- The wrapper used when the JSON doesn't have exactly one top-level property (default root).
- Attribute prefix and text key
- Which properties become attributes (@ by default) and text (#text).
- XML declaration
- Start with <?xml version="1.0" encoding="UTF-8"?>.
Limitations
- null and empty strings become empty elements; true and false become text.
- Property names must be valid XML names.
- No namespaces are added; write prefixed names (soap:Body) and xmlns attributes yourself.
Frequently asked questions
How do I make an attribute?
Start the property name with @: {"book": {"@id": "b1", "title": "XML"}} gives <book id="b1"><title>XML</title></book>.
Does it round-trip with XML to JSON?
Yes, both use the same conventions; converting XML to JSON and back gives the same XML, apart from comments and whitespace.
Why was my JSON wrapped in <root>?
XML documents have exactly one root element. When your JSON has several top-level properties or is an array, they're wrapped in the root element you choose.
Is my JSON uploaded?
No. The conversion runs in your browser, and nothing you paste is sent anywhere.