Free Converter

XML to JSON Converter

Convert XML data or files to JSON format instantly in your browser. Supports custom indentation and handles nested structures.

Select XML file

or drag and drop here

Or

About XML to JSON Conversion

XML and JSON are both data interchange formats but come from different eras of the web. XML, standardized in 1998, was designed as a self-describing markup format with attributes, namespaces, and schema validation. JSON emerged in the early 2000s as a lighter-weight format better suited to JavaScript runtimes and modern APIs. Most new web APIs speak JSON; many older systems — SOAP services, RSS feeds, configuration files, enterprise integrations — still produce XML. Converting between them is one of the most common interop tasks in modern development.

The conversion is not perfectly lossless because the two formats have different expressive power. XML has attributes (which JSON does not natively), distinguishes between text content and element names, and supports mixed content (elements containing both text and child elements). JSON's straightforward object/array model cannot directly represent these features, so converters apply conventions: attributes become @-prefixed keys, text content becomes a #text key, and so on.

This converter parses XML using the browser's built-in DOMParser and walks the resulting DOM tree to produce JSON. Elements with the same tag name as siblings are collected into arrays; attributes go into special keys; namespaces and processing instructions are preserved when present. The result is human-readable JSON that round-trips reasonably back to XML.

Why Convert XML to JSON

Most modern application code is more comfortable with JSON. JavaScript parses JSON natively, Python's json module is in the standard library, and virtually every other language has equivalent first-class JSON support. XML processing requires importing additional libraries and writing more boilerplate. Converting incoming XML to JSON before processing is often the quickest path from external XML feed to internal data structure.

Tooling also leans JSON. JSON beautifiers, validators, schema validators, query languages (jq, JSONPath), and viewers are abundant; the XML equivalents are fewer and frequently older. Working in JSON unlocks a richer ecosystem of tools.

How to Convert XML to JSON

Paste XML, get JSON.

  1. Paste or upload your XML: Paste XML text into the input area or drop a file. The XML must be well-formed; malformed XML produces a parse error rather than a guess.
  2. Convert: DOMParser builds a DOM, the converter walks each element, and JSON is generated using attribute keys (@attr) and text content keys (#text) where needed. Sibling elements with the same name become arrays.
  3. Review the structure: Inspect the JSON to confirm attributes, text, and nesting are mapped as expected. The conventions used (e.g., @ for attributes) are visible in the output.
  4. Download or copy: Save as .json or copy to clipboard.

Common Use Cases

Technical Details

DOMParser produces an XML DOM identical to what the browser uses for XHTML pages. The converter walks this DOM recursively. For each element it creates a JSON object: attributes become keys prefixed with @ (e.g., @id, @class); the element's child elements are recursed into the same structure; text content, when present alongside children, lives under a #text key.

Repeated child elements with the same tag name are collected into a JSON array. A single child of a given name appears as an object value; multiple children appear as an array of objects. This convention works well in practice but means the JSON shape depends on whether elements appear once or multiple times — a known imperfection of XML-to-JSON mapping.

Namespaces are preserved as part of element names (prefix:localName). Processing instructions and CDATA sections are converted to text content. XML declarations and DOCTYPEs are stripped from the JSON output but do not affect the data.

Best Practices

Frequently Asked Questions

How are XML attributes represented in JSON?
Attributes are converted into keys prefixed with @. For example, <book id="1"> becomes {"@id": "1"}. This convention preserves the distinction between attributes and child elements while keeping the JSON readable.
What happens to text content?
Element text becomes the value of the JSON object, or appears under a #text key when the element also has attributes or children. Mixed content (text and elements interleaved) is preserved using #text plus the children.
Are namespaces preserved?
Yes. Namespace prefixes appear as part of element names (prefix:localName). Default namespaces are also preserved via xmlns attributes converted to @xmlns keys.
What if my XML is malformed?
DOMParser produces a parse error and the conversion fails. Use an XML validator to identify the issue — most often unclosed tags, mismatched element names, or invalid characters.
Are CDATA sections handled?
Yes. CDATA content is converted to plain text in the JSON output. The CDATA wrapping itself is not preserved, but the content is.
Is my data uploaded to a server?
No. DOMParser runs in your browser; the conversion happens entirely on your device.
Will the JSON round-trip back to XML?
Yes if you use the inverse convention (the JSON to XML tool follows the same @attr and #text rules). The round trip preserves structure for typical XML; some edge cases (mixed-content order, processing instructions) may not survive perfectly.
What is the maximum input size?
Up to 50 MB. DOMParser is memory-bounded by your browser; very large XML files may slow down or fail to parse.