XML
JSON

How to convert XML to JSON

  1. Paste your XML document or fragment into the input field — a full document with declaration, or just a root element.
  2. Click "Convert" — the formatted JSON output appears instantly with nested elements, attributes, and text content preserved.
  3. Switch to "JSON → XML" using the toggle above if you need the reverse direction.
  4. Click "Copy" or "Download .json" to use the output in your application, API, or data pipeline.
  5. If the output uses @attributes or #text keys, see the mapping guide below to understand the convention.

How the XML to JSON converter works

The converter parses the XML string using a DOM-based parser, then traverses the element tree and maps each node to its JSON equivalent. Element names become JSON object keys. Child elements become nested objects. Repeated sibling elements with the same tag name are collected into a JSON array. XML attributes are grouped under an @attributes key. Text content inside elements that also have attributes is stored under a #text key. The conversion runs entirely in your browser — no data leaves your machine.

This @attributes / #text convention is the standard approach for lossless XML-to-JSON mapping — it ensures every piece of information in the XML (including attributes) can be recovered when converting back to XML. Simple elements with only text content and no attributes are mapped to plain string values without the wrapper object.

XML → JSON conversion with attributes
// Input XML:
<?xml version="1.0"?>
<users>
  <user id="1">
    <name>Alice</name>
    <city>Kyiv</city>
  </user>
  <user id="2">
    <name>Bob</name>
    <city>Lviv</city>
  </user>
</users>

// Output JSON:
{
  "users": {
    "user": [
      { "@attributes": { "id": "1" }, "name": "Alice", "city": "Kyiv" },
      { "@attributes": { "id": "2" }, "name": "Bob",   "city": "Lviv" }
    ]
  }
}

Who uses XML to JSON conversion

XML remains the dominant format in enterprise systems, legacy APIs, and configuration files. JSON is the dominant format in modern web applications and REST APIs. Converting XML to JSON is the standard integration step between these two generations of technology.

  • SOAP API integration — consuming responses from legacy SOAP or XML-RPC web services in a JavaScript or Node.js application.
  • RSS and Atom feeds — parsing news feeds, podcast directories, or blog syndication feeds into JSON for frontend rendering.
  • Configuration file migration — converting Maven pom.xml, Spring applicationContext.xml, or Android strings.xml to JSON-based config.
  • Data migration — transforming XML exports from ERP, CRM, or legacy database systems into JSON for import into modern document stores.
  • Sitemap processing — parsing XML sitemaps to extract URLs for crawling, analysis, or SEO audit tools.
  • Office document data — extracting structured data from OOXML formats (Excel, Word) that use XML as their underlying storage format.

How XML structures map to JSON

XML and JSON represent hierarchical data differently. The table below shows exactly how each XML construct is represented in the JSON output, following the @attributes convention used by this converter.

XML constructJSON representationNotes
<tag>text</tag>"tag": "text"Simple text element → string value
<tag attr="v">text</tag>"tag": { "@attributes": {"attr":"v"}, "#text": "text" }Element with attribute and text
<parent><child>v</child></parent>"parent": { "child": "v" }Nested element → nested object
Two or more <item> siblings"item": ["val1", "val2"]Repeated tags → JSON array
<tag attr="v"/>"tag": { "@attributes": {"attr":"v"} }Self-closing element with attribute
<tag/>"tag": ""Empty self-closing element
<?xml version="1.0"?>(dropped)XML declaration is not included in output
<!-- comment -->(dropped)XML comments are stripped
<![CDATA[text]]>"tag": "text"CDATA content extracted as string

When to convert XML to JSON — and when to keep XML

Convert to JSON when:

  • Target is a JavaScript or Node.js application — JSON.parse() is native; XML requires a separate DOM parser or library.
  • Feeding a REST API or NoSQL database — modern APIs and document stores (MongoDB, Firestore) expect JSON, not XML.
  • Reducing payload size — JSON is typically 20–40% smaller than equivalent XML due to the absence of closing tags and verbose element syntax.
  • Simplifying data processing — iterating over a JSON array is simpler than traversing an XML node list in most languages.
  • Dashboard or chart data — frontend visualization libraries (Chart.js, D3, Recharts) accept JSON arrays directly.

