$devtoolkit.sh/examples/convert/xml-to-json-example

Convert XML to JSON

Many legacy APIs and data exports use XML while modern applications expect JSON. Converting XML to JSON allows you to use familiar JavaScript object access patterns instead of XPath or DOM traversal. This example converts a products XML document showing how element hierarchies become nested objects, attributes become prefixed keys, and repeated elements become JSON arrays. The converter handles CDATA sections, XML namespaces, and the standard edge cases around text-only nodes and mixed content.

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

/examples/convert/xml-to-json-examplev1.0.0