XML to JSON Converter
XML has attributes and text. JSON has neither, so something has to give.
XML has attributes and text. JSON has neither, so something has to give. If you don’t know whether id landed in @attributes or as a sibling, the convert “worked” and your code is still wrong.
Paste the XML. Find one element that had an attribute and a body. If those aren’t two different keys, the mapping will lie to you later.
How to use it
- Attributes become
@idon the object (@plus the attribute name). Text becomes"#text": "…". That convention is not standard JSON. It is a choice. Match it in your code or remap it. - Repeated child tags become an array. A single child stays an object. One vs many records is the usual off-by-one after convert.
- Namespaces (
soap:,xsi:) stay in the keys unless you strip them. They are not decoration. - Mixed content (
<p>hello <b>x</b></p>) does not have a clean JSON shape. Expect#textplus abkey, not a sentence.
When it breaks
- Self-closing vs empty (
<a/>vs<a></a>) often collapse to the same JSON. Don’t use this to round-trip markup you need bit-perfect. - Comments and processing instructions are dropped. The JSON will not mention them.
- Huge XML with DTD/entities is a parser footgun. If it tries to fetch a DTD, don’t. That’s XXE, not conversion.
XML
Loading editor...
JSON
Loading editor...
Features
- Attributes become @name keys (xml2js-style), not flattened onto the parent
- Element text lives in #text when the element also has attributes or children
- Repeated sibling tags become JSON arrays; a single sibling stays an object
- Comments are dropped; JSON has no comment node
Common Use Cases
- See why <book id="1">Hello</book> is {"@id":"1","#text":"Hello"}
- Predict whether item is an object or an array after one vs two siblings
- Convert a SOAP-like tree without pretending namespaces round-trip
- Inspect RSS item lists as arrays of objects
XML has attributes and text
XML has attributes and text. JSON has neither, so something has to give. If you don’t know whether id landed in @attributes or as a sibling, the convert “worked” and your code is still wrong.
Paste the XML. Find one element that had an attribute and a body. If those aren’t two different keys, the mapping will lie to you later.
Attributes become @id on the object (@ plus the attribute name). Text becomes "#text": "…". That convention is not standard JSON. It is a choice. Match it in your code or remap it. Repeated child tags become an array. A single child stays an object. One vs many records is the usual off-by-one after convert.
Namespaces (soap:, xsi:) stay in the keys unless you strip them. They are not decoration. Mixed content (<p>hello <b>x</b></p>) does not have a clean JSON shape. Expect #text plus a b key, not a sentence.
Examples
<book id="1">Dune</book><list><item>a</item></list><list><item>a</item><item>b</item></list>{"a":1}Frequently Asked Questions
XML has attributes and text. JSON has neither, so something has to give. If you don’t know whether id landed in @attributes or as a sibling, the convert “worked” and your code is still wrong. Attributes become @id on the object (@ plus the attribute name). Text becomes "#text": "…". That convention is not standard JSON. It is a choice. Match it in your code or remap it.
Repeated child tags become an array. A single child stays an object. One vs many records is the usual off-by-one after convert.
Namespaces (soap:, xsi:) stay in the keys unless you strip them. They are not decoration. Mixed content (<p>hello <b>x</b></p>) does not have a clean JSON shape. Expect #text plus a b key, not a sentence.
Self-closing vs empty (<a/> vs <a></a>) often collapse to the same JSON. Don’t use this to round-trip markup you need bit-perfect. Comments and processing instructions are dropped. The JSON will not mention them. Huge XML with DTD/entities is a parser footgun. If it tries to fetch a DTD, don’t. That’s XXE, not conversion.
Common Mistakes
Tips
- If you need a stable array, ensure the sample has at least two sibling tags of that name, or wrap in code.
- Look at @ and #text before writing TypeScript interfaces from a single example.
- Namespaces: check the tag names in the JSON. You may want to strip prefixes in the app.