XML API to JSON API Converter
Convert XML API responses to clean JSON format. Fetch XML from any API URL or paste raw XML for instant, accurate JSON conversion.
About
XML remains the default response format for thousands of legacy APIs - SOAP services, RSS feeds, ATOM endpoints, government data portals, and broadcast metadata systems like TheTVDB. Consuming XML in modern JavaScript applications requires parsing, namespace handling, attribute extraction, and repeated-element detection before the data becomes usable. Errors in this conversion - dropped attributes, collapsed arrays, mishandled CDATA - propagate silently and corrupt downstream logic. This tool performs a complete, recursive DOM-tree walk using the browser's native DOMParser, converting every node type (elements, attributes via @attributes, text, CDATA, mixed content) into a deterministic JSON structure. It auto-detects repeated sibling elements and promotes them to arrays. Limitations: the converter assumes well-formed XML; namespace prefixes are preserved as-is rather than resolved to URIs.
Formulas
The conversion follows a recursive DOM traversal algorithm. For each XML element node E, the converter builds a JSON object J according to these rules:
convert(E) → J
For sibling detection: if element name tag appears n > 1 times among siblings, the value at key tag becomes an array. The algorithm counts tag name occurrences in a single pre-scan pass with O(k) complexity where k is the number of child nodes. Total conversion complexity is O(N) where N is the total node count in the XML document.
For remote URL fetching, requests route through a public CORS proxy: fetch(proxyBase + encodeURIComponent(url)). The response text is then parsed identically to pasted XML.
Reference Data
| XML Node Type | JSON Mapping | Example XML | Example JSON Output |
|---|---|---|---|
| Element (single child) | Object key | <name>Foo</name> | {"name": "Foo"} |
| Element (repeated siblings) | Array | <item>A</item><item>B</item> | {"item": ["A", "B"]} |
| Attribute | @attributes object | <el id="1"> | {"@attributes": {"id": "1"}} |
| Text node (leaf) | String value | <v>42</v> | {"v": "42"} |
| Text node (with attrs) | #text key | <v unit="m">42</v> | {"@attributes":{"unit":"m"}, "#text":"42"} |
| CDATA section | String value | <![CDATA[raw]]> | "raw" |
| Empty element | NULL | <empty/> | {"empty": null} |
| Namespaced element | Prefixed key | <ns:el>X</ns:el> | {"ns:el": "X"} |
| Mixed content (text + children) | #text + child keys | <p>Hi <b>world</b></p> | {"#text": "Hi ", "b": "world"} |
| Nested hierarchy | Nested objects | <a><b><c>1</c></b></a> | {"a":{"b":{"c":"1"}}} |
| Boolean-like text | String (not coerced) | <flag>true</flag> | {"flag": "true"} |
| Numeric text | String (not coerced) | <count>99</count> | {"count": "99"} |
| Processing instruction | Ignored | <?xml version="1.0"?> | (not included) |
| Comments | Ignored | <!-- note --> | (not included) |
| XML Declaration | Ignored | <?xml encoding="UTF-8"?> | (not included) |