Convert XML to JSON

XML-to-JSON conversion is a practical necessity when integrating with enterprise systems, financial APIs, SOAP web services, and RSS/Atom feeds that predate the JSON era. Modern JavaScript applications, mobile apps, and REST APIs all work with JSON, but a significant portion of business-critical systems — banks, ERPs, healthcare systems, government APIs — still communicate exclusively in XML. The converter bridges this gap without requiring you to learn XPath or DOM manipulation. This example converts a product catalog XML document with two product elements, each containing child elements and XML attributes. The conversion demonstrates several important mapping decisions: the catalog root element becomes the top-level JSON object key. Each product element with its attributes and child elements becomes a nested JSON object. The id and category XML attributes (which in XML are written inside the opening tag) become JSON properties alongside the element children. Attribute vs element naming conventions: different XML-to-JSON converters use different conventions for distinguishing attributes from elements in the JSON output. Some use an @ prefix (@id, @category), some use an _attributes object, and some simply mix attributes and child elements as peers at the same level. When writing code that consumes converted XML, check which convention your converter uses so you can access the right property names. Repeated elements becoming arrays: when the same element name appears multiple times as siblings (like multiple product elements or multiple tag elements), the converter groups them into a JSON array. This is intuitive but can cause problems when an element sometimes appears once and sometimes multiple times — a single occurrence gives you an object, multiple occurrences give you an array. Robust XML parsing code must handle both cases, or you use a converter that always wraps in arrays for known-repeated elements. CDATA sections: XML allows CDATA sections (<![CDATA[content]]>) for embedding characters that would otherwise need escaping (< > &). The converter strips the CDATA wrapper and treats the content as plain text, which is the correct behavior — the consumer sees the actual content, not the XML encoding syntax. XML namespaces: this example uses no namespaces, but many enterprise XML formats use extensive namespacing (xmlns:soap, xmlns:wsse). Namespace prefixes typically appear verbatim in JSON key names (soap:Header becomes "soap:Header"), which may require additional cleanup depending on how your application accesses the data. Real-world scenarios: parsing a Stripe webhook that arrives as XML (some legacy integrations), converting an RSS feed to JSON for a JavaScript news reader, processing a USPS API response that only speaks XML, or migrating data from an XML-based CMS to a JSON-based headless CMS. Tips: for production XML parsing of complex documents, consider a dedicated library (xml2js in Node.js, xmltodict in Python, JAXB in Java) rather than a generic converter, as these libraries let you configure the exact mapping behavior for attributes, arrays, and namespaces.

Example
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <product id="p001" category="electronics">
    <name>Wireless Keyboard</name>
    <price currency="USD">49.99</price>
    <stock>142</stock>
    <tags>
      <tag>wireless</tag>
      <tag>keyboard</tag>
      <tag>bluetooth</tag>
    </tags>
  </product>
  <product id="p002" category="accessories">
    <name>USB-C Hub</name>
    <price currency="USD">29.99</price>
    <stock>87</stock>
  </product>
</catalog>
[ open in XML to JSON → ]

FAQ

How are XML attributes represented in JSON?
XML attributes are typically converted to JSON keys prefixed with @ or placed in an _attributes object, depending on the converter. The resulting JSON key name convention varies between conversion libraries.
What happens to XML namespaces during conversion?
Namespace prefixes are usually retained as part of the JSON key name (e.g., ns:element becomes "ns:element"). Some converters strip namespaces — check your output if namespace-qualified element names are important.
Can all XML documents be converted to JSON?
Most document XML converts cleanly. However, XML features like processing instructions, comments, and mixed content (elements with both text and child elements) require special handling that varies by tool.

Related Examples