User Rating 0.0
Total Usage 0 times
Category JSON Tools
Is this tool helpful?

Your feedback helps us improve.

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.

xml to json api converter xml parser json converter xml api data transformation web development

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

{
NULL if E has no children, no attributes, no texttextContent if E is a leaf with only text/CDATA{@attributes, #text, ...children} otherwise

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 TypeJSON MappingExample XMLExample 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 sectionString value<![CDATA[raw]]>"raw"
Empty elementNULL<empty/>{"empty": null}
Namespaced elementPrefixed 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 hierarchyNested objects<a><b><c>1</c></b></a>{"a":{"b":{"c":"1"}}}
Boolean-like textString (not coerced)<flag>true</flag>{"flag": "true"}
Numeric textString (not coerced)<count>99</count>{"count": "99"}
Processing instructionIgnored<?xml version="1.0"?>(not included)
CommentsIgnored<!-- note -->(not included)
XML DeclarationIgnored<?xml encoding="UTF-8"?>(not included)

Frequently Asked Questions

The algorithm performs a pre-scan of all child nodes within each parent element. If a tag name appears more than once among siblings, all values under that tag are collected into a JSON array. This is deterministic: even if only two elements exist, they become an array. Single occurrences remain plain objects or strings.
No. Every element's attributes are preserved under an @attributes key in the resulting JSON object. If an element has both attributes and a text value, the text is stored under a #text key alongside @attributes. Self-closing elements with attributes produce an object containing only @attributes.
XML is untyped - all text content is character data. Automatic coercion to numbers risks data loss (leading zeros in IDs like "007", phone numbers, ZIP codes). The converter preserves raw string values. Consumers can parse numbers downstream with full context about whether coercion is safe.
Namespace prefixes are preserved as-is in JSON keys (e.g., becomes "dc:creator"). The converter does not resolve namespace URIs or strip prefixes. This is intentional: prefix-based keys maintain a one-to-one mapping with the source XML and avoid ambiguity when multiple namespaces define identically named elements.
The browser's native DOMParser returns a document containing a element for malformed XML. The converter detects this element and surfaces the exact parser error message in a toast notification. No partial or corrupt JSON is produced. Fix the XML source and retry.
The tool routes requests through a public CORS proxy (allorigins.win) to bypass browser same-origin restrictions. Most public XML APIs work. However, APIs requiring authentication headers, POST bodies, or those that block proxy IPs will fail. For such cases, paste the XML response directly into the input textarea.
CDATA sections (type 4 nodes) are treated identically to text nodes (type 3) - their content is extracted and concatenated with any adjacent text. The CDATA wrapper is stripped. This matches the XML specification: CDATA is a serialization convenience, not a semantic distinction.