How-to guide

How to Convert XML to JSON

To convert XML to JSON, paste the document into a converter and run it. Elements become object keys, attributes are preserved with a prefix to distinguish them from child elements, and repeated sibling elements become arrays. The conversion is not perfectly lossless, because XML has concepts — attributes, namespaces, mixed content, comments — that JSON has no direct equivalent for.

XML to JSON Converter

Convert any XML document into readable JSON.

Open the tool

XML and JSON model data differently, so converting between them always involves a convention. XML distinguishes an attribute from a child element; JSON has only keys. XML allows text and elements to be mixed inside one element; JSON has no way to express that. Any converter must decide how to encode those differences.

A word on safety: XML parsers that resolve external entities can be induced to read local files or make outbound network requests. That is the XXE class of vulnerability, and it is why parsing here is done with entity resolution disabled.

Step by step

  1. Paste the XML

    Include the root element. A fragment with several top-level elements is not a well-formed XML document and will be rejected before conversion starts.

  2. Fix well-formedness errors first

    Unclosed tags, mismatched nesting and unescaped ampersands all stop the parse. The error position tells you where the document became impossible to parse.

  3. Check how attributes came through

    Attributes are preserved with a distinguishing prefix rather than merged with child elements, so an id attribute and an id child element do not collide.

  4. Watch the single-versus-repeated element trap

    Repeated sibling elements become a JSON array. When a list happens to contain exactly one item, it can convert to a single object instead of a one-element array — handle both shapes in code that consumes the output.

  5. Format the result

    Run the JSON through a formatter to check the nesting is what you expected, particularly around arrays and attributes.

Example

The id attribute is preserved with a prefix; two <item> siblings become an array.

XML

<catalogue>
  <item id="1">Alpha</item>
  <item id="2">Beta</item>
</catalogue>

JSON

{
  "catalogue": {
    "item": [
      { "@id": "1", "#text": "Alpha" },
      { "@id": "2", "#text": "Beta" }
    ]
  }
}

Frequently asked questions

Is the conversion lossless?
Not entirely. Attributes and text content are preserved, but comments, processing instructions, namespace prefixes and mixed content have no natural JSON equivalent. Round-tripping XML to JSON and back will not always reproduce the original document byte for byte.
How are attributes represented?
With a distinguishing prefix on the key, so an attribute named id and a child element named id remain separate. Without that convention the two would collide and one would silently overwrite the other.
Why did my list become an object?
Because it contained exactly one element. XML has no way to signal "this is a list of one", so a single occurrence looks identical to a non-repeating element. Code that consumes the output should accept both an object and an array.
Are external entities processed?
No. Entity resolution is disabled, which prevents XXE attacks in which a crafted document makes the parser read local files or issue outbound requests.
Why does my document fail with an undefined entity error?
XML predefines only five entities: &lt; &gt; &amp; &quot; and &apos;. HTML entities such as &nbsp; are not valid unless a DTD declares them. Replace them with numeric references such as &#160;.

Tools used in this guide

All tools →
All guides →

Last reviewed .