XML to JSON Converter

XML to JSON Converter

Updated August 27, 2026

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

  1. 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.
  2. Repeated child tags become an array. A single child stays an object. One vs many records is the usual off-by-one after convert.
  3. Namespaces (soap:, xsi:) stay in the keys unless you strip them. They are not decoration.
  4. Mixed content (<p>hello <b>x</b></p>) does not have a clean JSON shape. Expect #text plus a b key, 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

0 lines · 0 chars

Loading editor...

JSON

0 lines · 0 chars read-only

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

Valid - Attribute + text
<book id="1">Dune</book>
Valid - One sibling stays an object
<list><item>a</item></list>
Valid - Two siblings become an array
<list><item>a</item><item>b</item></list>
Invalid - Not XML
{"a":1}

Frequently Asked Questions

Where did my attributes go?

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.

Why is item sometimes an object and sometimes an array?

Repeated child tags become an array. A single child stays an object. One vs many records is the usual off-by-one after convert.

What happens to namespaces and mixed content?

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.

Will comments and DTDs survive?

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

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.

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.