How to convert JSON to XML
- Drop a
.jsonfile into the box above or paste the JSON straight in. Newline-delimited JSON works too — one object per line. - Set the root element name, and the name used for entries when a list has to become repeated elements.
- Keys starting with
@become attributes. Leave that on if your JSON came from our XML to JSON page and you want the attributes back where they started. - Press Convert to XML and read the report. Renames and collisions are the part worth looking at.
JSON keys and XML names do not overlap
A JSON key can be any string at all. An XML element name cannot: it has to start with
a letter or an underscore, it cannot contain spaces or most punctuation, and it is not
allowed to begin with the letters xml in any casing. So a converter has
to rename, and every converter does:
| JSON key | Why it is illegal | Becomes |
|---|---|---|
"user name" | Space | user_name |
"123id" | Starts with a digit | _123id |
"pr!ce" | Punctuation | pr_ce |
"xmlThing" | Reserved prefix | _xmlThing |
That much is unavoidable. What is avoidable is doing it in silence, because renaming is not always a cosmetic change.
The rename that costs you a field
Consider a record with both "user name" and "user_name"
— two separate fields, which happens constantly in data assembled from more than
one source. Both are legal JSON. Both become <user_name>. The output
is well-formed XML in which the two values are no longer distinguishable from one
another, and nothing about the file says so.
This page detects that case and reports it as a problem, naming both keys. It is
usually a five-second fix at the source, and it is only findable while you still know
the conversion happened. There is also an option to write the original key into an
original-name attribute on any element that had to be renamed, which
makes the whole mapping reversible rather than just documented.
The character that makes a file unreadable
JSON strings may contain escaped control characters — \u0001 and
friends. XML 1.0 cannot represent them at all: not literally, not as a numeric
reference, not inside CDATA. There is no escape that works.
A converter that copies the character through produces a file that fails to parse in every conformant reader, and the failure surfaces somewhere else, later, as somebody else's error message. Here those characters are removed and counted, so the output is a file that actually opens and you know one byte of input did not survive.
What each JSON type becomes
| JSON | XML | Note |
|---|---|---|
| Object | Element with child elements | Keys become tag names |
| Array | The element repeated | A one-entry list is indistinguishable from a plain value |
| String, number, boolean | Element text | XML has no types; a schema supplies them |
null | Empty element | Or xsi:nil="true" if you switch it on |
"@id" | An attribute | Round-trips with our XML to JSON page |
"#text" | The element's own text | Lets you rebuild mixed content |
Numbers come out better here than in the other direction
Going from XML to JSON, long identifiers are a hazard: JavaScript cannot hold a whole number past 9,007,199,254,740,991 exactly, so converting one changes its final digits. Going this way the risk is smaller, because XML has no number type — everything is text. A large value that is already a JSON number has been through that narrowing before it reached this page, though, so if your IDs matter, keep them as strings in the JSON rather than as numbers.
Next steps
XML to JSON goes back the other way and keeps repeating elements as arrays so the shape does not drift between files. XML to CSV turns a record-shaped XML document into rows, and JSON to CSV does the same from JSON. All of them are listed on the data tools page.