Keep XML when:

  • Target system requires XML — SOAP services, enterprise message buses (MQ, ESB), and some legacy APIs only accept XML.
  • Document markup is needed — XML supports mixed content (text interleaved with elements) that JSON cannot represent.
  • Namespaces and schemas matter — XML namespaces and XSD validation have no JSON equivalent.
  • Transformation pipelines use XSLT — XSLT processing requires XML input; JSON cannot be processed by XSLT directly.
  • Comments and processing instructions must be preserved — XML comments and processing instructions are dropped during JSON conversion.

Edge cases: namespaces, mixed content, and CDATA

XML has several features that do not map cleanly to JSON. Understanding how the converter handles them prevents surprises in the output:

  • XML namespaces — namespace prefixes (ns:element) are preserved as-is in the JSON key name. The xmlns declarations are included as @attributes. Namespace-aware processing is not performed — the prefix is treated as part of the key string.
  • CDATA sections — <![CDATA[raw text]]> content is extracted and treated as plain text. The CDATA delimiters are stripped; the content becomes a regular JSON string value.
  • Mixed content — elements containing both text nodes and child elements (common in HTML-like markup) are handled by combining the text into #text and child elements as sibling keys. Complex mixed content may not round-trip perfectly.
  • Processing instructions — <?php ... ?>, <?xml-stylesheet ?>, and other processing instructions are dropped from the JSON output.
  • XML comments — <!-- ... --> comments are stripped and do not appear in the JSON output.
Namespace handling in JSON output
// Input XML with namespace:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetUser xmlns="http://example.com/api">
      <UserId>42</UserId>
    </GetUser>
  </soap:Body>
</soap:Envelope>

// Output JSON (namespaces preserved as key prefixes):
{
  "soap:Envelope": {
    "@attributes": { "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/" },
    "soap:Body": {
      "GetUser": {
        "@attributes": { "xmlns": "http://example.com/api" },
        "UserId": "42"
      }
    }
  }
}

Frequently Asked Questions

What does @attributes mean in the JSON output?
@attributes is the key used to group XML element attributes in the JSON representation. For example, <user id="1" role="admin"> becomes { "@attributes": { "id": "1", "role": "admin" }, ... }. This convention is standard for lossless XML-to-JSON mapping and allows round-tripping back to XML.
What does #text mean in the JSON output?
#text is the key used for the text content of an XML element that also has attributes. For example, <price currency="USD">9.99</price> becomes { "@attributes": { "currency": "USD" }, "#text": "9.99" }. When an element has only text content and no attributes, the text is mapped directly as a string value without the #text wrapper.
Why are some XML elements converted to arrays?
When two or more sibling elements share the same tag name, the converter groups them into a JSON array. For example, three <item> elements under <list> become "item": ["val1", "val2", "val3"]. This is the standard behavior — XML allows repeated siblings but JSON objects cannot have duplicate keys.
What happens to the XML declaration (<?xml version="1.0"?>)?
The XML declaration is dropped from the JSON output. It is metadata about the XML document format (version and encoding) and has no meaningful JSON equivalent.
Are XML comments preserved in the JSON output?
No. XML comments (<!-- ... -->) are stripped during conversion. If you need to preserve comments, keep the original XML file alongside the JSON output.
How are CDATA sections handled?
CDATA sections (<![CDATA[raw text]]>) are treated as plain text. The CDATA delimiters are stripped and the content is output as a regular JSON string value.
Are XML namespaces supported?
Namespace prefixes are preserved as-is in JSON key names (ns:element becomes "ns:element"). The xmlns declarations are included as @attributes entries. Full namespace-aware processing (resolving prefix URIs) is not performed — the prefix is treated as part of the key string.
Can I convert an XML fragment without a root element?
No. XML requires exactly one root element. If you paste a fragment with multiple root-level elements, the parser will throw an error. Wrap the fragment in a temporary root element: <root>...your content...</root> before converting.
Are all values in the JSON output strings?
Yes — XML element content and attribute values are always text, so all values in the JSON output are strings. Numbers, booleans, and null are not inferred. If you need typed values, post-process the JSON after conversion.
What is the maximum XML file size the converter handles?
There is no server-side limit — the conversion runs entirely in your browser. Practical limits are set by your browser's memory and the DOM parser's capacity. XML files up to several megabytes convert without issue on modern devices.
Is any data sent to a server during conversion?
No. The entire conversion runs in your browser using the built-in DOMParser API and JavaScript. No data is transmitted over the network — there are no privacy concerns and no file size limits.
Can I convert the JSON back to XML?
Yes — switch to "JSON → XML" using the toggle above the tool. The round-trip works correctly if the JSON follows the @attributes and #text conventions produced by this converter.