XML to JSON Converter
Paste XML — get formatted JSON output instantly. Nothing leaves your browser.
How to convert XML to JSON
- Paste your XML document or fragment into the input field — a full document with declaration, or just a root element.
- Click "Convert" — the formatted JSON output appears instantly with nested elements, attributes, and text content preserved.
- Switch to "JSON → XML" using the toggle above if you need the reverse direction.
- Click "Copy" or "Download .json" to use the output in your application, API, or data pipeline.
- 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.
// 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 construct | JSON representation | Notes |
|---|---|---|
| <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.
// 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"
}
}
}
}