XML to JSON and JSON to XML
How to convert XML to JSON
- Leave Direction on XML to JSON. Paste your XML into the box, or click Open an XML file and choose an .xml or .txt file.
- Read the line under the box. It names the root element, or says what is wrong and on which line.
- Set the options: keep attributes, turn numbers and true/false into JSON values, write every child as a list, and indent.
- Use Copy JSON or Download .json. The file is saved as converted.json.
- To go back, change Direction to JSON to XML, set a root name and the declaration, and download converted.xml.
What the converter does
The page reads XML with fast-xml-parser, a common library, and checks the document before converting it. The library comes from jsDelivr on your first action, and the browser refuses it if the file does not match a pinned hash. Our request log showed that single GET, no request bodies, and nothing containing the XML we entered. Google ad and analytics scripts, blocked for the test, load as on every GramFile page. Only Chromium was tested.
Our 307-byte sample (a catalog with two items, CDATA text and an attribute on the root) came out as 397 characters of indented JSON, or 227 as one line. A blank box gives no output and no message.
Attributes, one tag or many
With Keep attributes ticked, an attribute becomes a key with an @_ prefix: id="1" turned into "@_id": "1". Untick the option and they vanish: the sample shrank from 397 to 272 characters. Text that sits beside attributes goes under "#text", so <p lang="en">hello</p> became {"#text": "hello", "@_lang": "en"}, and with attributes off it became the plain string "hello".
The shape depends on how many tags there are. In the sample, the first item has two <tag> elements and became a list, while the second has one and became a string. Code that reads tag has to handle both. Tick Always write child elements as lists and every child is a list, even name and note, which took the output to 477 characters.
Numbers, true/false and text
Everything is text by default: 007, 42 and true all came out as strings. With Turn numbers and true/false into JSON values ticked, 42 became 42, true became true, 1.50 became 1.5 and 1e3 became 1000, losing the trailing zero. The values 007, 0x10 and the 19-digit 1234567890123456789 stayed text even then.
Entities are decoded: < gave <, and é gave é. CDATA is read as plain text.
What is dropped or merged
Comments and processing instructions such as <?xml-stylesheet?> are dropped, and so is the XML declaration. An empty element, <a/> or <b></b>, becomes "", and a self-closing element with an attribute becomes {"@_x": "1"}. Namespace prefixes stay in the names, as "ns:r" and "@_xmlns:ns".
Mixed content loses its order. <p>lead <b>bold</b> tail</p> became {"b": "bold", "#text": "leadtail"}, with the two text pieces joined and the position of the bold word gone. Check this case before converting document-style XML.
A DOCTYPE with a simple internal entity was expanded: &e; became EXPANDED. When we called the converter code directly, a chain of nested entities stayed unexpanded and an external one was refused.
JSON to XML
In this direction one top-level key becomes the root element. With several keys, or a list, the page wraps everything in the root name you set, root unless you change it: {"a":1,"b":2} gave <root> with <a> and <b> inside, and the list [1,2] gave two <item> elements. Keys that start with @_ are written as attributes and #text as text. A null and an empty string both gave an empty element, an empty list gave nothing at all, and special characters were escaped.
The sample's JSON went to XML and back to the same JSON. The XML itself differs from the original: the CDATA block became escaped text, and the declaration is added by the page, so untick the declaration option if you do not want it.
Errors and the 10 MB limit
Broken XML is reported with a line: a closing </c> where </b> belonged was flagged on line 3, and an unclosed root on line 1. Two roots and text before the root are refused. JSON that cannot be XML is refused with a reason: a key with a space or a leading digit, a list inside a list, a single value such as 5. Files up to 10 MB are accepted: a 2,000,077-byte file converted in about 1.9 seconds. A 10.6 MB file was turned away.
Related conversions
For YAML, use the YAML and JSON converter. To tidy or validate the JSON first, open the JSON formatter, and to turn a list of records into rows, use JSON to CSV.
Measured results
| Input | Setting | What came out |
|---|---|---|
| sample.xml, 307 bytes | Defaults | 397 characters; item is a list of 2, tag is a list in item 1 and a string in item 2, @_id and @_kind keys |
| sample.xml | Attributes off | 272 characters, no @_ keys |
| sample.xml | Lists on | 477 characters; name, tag and note are lists |
| sample.xml | Numbers on | @_id is 1 and 2 instead of "1" and "2" |
| id 007, 0x10, 1234567890123456789 | Numbers on | All three stay text; 42 becomes a number |
| p with lang attribute and text | Defaults | #text and @_lang keys |
| lead, b, tail | Defaults | "#text": "leadtail" next to "b": "bold" |
| {"a":1,"b":2} | JSON to XML | root element with a and b inside |
| 2,000,077-byte file | Defaults | Converted in about 1.9 s |
When a script or XSLT is the better choice
A script (in Python, for example), xmllint or an XSLT stylesheet is the better fit for files over 10 MB, for XML that depends on element order or namespaces, or for a conversion that runs on a schedule. Use this page to see how a small sample comes out first.
Frequently asked questions
Is my XML uploaded?
No. Conversion happens in your browser tab. Only the fast-xml-parser library is downloaded, from jsDelivr, and our log showed nothing that carried the XML we pasted or opened.
What does @_ mean in the output?
It marks an attribute. id="1" on an element becomes "@_id": "1" in that element's object. Untick Keep attributes to leave them out.
Why is a tag a list in one place and a string in another?
A tag that appears once is written as a single value and a repeated tag as a list. Tick Always write child elements as lists to make every child a list.
Why did a number stay text?
Numbers are text unless you tick the number option, and 007, 0x10 and very long IDs stay text even then.
Does the order of elements survive?
Not always. In our test, text mixed with elements was rearranged.