How to Convert XML to JSON Without Losing Attributes or Structure
XML and JSON model data differently, so a naive conversion loses attributes or flattens structure. This guide explains a predictable XML-to-JSON convention and how to apply it safely.
XML and JSON both describe structured data, but they do not think about structure the same way. XML has elements, attributes, and mixed text content; JSON only has objects, arrays, strings, numbers, booleans, and null. Converting from one to the other looks simple until you hit an attribute, a repeated tag, or an element that has both text and children - and a naive converter either drops information or produces a shape that changes unpredictably between documents.
This guide walks through a convention that handles all of that consistently, so the same input structure always produces the same kind of JSON output. You will see how attributes, text nodes, and repeated elements are represented, plus a full example. When you are ready to convert something real - a SOAP response, an RSS feed, or a legacy config file - the XML to JSON Converter does exactly this, entirely in your browser.
What XML to JSON conversion actually does
XML to JSON conversion parses an XML document into a tree, then rebuilds that same tree as nested JSON objects and arrays. Element names become object keys, nested elements become nested objects, and the text between opening and closing tags becomes a string value. Nothing about the underlying data changes - only the notation does.
The hard part is that XML carries two kinds of information JSON does not have a built-in place for: attributes (sku="A100") and mixed content (an element that has both attributes and text, or text alongside child elements). A good converter needs a fixed convention for both, so the output is predictable no matter what document you feed it. The XML to JSON Converter uses an @ prefix for attributes and a #text key for element text when it coexists with attributes or children.
Why teams still convert XML to JSON
XML has not disappeared - SOAP APIs, RSS/Atom feeds, many enterprise config formats, and older data exports still use it. But most modern tooling, from JavaScript frontends to REST-based backends, is built around JSON. Converting at the boundary lets you keep working with a legacy source while writing the rest of your code the way you already do.
- Simpler client code -
JSON.parseand dot-notation access replace XML DOM traversal or XPath. - Easier interoperability - JSON slots straight into REST APIs, JavaScript objects, and NoSQL document stores.
- Better tooling fit - JSON works natively with things like JSON Path queries, schema validators, and most logging pipelines.
- Lighter payloads - dropping closing tags and redundant markup typically shrinks the document.
How attributes, text, and nesting are mapped
The parser walks the XML tree depth-first. For each element it creates a JSON object, adds one @-prefixed key per attribute, and recurses into each child element. If a child tag name appears more than once under the same parent, those occurrences are collapsed into a JSON array instead of overwriting one another - so a single <item> stays an object, but three <item> siblings become an array of three objects.
Text content is where the two formats disagree most. If an element contains only text and no attributes or children, it converts to a plain string. If it also has attributes (or child elements alongside the text), the text is moved to a #text key so it does not collide with the attribute keys or the nested objects. Entities such as &, <, and > are unescaped back into their literal characters along the way, since JSON has no equivalent escaping requirement.
XML with attributes and nested elements
Attributes become @-prefixed keys; the price element's text moves to #text because it also carries a currency attribute.
Input
<catalog>
<product sku="A100">
<name>Wireless Mouse</name>
<price currency="USD">24.99</price>
</product>
</catalog>Output
{
"catalog": {
"product": {
"@sku": "A100",
"name": "Wireless Mouse",
"price": {
"@currency": "USD",
"#text": "24.99"
}
}
}
}Your XML never leaves your browser
SOAP responses and internal config exports often carry session tokens, customer identifiers, or account data inside their attributes and text nodes. The XML to JSON Converter parses everything client-side - there is no upload, no server round trip, and no logging - so that data stays on your machine even while you convert it.
Step-by-step: convert XML to JSON
- Open the XML to JSON Converter and paste your XML - a SOAP response, a feed, or a config file with its declaration and namespaces intact.
- Let the parser walk the document; it converts elements, attributes, and text using the @ and #text convention automatically.
- Check the array shape for any elements that repeat, since a document with one occurrence today might have several tomorrow.
- Copy or download the resulting JSON, then consume it directly in your JavaScript app, pipeline, or data store.
There is nothing to install and no account required, and the conversion keeps working offline once the page has loaded - useful when you are converting XML that should not touch a network at all.
Common use cases
- Consuming a legacy SOAP or XML API response as JSON inside a modern JavaScript or TypeScript app.
- Turning an RSS or Atom feed into JSON for a custom reader or aggregator.
- Migrating XML configuration files into a JSON-based configuration system.
- Inspecting a dense XML document by converting it to JSON and browsing it with a tree viewer.
- Normalizing third-party XML exports into JSON before loading them into a data pipeline or database.
Common problems when converting XML to JSON
Repeated elements changing shape
Because a single occurrence of a tag converts to an object while two or more convert to an array, code that reads catalog.product.name can break the moment a feed adds a second product and the value becomes catalog.product[1].name. Always write consumer code that treats a field as either-object-or-array, or normalize it to an array right after conversion.
Mixed content and text nodes
An element like <price currency="USD">24.99</price> has both an attribute and text, so its value cannot simply be the string "24.99" - that would silently discard the currency. Look for the #text key rather than assuming every element resolves to a plain string, especially when the source XML mixes plain elements with attributed ones.
No native types in XML
XML has no type system, so numbers, booleans, and dates are all just text in the source document. The converter preserves them as strings ("24.99", "true") rather than guessing at a type, because guessing wrong is worse than leaving the cast to code that knows the intended type. Convert values explicitly where your application needs a number or boolean.
Best practices for XML to JSON conversion
- Confirm the XML is well-formed before converting - unclosed tags or unescaped
&characters will fail to parse. - Treat any element that could legally repeat as an array in your consuming code, even if today's sample only has one.
- Cast strings to numbers, booleans, or dates explicitly in your own code rather than assuming the JSON already has the right type.
- Once converted, format or validate the JSON output so it is easy to review before it goes into a pipeline.
- If you need to go the other direction later, the matching JSON to XML converter reverses the same @ and #text convention.
Related tools
Convert JSON into well-formed XML with attribute and element control.
Parse any YAML config into strict, valid JSON your code can consume.
Parse CSV into a clean array of typed JSON objects, header-aware.
Pretty-print JSON with configurable indentation and instant validation.
Related guides
XML has no native concept of arrays or attributes the way JSON does. This guide explains the mapping rules and shows how to convert JSON to clean, valid XML without uploading anything.
YAML is friendlier to write, but most tools and APIs still expect JSON. Here is how the conversion really works, what gets lost, and how to do it safely in your browser.
Turning a spreadsheet export into usable JSON is trickier than a simple comma split. This guide covers headers, quoting, delimiters and type inference, plus how to convert CSV safely in your browser.
Minified JSON is unreadable and hard to debug. This guide covers how JSON formatting works, when to use it, common mistakes, and how to pretty-print JSON without uploading it anywhere.