JSON to XML Converter
- An XML document has exactly one root element and a JSON text need not have one thing at the top, so the whole document was wrapped in <root>. Reading this XML back gives an object with a root member around what you started with.
- An array became repeated <item> elements inside <tags>. XML has no arrays (DFCF14), so reading this document back gives an object with an item member rather than the array it started as.
Going the other way? Convert XML back to JSON →
How do you convert JSON to XML?
JSON to XML turns an object into a tree: each key becomes an element, each array element becomes a repeated tag, and each value becomes a text node.
- Paste the JSON above. Every key has to be a legal XML element name, because renaming one would change the document.
- Wrap the whole document in a named root element, since an XML document has exactly one root and a JSON text need not have one thing at the top.
- Write each key as an element and each value as a text node, and write an array as one repeated tag per element.
- Read the result back to see what the trip costs: an array returns as an object with a repeated member, not as the array it started as.
This page writes XML with one element per key, a repeated tag per array element, values as text nodes, and a named root element.
JSON to XML Conversion Table
Six JSON shapes, one XML form each. The gap runs the other way from the XML to JSON page: there JSON had too few containers, here XML has too few types. none in XML 1.0 itself - every attribute value and text node is character data.
| JSON shape | XML form | Note |
|---|---|---|
| {"name": "Ada"} | <name>Ada</name> | one element per key |
| "Ada" | a text node | the five predefined entities are written out where needed |
| 36 | <age>36</age> | nothing records that it was not a string |
| true | <active>true</active> | nothing records that it was not a string |
| null | <note/> | an empty element, which is also what an empty string writes |
| ["x", "y"] | <item>x</item><item>y</item> | a repeated tag per element; the array itself is gone |
| the whole document | <root>...</root> | XML has exactly one root and JSON need not have one |
A format is defined by what it can say. The rows below put the two halves of this conversion side by side, so the gap the conversion has to cross is a thing you can read rather than a thing you find out later. Every value in the table is the wording the defining document uses, not a summary of it, and a dash means the format has no answer to that row at all.
| What it says | JSON | XML |
|---|---|---|
| Specification | RFC 8259, Standards Track, December 2017; also ECMA-404 | W3C Recommendation, Extensible Markup Language (XML) 1.0 |
| Media type | application/json | application/xml and text/xml |
| Structure | objects and arrays, nested to any depth | a tree of elements, each carrying attributes and content |
| Delimiter | a comma (%x2C) between two members of an object and between two elements of an array | — |
| Quoting | double quotes; a quotation mark, a reverse solidus and the control characters U+0000 to U+001F must be escaped with a reverse solidus | — |
| Type set | string, number, boolean, null, object, array | none in XML 1.0 itself - every attribute value and text node is character data |
| Nesting | unlimited by the grammar; a parser may set its own depth limit | unlimited |
| Ordering | an object is unordered; an array is ordered | — |
| Child order | — | significant - element content is an ordered production |
| Attribute order | — | not significant |
| Attribute names | — | an attribute name must not appear more than once in the same start tag or empty-element tag |
| Mixed content | — | character data may be interleaved with child elements |
| Comment | none - the grammar has no comment production | <!-- ... --> |
| Encoding | UTF-8 | — |
Worked Example: JSON to XML
One object with a string and a two-element array, converted here at build time by the same code the box above runs. The working under it names each key and the element it became.
{"name":"Ada","tags":["one","two"]} <root><name>Ada</name><tags><item>one</item><item>two</item></tags></root> Result: the JSON above is the XML beside it, and every decision that made it is listed below.
The array became two tags with the same name, and nothing in the XML says it was an array. JSON has one kind of container for both, so an XML to JSON conversion has to choose. Attributes and child elements collapse into the same kind of member, a single child element gives no way to tell one value from a one-element array, and mixed content - text interleaved with child elements - has no JSON shape at all.
A key can also be asked to become an attribute rather than an element, by writing it with the declared prefix. An XML attribute may appear at most once per element and the order of attributes in a start tag carries no meaning; a child element may repeat any number of times and its order does carry meaning. An attribute value is always text, while an element can hold text, other elements, or both.
Check it in reverse
Reading the XML back gives you an object one level deeper than the one you started from, because the root element survives the trip and becomes a member. The repeated tags come back as an array, so that part holds. What does not is the type: every text node reads as ' + 'a string, so a number written out here returns as a string unless something reads it again.
To prove nothing was lost, convert the XML result back to JSON and compare it with the file you started from.
What This Conversion Cannot Carry
XML carries structure that JSON does not and types that JSON does, so this direction loses the types and gains a wrapper. string, number, boolean, null, object, array go in; character data comes out.
- The root element XML requires
- An XML document has exactly one root element and a JSON text need not have one thing at the top, so the whole document was wrapped in <root>. Reading this XML back gives an object with a root member around what you started with.
- An array
- An array became repeated <item> elements inside <tags>. XML has no arrays (DFCF14), so reading this document back gives an object with an item member rather than the array it started as.
- A null
- A JSON null became the empty element <a/>. XML 1.0 has no null (FMT-xml-types), so null, "" and an element with no content are one thing in the output.
- A number or a boolean
- A JSON number or boolean became a text node. FMT-xml-types: XML 1.0 has no type set at all — every attribute value and text node is character data — so nothing in the XML records that it was not a string.
- An empty object
- An empty object became the empty element <empty/>.
Each line above is written by the converter itself, and it appears under your own result whenever your document raises it. Nothing is dropped without one.
The sample above reverses exactly - read the same elements back in as JSON .
What Has To Be Decided Before JSON Becomes XML?
What the root element is called
It is named by the converter, because the document you paste in has no name of its own. a tree of elements, each carrying attributes and content, and the wrapper is the only way a JSON array or a bare value can become one of those.
Whether a key can be an XML name at all
A key that cannot be an element name stops the conversion rather than being renamed. RFC 8259 gives JSON four primitive types - string, number, boolean and null - and two structured types, object and array. There is no date type and no separate integer type, so a date arrives as a string and every number is read the same way.
Whether a key becomes an attribute or an element
The declared prefix decides it, and nothing else can. not significant for attributes while significant - element content is an ordered production, so the choice changes what the document means, not only how it looks.
JSON to XML in Code
The same conversion in two lines, one per language. Both need a root element name of their own, because JSON does not supply one. Neither line reports what the conversion could not carry; the converter above does.
Python: ElementTree.tostring(build(json.load(open("in.json")), "root"), encoding="unicode") JavaScript: new XMLSerializer().serializeToString(build(JSON.parse(s), "root")) Common Mistakes
- Expecting an array to come back as an array. It becomes a repeated tag, and reading that back gives an object with a repeated member.
- Using a key that is not a legal XML name. A key with a space or a leading digit cannot be an element name, and renaming it would change the document.
- Forgetting the root wrapper on the way back. A round trip through XML returns a document one level deeper than the one that went in.
Frequently Asked Questions
Why does XML need a root element when JSON does not?
Because an XML document is a tree and a tree has one top. A JSON text can be an array or a bare value, so the converter names a root element and wraps the document in it.
Should a JSON key become an attribute or an element?
An element, unless you write it with the declared prefix. An attribute value is always text, so a key holding an object or an array cannot become one.
What happens to a JSON key that is not a legal XML name?
The conversion stops and names the key. Renaming it would change the document, so the choice is yours: rename the key, or convert to a format that takes any key.
What happens to null and to boolean values?
A null becomes an empty element and a boolean becomes the text true or false. XML has no types of its own, so nothing in the output records what either one was.
Yes. The same file converts straight back in the XML to JSON direction .
Related Conversions
Pick another pair from the format converter index.