Search tools...
Converters

XML to JSON Converter Guide: Transform XML Data to JSON (2026)

Convert XML documents and API responses to JSON. Understand attribute handling, namespace mapping, array detection, and SOAP-to-REST migration strategies.

9 min readUpdated April 8, 2026Developer, XML, JSON, APIs

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.

Free Tool

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 →

XML vs JSON: Complete Comparison

FeatureXMLJSON
VerbosityHigh — opening + closing tags for every elementLow — braces, brackets, minimal punctuation
File size30-50% larger for same dataSmaller and more compact
Parsing speedSlower (complex, stateful grammar)Fast (simple, context-free grammar)
Native JS supportRequires DOMParser or xml2js libraryJSON.parse() built-in, zero dependencies
AttributesSupported (<user id="1">)No concept — everything is key-value
CommentsSupported (<!-- comment -->)Not supported
Schema validationXSD/DTD (very powerful, verbose)JSON Schema (simpler, sufficient for most)
NamespacesFull support (xmlns)No concept
Still dominant inSOAP APIs, RSS/Atom, SVG, Office docs, Android, enterpriseREST APIs, NoSQL databases, web apps, mobile
The Industry Trend

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:

ConventionJSON OutputUsed 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.

No Perfect Mapping Exists

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

SourceFormatWhy Convert?
SOAP API responsesXML with envelope/body/namespacesModern frontend expects JSON
RSS/Atom feedsXML with channel/item structureDisplay in React/Vue apps
XML Sitemaps<urlset><url> structureSEO analysis tools need JSON
SVG filesXML-based vector graphicsProgrammatic SVG manipulation
Office documents (.docx, .xlsx)ZIP of XML filesExtract content programmatically
Android layoutsXML layout definitionsCross-platform migration
Bank/financial APIsISO 20022 XML messagesFintech applications
Government APIs (India)Many use SOAP/XMLModern 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
explicitArray: false

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. 1

    Open the Converter

    Navigate to XML to JSON on ToolsArena — no signup needed.

  2. 2

    Paste XML

    Paste your XML content — SOAP responses, RSS feeds, sitemaps, or any XML document.

  3. 3

    Convert

    Click convert. Attributes are prefixed with @, repeated elements become arrays.

  4. 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.

Free — No Signup Required

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