XML to JSON Converter

XML Input
1
JSON Output
1

Last updated:

Jsonic's XML to JSON converter parses any valid XML document and produces clean, formatted JSON. XML elements become JSON object keys, text content becomes string values, and repeated sibling elements automatically become JSON arrays. Numbers and booleans in text nodes are cast to their native JSON types. All parsing happens in your browser using the built-in XML parser — your data is never uploaded.

How XML elements map to JSON keys

Each XML element becomes a JSON key and its text content becomes the value. An element that holds only text collapses straight to a string, number, or boolean. The converter infers the native type when it is unambiguous — 30 becomes a number and true a boolean, while anything else stays a string.

<!-- XML -->
<person>
  <name>Alice</name>
  <age>30</age>
  <active>true</active>
</person>
// JSON
{
  "name": "Alice",
  "age": 30,
  "active": true
}

Repeated elements become arrays — and the single-item trap

When sibling elements share a tag name, they are grouped into a JSON array under that key. This is the core structural gap between XML and JSON: XML expresses "many" by repeating a tag, JSON expresses it with brackets. The trap is the single case — one <book> often yields a plain object, not a one-element array, so code that does data.book.map(...) breaks. Use a "force array" option for tags that can repeat.

<!-- XML -->
<bookstore>
  <book><title>Learning XML</title><price>39.95</price></book>
  <book><title>JSON at Work</title><price>49.99</price></book>
</bookstore>
// JSON
{
  "book": [
    { "title": "Learning XML", "price": 39.95 },
    { "title": "JSON at Work", "price": 49.99 }
  ]
}

Attributes become prefixed keys

JSON has no native attributes, so converters fold them into the object using a convention — most commonly an @ prefix on the key name. An element that carries both an attribute and text can no longer be a bare string, so its text moves into a #text key alongside the attributes.

<!-- XML -->
<user id="42" active="true">
  <name>Alice</name>
  <price currency="USD">9.99</price>
</user>
// JSON
{
  "user": {
    "@id": "42",
    "@active": "true",
    "name": "Alice",
    "price": { "@currency": "USD", "#text": "9.99" }
  }
}

Empty elements, nesting, and namespaces

Self-closing tags and empty elements map to null. Nested elements become nested objects. XML namespaces are usually kept verbatim, so a soap: prefix lands right in the key name — valid JSON, but awkward to read in JavaScript unless you strip the prefix.

<!-- XML -->
<order xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <customer><name>Alice</name></customer>
    <middleName/>
  </soap:Body>
</order>
// JSON
{
  "soap:Body": {
    "customer": { "name": "Alice" },
    "middleName": null
  }
}

Convert XML to JSON in code

When you need conversion in a script or pipeline instead of the browser tool:

# Python — xmltodict, one call, preserves attributes as @-prefixed keys
import xmltodict, json
doc = xmltodict.parse(open("data.xml").read())
print(json.dumps(doc, indent=2))
// Node.js — fast-xml-parser, with attribute + array control
import { XMLParser } from 'fast-xml-parser'
const parser = new XMLParser({ ignoreAttributes: false, isArray: (name) => name === 'book' })
console.log(JSON.stringify(parser.parse(xmlString), null, 2))
// Browser — built-in DOMParser, no dependency
const doc = new DOMParser().parseFromString(xmlString, 'text/xml')
// then walk doc.documentElement recursively to build the object

Common XML to JSON mistakes

  • Assuming a repeatable tag is always an array — single occurrences silently become objects.
  • Dropping attributes by using a converter that only reads element text.
  • Letting type inference mangle codes like 007 or ZIP codes into numbers.
  • Carrying noisy namespace prefixes (soap:, xmlns:) straight into JSON keys.

For a step-by-step walkthrough with JavaScript and Python examples, see the XML to JSON tutorial. To go the other way, use JSON to XML.

How to convert XML to JSON

  1. Paste your XML into the left panel.
  2. Click Convert.
  3. Formatted JSON output appears in the right panel.
  4. Click Copy or Download to save the result.

FAQ

How are repeated XML elements handled?

When multiple sibling elements share the same tag name, they are automatically grouped into a JSON array. For example, three <item> elements become an array under the "item" key.

Are XML attributes converted?

This converter focuses on element content and text nodes. XML attributes are not included in the JSON output.

How are empty XML elements handled?

Empty elements (self-closing tags or elements with no content) are converted to null in JSON.

Is the JSON output formatted?

Yes. The output uses 2-space indentation for readability.

Is my XML data sent to a server?

No. Conversion runs entirely in your browser using the built-in DOMParser API. Your data never leaves your device.

Can I convert JSON back to XML?

Yes — use our JSON to XML converter for the reverse direction.

What XML versions are supported?

The converter supports well-formed XML 1.0 documents, including those with an XML declaration and standard namespaces.