HomeFormattersDataXML to JSON Converter

XML to JSON Converter

Data

Convert XML to JSON instantly — paste your XML and get a clean, indented JSON representation. Handles attributes and text nodes. Runs entirely in your browser.

What is a XML→JSON?

An XML to JSON Converter transforms XML markup into a JSON representation of the same data. XML and JSON are both formats for structured data, but JSON has become the dominant format for web APIs, JavaScript applications, and modern NoSQL databases, while XML remains common in enterprise integrations, SOAP services, and legacy systems.

Converting XML to JSON lets you work with XML-source data using the tools and libraries that consume JSON — parsing it with JSON.parse, querying it with jq, inserting it into MongoDB, or passing it to a REST API endpoint.

The converter maps XML elements to JSON objects, text content to string values, and attributes to prefixed keys (e.g. @category by default). When the same element tag appears multiple times under a parent, the converter groups those elements into a JSON array automatically. Leaf elements with no child elements and no attributes are reduced to plain string values rather than {"#text": "..."} objects, keeping the output as clean as possible.

The conversion runs entirely in your browser using the native DOMParser — nothing is uploaded. For formatting XML while keeping it as XML, use the XML Formatter.


How to use this XML→JSON calculator

  1. Paste your XML into the XML input field. The default shows a bookstore example.
  2. Choose a JSON Indent size — 2 spaces produces readable output for most uses.
  3. Choose an Attribute prefix@ is the common convention; choose _ or none if your downstream consumer prefers.
  4. The JSON Output appears instantly.
  5. Click Copy to copy the JSON to your clipboard.
  6. Paste into your application, database import tool, or the JSON to CSV Converter for further processing.

Formula & Methodology

The converter uses the browser's native DOMParser with MIME type application/xml to parse the input into a DOM tree, then traverses the tree recursively.

Mapping rules:
- Element with only text content and no attributes → plain string value
- Element with attributes → object where attribute keys are prefixed (e.g. @name)
- Element with both attributes and text → object with attribute keys plus a #text key
- Element with child elements → object with child tags as keys
- Repeated child tags → grouped into an array under that tag name
- Single child tag → plain object (not a one-element array)

Example:
xml <book category="fiction"><title>Gatsby</title></book> json {"book": {"@category": "fiction", "title": "Gatsby"}} 
Frequently Asked Questions
What XML structures does this converter support?
The converter handles standard XML documents with elements, attributes, text content, and nested structures. It supports both complete documents with an XML declaration and bare fragments. CDATA sections and processing instructions are not included in the JSON output — only element content and attributes are converted.
How are XML attributes represented in the JSON?
Attributes are included as keys with a configurable prefix. With the default '@' prefix, an element <book category='fiction'> produces a JSON key '@category'. You can switch to an underscore prefix or no prefix at all using the Attribute Prefix option.
How are repeated child elements handled?
When the same tag appears more than once under a parent, the converter groups those elements into a JSON array. For example, two <book> elements inside <bookstore> produce a 'book' key whose value is an array. A single <book> produces a plain object (not a one-element array).
What does the '#text' key mean?
When an element has both child elements and direct text content, the text is captured under the '#text' key to avoid collision with element-child keys. For pure text-content elements (no child elements, no attributes), the value is returned as a plain string rather than an object.
Is the conversion reversible?
Converting XML to JSON is lossy in some edge cases — XML comments, processing instructions, and namespace prefixes are not preserved. For round-tripping data reliably, use a schema-aware library in your application code rather than this browser tool.
Is my XML data uploaded anywhere?
No. The conversion runs entirely in your browser using the browser's native DOMParser. Your XML never leaves your device.
Can I use this for SOAP or RSS feeds?
Yes — SOAP envelopes and RSS/Atom feeds are standard XML and will be converted. The resulting JSON will mirror the XML structure, including any namespace prefixes in the element tag names.
What indent size should I choose?
2 spaces produces compact output that is easy to read. 4 spaces is more spacious and preferred in Python-style projects. Minified removes all whitespace — use it when passing the JSON to another tool programmatically and readability is not needed.
What is the difference between XML to JSON and the XML Formatter?
The [XML Formatter](/xml-formatter/) pretty-prints XML, keeping it as XML with consistent indentation. This tool converts XML into a different format (JSON), which is more suitable for JavaScript applications, REST APIs, and NoSQL databases.
Can I then convert the JSON to CSV?
If the resulting JSON is an array of objects (which often happens after unwrapping one level of nesting), you can paste it into the [JSON to CSV Converter](/json-to-csv-formatter/) to produce a spreadsheet-ready CSV.
What happens if the XML is invalid?
The converter uses the browser's native XML parser, which returns a parsererror element for invalid XML. The tool detects this and shows the parser's error message as output — for example 'XML parse error: mismatched tag' — so you can identify and fix the issue.