Back to Blog

XML to JSON Conversion: A Complete Developer Guide

Mar 5, 20266 min read

XML (eXtensible Markup Language) has been the backbone of enterprise data exchange for decades. But modern REST APIs, web applications, and AI pipelines almost universally prefer JSON. Converting XML to JSON is a daily task for developers integrating legacy SOAP services, parsing RSS feeds, or migrating configuration files.

Why Convert XML to JSON?

The reasons are practical:

  • REST APIs: Most modern APIs consume and return JSON. If your data source is XML (e.g. a legacy SOAP endpoint), you must convert before sending or processing.
  • Readability: JSON is significantly more compact and easier to read. The same data structure in XML can be 2-3× larger due to opening and closing tags.
  • JavaScript compatibility: Browsers and Node.js parse JSON natively with JSON.parse(). XML requires an additional DOM parser.
  • AI & LLM pipelines: Language models tokenize every character. JSON's lighter syntax means fewer tokens and lower API costs.

How XML Structure Maps to JSON

The conversion follows predictable rules:

XML conceptJSON equivalent
ElementObject key
Attribute (e.g. id="1")Key prefixed with @ (e.g. "@id": "1")
Text content of an element"#text" key
Repeated sibling elementsArray
Nested elementsNested objects

Example

XML Input
<users>
  <user id="1">
    <name>Alice</name>
    <role>admin</role>
  </user>
  <user id="2">
    <name>Bob</name>
    <role>editor</role>
  </user>
</users>
JSON Output
{
  "users": {
    "user": [
      { "@id": "1", "name": "Alice", "role": "admin" },
      { "@id": "2", "name": "Bob",   "role": "editor" }
    ]
  }
}

Online XML to JSON Converter

The fastest way to convert XML to JSON without writing any code is our free XML to JSON Converter. Just paste your XML and get structured JSON instantly — attributes become @attr keys, repeated tags become arrays, and text nodes become #text. No sign-up required.

Convert XML to JSON in JavaScript

In Node.js, the fast-xml-parser library is the most popular and performant option:

Node.js
import { XMLParser } from 'fast-xml-parser';

const xml = `<users>
  <user id="1"><name>Alice</name></user>
  <user id="2"><name>Bob</name></user>
</users>`;

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@' });
const json = parser.parse(xml);

console.log(JSON.stringify(json, null, 2));

Convert XML to JSON in Python

The xmltodict library produces a Python dictionary that maps directly to JSON:

Python
import xmltodict, json

xml_string = """
<users>
  <user id="1"><name>Alice</name></user>
  <user id="2"><name>Bob</name></user>
</users>
"""

data = xmltodict.parse(xml_string)
print(json.dumps(data, indent=2))

Common Conversion Challenges

  • Namespaces: XML namespaces (e.g. xsi:type) produce keys like "xsi:type" in JSON. Clean these up with a replacer function if your JSON consumer doesn't expect them.
  • CDATA sections: CDATA content is treated as raw text; most parsers map it to a string value.
  • Mixed content: Elements that mix child elements and text nodes produce a #text key alongside child keys.
  • Single vs. multiple children: An element with one child may produce an object, while two children produces an array. Use parser options like isArray to force consistent types.

Validate After Converting

After converting XML to JSON, paste the output into our JSON Validator to confirm the result is structurally sound before sending it downstream.

Convert XML to JSON Instantly

No code needed. Paste any XML — SOAP responses, RSS feeds, config files — and get clean, formatted JSON in one click.