Skip to main content

XML to JSON Converter — Attributes, Arrays and CDATA Handled

Paste XML and get readable JSON: attributes land in @attributes, repeated elements become arrays and indentation whitespace is ignored. The conversion runs entirely in your browser, so nothing is uploaded.

Features

  • Convert XML to JSON in one click or while you type
  • Attributes kept in an @attributes object
  • Repeated elements become JSON arrays
  • Handles CDATA, entities and namespace prefixes
  • Indent with 2 or 4 spaces, tabs or compact output
  • Runs entirely in your browser, nothing is uploaded

When You Need XML as JSON

XML is still everywhere interfaces are old or formal: SOAP web services, RSS and Atom feeds, XML sitemaps, Maven or Android config files and exports from ERP, accounting or banking software. Modern code, on the other hand, expects JSON: JavaScript works with it natively, REST APIs speak it and document databases store it.

Converting an XML response to JSON lets you see its structure at a glance, build test fixtures or check which fields a legacy API really returns before you write a parser. Paste the XML, convert it, then copy the result or download it as converted.json.

How Elements, Attributes and Text Are Mapped

XML has concepts that JSON lacks, so every converter needs conventions. This one uses these:

  • Root element: its name is dropped; its content becomes the top-level JSON object.
  • Attributes go into an @attributes object under their exact names.
  • Text: an element that contains only text becomes a string. If it also has attributes, the text moves to #text.
  • Repeated elements with the same name become an array; a single element stays an object.
  • Whitespace used for indentation is ignored, and text is trimmed at both ends.
  • CDATA is treated as normal text, entities such as & are decoded, and comments and processing instructions are dropped.
  • Namespaces: prefixes stay part of the key (soap:Body), and xmlns declarations appear under @attributes.

The JSON can be indented with 2 or 4 spaces or tabs, or written compactly on one line. The root and item name fields only matter for the opposite direction, JSON → XML.

Example: A Small Book Catalog

Two book elements with attributes and a CDATA section:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="b1">
    <title>XML Basics</title>
    <price currency="EUR">19.90</price>
  </book>
  <book id="b2">
    <title><![CDATA[Tips & Tricks]]></title>
    <price>9.50</price>
  </book>
</catalog>

Result with 2-space indentation:

{
  "book": [
    {
      "@attributes": {
        "id": "b1"
      },
      "title": "XML Basics",
      "price": {
        "@attributes": {
          "currency": "EUR"
        },
        "#text": "19.90"
      }
    },
    {
      "@attributes": {
        "id": "b2"
      },
      "title": "Tips & Tricks",
      "price": "9.50"
    }
  ]
}

The repeated book became an array, id landed in @attributes, and the price with a currency attribute keeps its value in #text. The CDATA content is now plain text.

Common Pitfalls

  • One or many: a feed with a single item yields an object, with two it yields an array. Code that reads the JSON should handle both cases.
  • Everything is a string: <age>30</age> becomes "30", not the number 30, and true stays a string too. Without a schema, XML has no data types.
  • Empty elements such as <note/> become an empty object {}.
  • Mixed content: in <p>Hello <b>world</b></p> the text is collected into a single #text next to b, and its position is lost. For HTML-like documents, JSON is the wrong target.
  • Invalid XML: the parser is strict. Unclosed tags, a bare &, HTML entities such as &nbsp; or two root elements produce the message “Invalid XML format” without a line number. If you cannot spot the problem, run the XML through a validator first.

Useful Resources

Wikipedia: XML Overview of XML, its syntax and where it is used
Wikipedia: JSON Overview of the JSON data format and its data types
MDN: DOMParser The browser API that parses XML strings into a document
W3C: Extensible Markup Language (XML) 1.0 The official XML specification, including CDATA sections and entities
W3C: Namespaces in XML 1.0 How namespace prefixes and xmlns declarations work