An XML to JSON converter transforms XML documents into JSON format — essential when migrating from legacy SOAP APIs to REST, parsing XML feeds in modern JavaScript apps, or converting XML config files for JSON-based tools.
This guide covers the key differences between XML and JSON, the tricky conversion challenges (attributes, arrays, namespaces), common real-world XML sources, programmatic conversion in JavaScript and Python, and best practices for clean output.
Convert XML to JSON Instantly
Paste any XML — SOAP, RSS, sitemaps, configs — and get structured JSON. Handles attributes, namespaces, CDATA, and nested elements.
XML vs JSON: Complete Comparison
| Feature | XML | JSON |
|---|---|---|
| Verbosity | High — opening + closing tags for every element | Low — braces, brackets, minimal punctuation |
| File size | 30-50% larger for same data | Smaller and more compact |
| Parsing speed | Slower (complex, stateful grammar) | Fast (simple, context-free grammar) |
| Native JS support | Requires DOMParser or xml2js library | JSON.parse() built-in, zero dependencies |
| Attributes | Supported (<user id="1">) | No concept — everything is key-value |
| Comments | Supported (<!-- comment -->) | Not supported |
| Schema validation | XSD/DTD (very powerful, verbose) | JSON Schema (simpler, sufficient for most) |
| Namespaces | Full support (xmlns) | No concept |
| Still dominant in | SOAP APIs, RSS/Atom, SVG, Office docs, Android, enterprise | REST APIs, NoSQL databases, web apps, mobile |
New APIs are almost exclusively JSON-based. XML remains in legacy enterprise systems, government APIs, financial services (FIX protocol), and publishing (epub). If you work in enterprise IT, you will encounter XML-to-JSON conversion regularly.
The 3 Hard Problems in XML-to-JSON Conversion
1. XML Attributes Have No JSON Equivalent
XML elements can have attributes: <user id="1" name="Rahul"/>. JSON has no attribute concept. Solutions:
| Convention | JSON Output | Used By |
|---|---|---|
| @ prefix | {"@id": "1", "@name": "Rahul"} | xml2js, ToolsArena |
| _ prefix | {"_id": "1", "_name": "Rahul"} | Some libraries |
| $ prefix | {"$id": "1", "$name": "Rahul"} | Badgerfish convention |
2. Array Detection Is Ambiguous
<!-- Two items = array -->
<items><item>A</item><item>B</item></items>
→ {"items": {"item": ["A", "B"]}}
<!-- One item = string (not array!) -->
<items><item>A</item></items>
→ {"items": {"item": "A"}}
This inconsistency breaks code that expects arrays. Solutions: always force arrays for known repeated elements, or use schema-aware converters.
3. Mixed Content
<p>Hello <b>world</b> today</p>
Text mixed with child elements has no clean JSON representation. Most converters lose the text positioning or use complex nested structures.
XML and JSON are fundamentally different data models. Every converter makes trade-offs. Always validate the JSON output against your application's expectations, especially for attributes and single-element arrays.
Common XML Sources You Will Need to Convert
| Source | Format | Why Convert? |
|---|---|---|
| SOAP API responses | XML with envelope/body/namespaces | Modern frontend expects JSON |
| RSS/Atom feeds | XML with channel/item structure | Display in React/Vue apps |
| XML Sitemaps | <urlset><url> structure | SEO analysis tools need JSON |
| SVG files | XML-based vector graphics | Programmatic SVG manipulation |
| Office documents (.docx, .xlsx) | ZIP of XML files | Extract content programmatically |
| Android layouts | XML layout definitions | Cross-platform migration |
| Bank/financial APIs | ISO 20022 XML messages | Fintech applications |
| Government APIs (India) | Many use SOAP/XML | Modern app integration |
Programmatic XML to JSON Conversion
JavaScript (xml2js)
import { parseStringPromise } from 'xml2js';
const result = await parseStringPromise(xmlString, {
explicitArray: false, // Don't wrap single elements in array
attrkey: '@', // Prefix attributes with @
charkey: '#text' // Text content key
});
const json = JSON.stringify(result, null, 2);
Python (xmltodict)
import xmltodict, json
data = xmltodict.parse(xml_string)
json_string = json.dumps(data, indent=2)
# Attributes become @key: "@id": "1"
Command Line (xq)
# Install: pip install yq (includes xq)
xq . input.xml > output.json
In xml2js, set explicitArray: false to avoid every single element becoming a 1-item array. This produces cleaner JSON but introduces the inconsistency mentioned above — single items are strings, multiple items are arrays.
Handling XML Namespaces
XML namespaces (xmlns) have no JSON equivalent:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<m:GetPrice xmlns:m="http://example.com">
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
Common Approaches
- Strip prefixes — "Envelope", "Body", "GetPrice" (most common, cleanest)
- Keep prefixes — "soap:Envelope", "m:GetPrice" (preserves info, messy keys)
- Map to nested objects — {"soap": {"Envelope": ...}} (complex but accurate)
For most use cases, stripping namespace prefixes produces the cleanest and most usable JSON.
How to Use the Tool (Step by Step)
- 1
Open the Converter
Navigate to XML to JSON on ToolsArena — no signup needed.
- 2
Paste XML
Paste your XML content — SOAP responses, RSS feeds, sitemaps, or any XML document.
- 3
Convert
Click convert. Attributes are prefixed with @, repeated elements become arrays.
- 4
Review and Copy
Review the JSON structure, then copy for your API, database, or code.
Frequently Asked Questions
How are XML attributes handled in JSON?+−
XML attributes have no JSON equivalent. The converter prefixes them with @ (e.g., @id, @name) to distinguish from child elements. This is the most common convention, used by xml2js and xmltodict.
How are repeated XML elements converted?+−
Repeated sibling elements with the same name become a JSON array. A single element becomes a string or object (not a 1-element array). This inconsistency is the biggest XML-to-JSON challenge — always validate the output for your specific use case.
What happens to XML namespaces?+−
By default, namespace prefixes are preserved in key names (soap:Envelope). Many converters offer an option to strip prefixes for cleaner output. The namespace URIs are typically lost unless explicitly preserved.
Is the conversion lossless?+−
For data: mostly yes. For metadata: comments, processing instructions, DTD declarations, and namespace URIs may be lost. Mixed content (text + child elements) may lose text positioning. For pure data XML, conversion is functionally lossless.
Can I convert SOAP responses to JSON?+−
Yes. The converter handles SOAP envelopes, bodies, and namespaced elements. The JSON output will contain the SOAP structure — you typically want to extract the body content and discard the envelope wrapper in your application code.
Is my XML sent to a server?+−
No. Conversion happens entirely in your browser. Your XML — including SOAP responses with authentication tokens — is never transmitted anywhere.
Convert XML to JSON Instantly
Paste any XML — SOAP, RSS, sitemaps, configs — and get structured JSON. Handles attributes, namespaces, CDATA, and nested elements.
Open XML to JSON →Related Guides
JSON Formatter Guide
A complete developer reference for JSON syntax, common errors, formatting options, and how to validate JSON in any language or tool.
JSON to YAML Converter Guide
Convert JSON to YAML and back. Understand syntax differences, use cases for each format, and common conversion pitfalls.
CSV to JSON Converter Guide
Convert CSV files to JSON arrays or objects. Understand CSV parsing, handle commas in fields, Excel exports, type detection, and transform data for APIs and databases.