XML to JSON Converter
A parser that refuses malformed XML instead of guessing, and says which tag broke it.
The three questions every XML converter answers differently
- Where do attributes go? Here they become keys prefixed with
@, so<item id="7">becomes{"@id": "7"}. - Where does element text go? When an element has attributes and text —
<a id="1">x</a>— the text becomes a#textkey, because it has to coexist with the attributes. When an element has only text, the text is the value:<name>Ada</name>becomes simply"Ada". - How are repeated siblings handled? Two or more children with the same tag become an array; a single child stays a scalar. This is the pragmatic choice, and it is why consumers often fail when a list happens to contain exactly one item.
That last point is a genuine hazard in production systems. An API that returns <item> once for a single result and twice for a list produces JSON of two different shapes, and the client breaks on the day the list has one element. Robust code normalises by checking the type before iterating — or you convert with a schema that declares which elements are always collections.
What this parser rejects
- Mismatched tags.
<a><b></a>raises an error naming both tags, rather than silently guessing an intended structure. - Unclosed tags. The parser reports which element was never closed.
- Multiple root elements. XML permits exactly one root; a document with two is not XML and is reported as such.
- Documents with no elements at all. Text-only input is not a document.
Well-formed is not the same as valid
A parser can only check well-formedness — that tags nest, close and appear once as a root. It cannot check that the document matches a DTD or XSD schema, that required attributes are present, or that element order follows the rules. Those are separate, schema-level concerns. So a successful conversion means 'this was parseable', not 'this document is correct'.
Common sources of XML that will not convert
- HTML, pasted as XML. Real-world HTML routinely has unclosed
<br>,<img>and<li>elements. Only XHTML is well-formed. Run it through an HTML parser, not an XML one. - RSS and Atom feeds. These are well-formed and convert cleanly — one of the most common reasons to need this tool.
- SOAP envelopes. Well-formed by construction, though the namespaces in the tags (
soap:Body) come through literally in the JSON keys. - Config files with a DOCTYPE. The declaration is skipped, but any entity it defines is not expanded.
How to use it
- Paste XML or an RSS/Atom feed.
- Choose whether the root element should become a wrapper key in the output.
- Convert.
- If parsing fails, read the reported tag name — it identifies the exact malformation.
Worth knowing
- Attributes become keys prefixed with
@; element text with siblings becomes#text. - Repeated sibling tags become arrays; a single occurrence stays a scalar.
- Only well-formedness is checked — never DTD or schema validity.
- Real-world HTML is usually not well-formed and will fail to parse.
Limitations
- Namespaces are kept literally in tag names and not resolved to URIs.
- Entity references defined in a DTD are not expanded.
- A single-element list produces a scalar, which consumers must accommodate.
- Mixed text-and-element content is flattened into a text key alongside child keys.
Frequently asked questions
How do I convert an RSS feed to JSON?
<item> or <entry> elements become an array.Why does an HTML snippet fail to parse?
<br>, <img> and <li> tags; an XML parser does not. Use an HTML parser for HTML, or convert the fragment to XHTML first.How are attributes represented?
@ — <a id="1"/> becomes {"@id": "1"}. When the element also has text, the text is stored under a #text key so both can coexist.