XML to JSON
Published Updated
Convert XML to indented JSON keyed by the root tag, with attributes as @name keys and mixed text under #text. Every value stays a string, so 007 and true come back as written, and a lone element is not wrapped in an array.
How each XML piece is represented
XML has attributes, text and nested elements where JSON has only keys and values, so a convention is needed. This page uses the common one, and the rows below are its results from Google Chrome 156.
JSON itself has four primitive types, strings, numbers, booleans and null, and two structured types, objects and arrays (RFC 8259, section 1). This converter writes every text and attribute value as a string and an empty element as null.
| XML | JSON |
|---|---|
<person id="7"> | Key "@id": "7" inside person |
<name>Jane</name> | "name": "Jane", a plain string |
<tag>a</tag><tag>b</tag> | "tag": ["a", "b"] |
<note/> | "note": null |
<item id="1">A</item> | {"@id": "1", "#text": "A"} |
<ok>true</ok> | "ok": "true", still a string |
& and A | & and A, decoded |
Surrounding whitespace is trimmed, and text that is only whitespace between tags is ignored.
Mixed content and uneven arrays
The W3C XML recommendation defines mixed content as character data interspersed with child elements. It is normal in documents such as HTML-like markup and rare in data feeds. This converter keeps the words but not their position among the tags.
Repeated elements can also differ in type. For <item id="1">A</item><item>B</item> the array holds an object first and the string "B" second, so check each entry before reading @id.
Converting an XML file
Step 1
Paste XML or add an .xml file
A file is read as text and placed in the box, so you can edit it before converting. An empty file is turned away before it reaches the parser.
Step 2
Press the convert button
Invalid XML shows the browser's own message with a line and column. Documents past the size limits are refused with a count.
Step 3
Copy the JSON
The result is indented two spaces and starts with the root tag as the only key. Copy JSON is the only output control; there is no download.
Limits that stop a conversion
- More than 5,000,000 characters of input.
- Nesting deeper than 500 levels, or more than 200,000 elements.
- More than 10,000 attributes on a single element.
- A second root element or an unclosed tag, each with the parser's line and column. An empty box shows no convert button.
To go back, JSON to XML writes elements only, never attributes. XML Formatter is the place to check a file before converting it.
Encodings, entities and parser messages in three browsers
A chosen file is read with Blob.text(), which MDN says presumes the data is UTF-8, so the encoding named in the XML declaration is not consulted. The text then goes to the browser's XML parser through DOMParser, which reports bad XML by returning a document with a parsererror element, and the page shows that element's text. Measured in Google Chrome 157, Firefox 155 and Playwright's WebKit build (not the Safari app) with generated files and typed XML; where a row gives one result, all three agreed:
| Input | Result |
|---|---|
A UTF-8 file with a byte order mark, holding <r>café</r> | "r": "café"; the mark does not reach the JSON |
| A file declaring encoding="ISO-8859-1", with é stored as the byte E9 | "r": "caf�": the byte becomes U+FFFD, the replacement character, already in the text box |
| A file declaring windows-1252, with curly quotes stored as the bytes 93 and 94 | "�hi�" |
| A UTF-16 LE file with a byte order mark | Converted in Chrome 157 and the WebKit build. Firefox 155 read the bytes as UTF-8 and stopped with XML Parsing Error: not well-formed at line 1, column 1 |
| A file with CR LF line ends inside an element's text | "line 1\nline 2": the carriage return is gone |
| A tab and a line break typed inside an attribute value | Both become spaces: "@a": "x y z" |
The same tab and line break written as 	 and | Kept: "x\ty\nz" |
<!ENTITY who "World"> in the DOCTYPE, then <r>Hello &who;</r> | "r": "Hello World" |
An entity declared as SYSTEM "ext.txt" | "r": null, and none of the three requested ext.txt |
 , or declared as   in the DOCTYPE | A no-break space (U+00A0) between the words |
with no declaration, or a bare & as in Fish & Chips | Refused, with each browser's own message (below) |
- Chrome 157: error on line 1 at column 10: Unexpected entity: nbsp, and for the bare & error on line 1 at column 10: Unexpected token inside entity:
- Firefox 155: XML Parsing Error: undefined entity at Line Number 1, Column 5, and not well-formed at Column 10. Its message also gives the page's address after Location and repeats the line with a caret under the column.
- The WebKit build: error on line 1 at column 11: Entity 'nbsp' not defined, and error on line 1 at column 10: xmlParseEntityRef: no name.
XML to JSON questions
Why is one tag an array in one file and a plain value in another?
A tag that appears once stays a single value, and a tag repeated among siblings becomes an array. Code that reads the result has to accept both shapes.
Why are all my numbers strings?
XML text has no types, and guessing would lose data. 007 stays "007", 1.50 stays "1.50", and the text true stays a string. Convert the fields you need in the code that reads the JSON.
Where did my XML comments go?
Comments and processing instructions are dropped, since they describe the document and are not data. CDATA sections are kept as plain text, so a section holding 1 < 2 gives the string "1 < 2".
What happens to namespaces?
Prefixes stay in the key names. In the test, an element a:r declaring xmlns:a gave the key a:r with a separate @xmlns:a entry. No prefix is resolved to its URI.
Why does text mixed with tags look scrambled?
A paragraph such as Hello <b>World</b>! becomes a b key and a #text of "Hello !". JSON objects have no slot for order between text and elements, so the interleaving is lost.
Is the XML sent anywhere?
No. The browser's DOMParser reads it inside the page, and the tool makes no network request. Closing the tab clears the text.
Why does my XML file show a replacement character (�) instead of accented letters?
The file is read as UTF-8 whatever its XML declaration says. A file declaring ISO-8859-1 with é stored as one byte came out as caf� in Chrome 157, Firefox 155 and the WebKit build, and windows-1252 curly quotes did the same. Save the file as UTF-8 and convert it again.
Why does stop the conversion?
It is an HTML entity, and XML knows it only when the document declares it. All three browsers refused <r>A B</r>, while  , or a DOCTYPE that declares nbsp as  , gave a no-break space between the words.
Why does the error point to a different column in another browser?
Each browser's own parser writes the message. For the same , Chrome 157 reported column 10, Firefox 155 column 5 and the WebKit build column 11, in different words; all three named line 1.