EthicalFusion

XML to JSON Converter

Convert XML to clean, formatted JSON instantly — attributes and nested tags preserved. Free and private.

Was this tool helpful?

Thanks for the feedback!

Paste XML and this converter returns formatted JSON, keeping attributes as keys that start with @ and turning repeated tags into arrays. It is handy for developers working with RSS feeds, SOAP responses, sitemaps or configuration files who want the data in a shape JavaScript and most APIs handle more naturally.

Example

Input:

<order ref="1042" status="paid">
  <customer>Asef</customer>
  <item sku="A1">Tea</item>
  <item sku="B7">Coffee</item>
  <note/>
</order>

Output after clicking Convert to JSON:

{
  "order": {
    "@ref": "1042",
    "@status": "paid",
    "customer": "Asef",
    "item": [
      { "@sku": "A1", "#text": "Tea" },
      { "@sku": "B7", "#text": "Coffee" }
    ],
    "note": ""
  }
}

The real output uses two-space indentation with each key on its own line; the item objects are shown compactly here to save space.

The conversion rules

  • The root element becomes the single top-level key.
  • Attributes become keys prefixed with @, so ref="1042" becomes "@ref": "1042".
  • A child element becomes a key named after its tag. When the same tag appears more than once under one parent, its values are collected into an array.
  • An element with only text becomes a plain string, as with "customer": "Asef".
  • An element with attributes or children and also text keeps the text under #text.
  • An empty element without attributes becomes an empty string.
  • CDATA sections are treated as ordinary text, and surrounding whitespace is trimmed.
  • Comments and processing instructions are dropped.

Gotchas when you use the JSON in code

One item or many

If an order has a single <item>, the output is an object, not a one-element array. Code that loops over order.item should normalise it first, for example [].concat(order.item).

Everything is a string

Numbers and booleans stay as text, such as "1042" and "true". Convert them in your code where needed.

Mixed content loses its order

For <p>Hello <b>world</b>!</p>, the result is {"b": "world", "#text": "Hello !"}: the text pieces are joined and their position around the bold tag is lost. This format suits data-style XML, not rich documents such as XHTML.

Namespaces

Prefixed tags keep their prefix, so a SOAP envelope gives keys like "soap:Body", and namespace declarations appear as attributes such as "@xmlns:soap".

When conversion fails

The XML is checked by the browser's built-in parser. If any tag is left unclosed, attribute quotes are missing, or a bare & appears in the text, you get the message "Invalid XML — check that every tag is closed." HTML pages are usually not well-formed XML and will fail for the same reasons. Tidy and inspect the input in the XML Formatter, and clean up the resulting JSON in the JSON Formatter. Because parsing happens in your own browser, the XML never leaves your device.

Frequently asked questions

How are XML attributes converted?
Each attribute becomes a key starting with @, so a ref attribute becomes "@ref". The attribute value is kept as a string.
Why is a repeated tag sometimes an array and sometimes an object?
Tags become arrays only when they appear more than once under the same parent. A single occurrence is converted to a plain value, so normalise it in your code.
What does #text mean in the output?
It holds an element's text when that element also has attributes or child elements, so both can be kept in one object.
Are numbers converted to JSON numbers?
No. All values are kept as strings, exactly as they appear in the XML.
Why do I get an Invalid XML error?
The browser's XML parser rejected the input, usually because of an unclosed tag, an unquoted attribute or a bare ampersand. Fix the markup and convert again.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.