JSON to XML
Published Updated
Convert JSON to indented XML under a root element you name, with each array item written as a repeated element. Keys that cannot be tag names, like first name or 2nd, are renamed and listed so nothing changes silently.
What happens to awkward keys
XML is stricter about names than JSON is. The XML 1.0 specification says a name cannot start with a digit, a hyphen or a full stop, and that names beginning with xml in any letter case are reserved. The page repairs every key that breaks a rule, and these results come from its own code:
| JSON key | XML element |
|---|---|
first name | first_name |
2nd | _2nd |
xml-x | _xml-x |
café, größe, 日本, 한국, имя | The same names: letters from any script are allowed |
a:b | a_b, since a colon marks a namespace prefix in XML |
😀 and a😀b | _ and a_b |
١٢ (Arabic-Indic digits) | _١٢: digits may follow the first character but not start a name |
foo_bar and foo bar together | foo_bar and foo_bar_2 |
@id and #text | _id and _text |
Root name 1 root | _1_root |
Letters from any script stay as they are, as the spec allows, and accents and digits may follow the first letter. Emoji and other symbols become an underscore, and so does a colon. Every rename is listed in the note above the result.
Arrays, nulls and numbers
A document needs exactly one root element, which JSON does not, so the page wraps everything in one.
{"tags":["a","b"]}becomes two<tags>elements, one per item, not a wrapper with children.[[1,2],[3]]under a keymgives three<m>elements, so the inner grouping is gone.null,"",[]and{}each come out as a self-closing tag such as<a/>.- Numbers and booleans are written as text:
<c>true</c>,<age>32</age>. - A bare value like
42is wrapped as<root>42</root>.
The XML declaration with UTF-8 is added at the top, and children are indented two spaces per level.
The XML, parsed back in three browsers
To see what a program reading the file gets, the converter's output was parsed with DOMParser in Google Chrome 157, Firefox 155 and Playwright's WebKit build (not the Safari app). All three read back the same text:
| JSON string value | Written in the XML as | Read back |
|---|---|---|
a < b & c > d "q" 's' ]]> | a < b & c > d "q" 's' ]]> | The original text |
x\u0001y\u0008z | xyz | xyz: the two control characters are gone |
tab\t and lf\n | A real tab and line break | Unchanged |
lf\rcr | lf cr | lf\rcr, carriage return kept |
\r\n | and a line break | \r\n |
U+D800 on its own (half of a surrogate pair) | U+FFFD | U+FFFD |
A raw carriage return would not survive: section 2.11 of XML 1.0 has parsers turn every carriage return into a line feed, which is why the page writes it as a character reference. Element names such as café, 日本, _2nd and a_b also parsed in all three browsers.
Numbers and size, from the same code run in Node.js 22.23.1: 1.50 is written as 1.5, 1e21 as 1e+21, -0 as 0, and 12345678901234567890 as 12345678901234567000. 1,000 generated product records, 146,226 bytes of minified JSON or 249,227 bytes indented, became 287,279 bytes of XML in 15,004 lines as a top-level array, each record an item element.
From JSON file to XML
Step 1
Paste JSON or add a .json file
Type a root element name if you want something other than root. A blank box means root, and an unusable name is cleaned up and listed.
Step 2
Press the convert button
Bad JSON stops with the parser's message. A document nested deeper than 500 levels is refused with its own message.
Step 3
Check the rename list, then copy
Renamed keys appear in a note above the result. There is no download button, so Copy XML is the way out.
Going the other way
XML to JSON maps attributes to @name keys, which this page cannot produce, so a round trip is lossy by design. To tidy the result, XML Formatter re-indents it.
JSON to XML questions
How do I get attributes instead of child elements?
This page never writes attributes. Every key becomes a child element, including a key such as @id, which turns into _id. If the XML must have attributes, edit the result by hand or use a script.
Can I convert the XML back to the same JSON?
Not always. An empty string, null, [] and {} all become one empty tag, and nested arrays lose their grouping. XML to JSON also reads numbers and booleans back as strings.
Why was my key renamed?
A name may hold letters from any script, digits, underscores, hyphens, dots and accent marks, and must start with a letter or underscore. Anything else, such as a space, @, #, a colon or an emoji, becomes an underscore, and a name starting with xml gets one more in front.
What happens to a JSON array at the top level?
Each item becomes an item element inside the root. {"a":[1,2]} gives two a elements instead, since the key supplies the name.
Are special characters escaped?
Ampersand, less-than and greater-than become entities, and a carriage return becomes so a parser reads it back as a carriage return. Control characters that XML 1.0 forbids, such as U+0001, are dropped, and an unpaired surrogate becomes U+FFFD.
Is the JSON uploaded anywhere?
No. The conversion is a small function in the page that builds the XML string, and it makes no request. Closing the tab discards the text.
Will a carriage return in my data survive?
Yes. A \r inside a JSON string is written as , and DOMParser in Chrome 157, Firefox 155 and the WebKit build read "lf\rcr" back with the carriage return in place. A Windows line break \r\n comes back as \r\n.
Can keys in other languages become element names?
Yes. café, größe, 日本, 한국, имя and اسم were kept as element names, and the XML parsed in all three browsers. Symbols such as emoji, spaces and most punctuation are replaced with an underscore.
Why are some elements in a different order than my keys?
Keys that are whole numbers come first, in numeric order, because that is how JavaScript orders object keys after parsing. {"b":1,"10":2,"2":3} gives <_2>, <_10> and then <b>; the number keys also gain an underscore, since a name cannot start with a digit.
How much bigger is the XML than the JSON?
About twice the minified JSON. 1,000 generated product records took 146,226 bytes as minified JSON and 287,279 bytes as XML, because every value carries an opening and a closing tag and each line is indented.