XML to JSON Converter
Convert XML data to JSON format instantly. Free, secure, and works entirely in your browser.
Drop a file here or click to upload
Supports .xml files
Understanding XML and JSON
XML (eXtensible Markup Language) is a versatile markup language that uses a hierarchical tag-based structure to represent data. Developed by the W3C in the late 1990s, XML became the standard for data exchange across enterprise systems, web services, and document formats. It supports features like attributes, namespaces, schemas (XSD/DTD), CDATA sections, and processing instructions that give it exceptional expressiveness and validation capabilities. Dive deeper into XML with our XML learning resource.
JSON (JavaScript Object Notation) is a lightweight, text-based data format that represents structured data using key-value pairs and ordered arrays. Its simplicity and native compatibility with JavaScript have made it the preferred format for REST APIs, configuration files, and client-server communication in modern web development. JSON supports six data types: strings, numbers, booleans, null, objects, and arrays. Explore JSON in depth in our comprehensive JSON guide.
The transition from XML to JSON reflects a broader industry shift toward simpler, more compact data formats. While XML excels in document-centric use cases with its rich metadata capabilities, JSON offers faster parsing, smaller payload sizes, and a more natural fit for the JavaScript-driven web ecosystem.
Why Convert XML to JSON?
As applications modernize and embrace RESTful architectures, converting XML data to JSON becomes a frequent requirement. Here are the most common real-world scenarios:
- Parsing SOAP API responses: Many enterprise services still return data in SOAP/XML format. Converting these responses to JSON allows your JavaScript or TypeScript frontend to process the data natively without XML parsing libraries, reducing bundle size and complexity.
- Processing RSS and Atom feeds: News aggregators, podcast apps, and content platforms consume RSS/Atom feeds that are XML-based. Converting feed data to JSON makes it straightforward to render in React, Vue, or Angular components and store in JSON-native databases.
- Migrating legacy XML configurations: Applications transitioning from older frameworks (Spring XML configs, Ant build files, web.xml) to modern tooling often need to convert XML configuration files to JSON equivalents for tools like package.json, tsconfig.json, or custom config files.
- Consuming XML-based data feeds: Financial data providers, weather services, and government open data portals frequently publish data in XML format. Converting to JSON enables easy integration with modern dashboards, data visualization libraries like D3.js, and NoSQL databases like MongoDB that store data natively in JSON-like documents.
- Building API translation layers: When building middleware or API gateways that sit between legacy XML systems and modern JSON-based microservices, XML to JSON conversion is a fundamental operation for request and response transformation.
How the Conversion Works
Converting XML to JSON requires mapping XML's rich feature set to JSON's simpler data model. The converter parses the XML document tree and systematically transforms each node into its JSON equivalent. Here is how each XML construct maps to JSON:
| XML Construct | JSON Equivalent | Notes |
| Element | Object key | Tag name becomes the property name |
| Attribute | Property with @_ prefix | Distinguishes attributes from child elements |
| Text content | String value or #text property | #text used when attributes are also present |
| Repeated sibling elements | Array | Same-named siblings are grouped automatically |
| Nested elements | Nested object | Hierarchy is preserved exactly |
| CDATA section | String value | Content is extracted as plain text |
| Namespace prefix | Included in key name | e.g., soap:Body stays as-is |
| XML declaration / comments | Omitted | No JSON equivalent exists |
One important nuance is the loss of the attribute-vs-element distinction. In XML, <book isbn="123"><title>Dune</title></book> clearly separates the attribute isbn from the child element title. In JSON, both become object properties, so the @_ prefix convention is used to maintain this distinction. Similarly, when an element contains both text content and attributes, the special #text key holds the text value alongside the prefixed attribute keys.
Before and After Example
Here is a realistic example showing an XML document with attributes, nested elements, and repeated items being converted to its JSON representation:
Input (XML)
<?xml version="1.0" encoding="UTF-8"?>
<library name="City Central">
<location>
<city>Springfield</city>
<country code="US">United States</country>
</location>
<books>
<book id="1" genre="fiction">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
</book>
<book id="2" genre="science">
<title>A Brief History of Time</title>
<author>Stephen Hawking</author>
<year>1988</year>
</book>
</books>
<open>true</open>
</library>Output (JSON)
{
"library": {
"@_name": "City Central",
"location": {
"city": "Springfield",
"country": {
"@_code": "US",
"#text": "United States"
}
},
"books": {
"book": [
{
"@_id": "1",
"@_genre": "fiction",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": "1925"
},
{
"@_id": "2",
"@_genre": "science",
"title": "A Brief History of Time",
"author": "Stephen Hawking",
"year": "1988"
}
]
},
"open": "true"
}
}Notice several important conversion details in this example. The name attribute on<library> becomes @_name in JSON. The <country>element has both an attribute (code) and text content, resulting in the @_codeand #text properties. The two <book> sibling elements are automatically grouped into a JSON array. Also note that all values become strings in JSON since XML does not carry type information — 1925 and true appear as string values.
Tips and Best Practices
Understand the attribute prefix convention
All XML attributes are prefixed with @_ in the resulting JSON. When processing the converted output programmatically, filter properties that start with @_ to distinguish original attributes from child elements. This convention is consistent with popular XML-to-JSON libraries like fast-xml-parser.
Handle mixed content nodes carefully
XML allows mixed content where an element contains both text and child elements, such as<p>Click <a>here</a> now</p>. This pattern does not map cleanly to JSON. The converter extracts text into #text properties, but the interleaving of text and elements may not be perfectly preserved. Review mixed content nodes in the output carefully.
Be aware of array vs. single element ambiguity
If your XML has only one <item> element, it will be converted to a single JSON object rather than an array. If your application expects arrays, you may need to normalize the output by wrapping lone elements in arrays. This is one of the most common pitfalls when consuming XML-to-JSON output in application code.
Whitespace trimming behavior
Insignificant whitespace used for XML formatting (indentation, line breaks) is automatically trimmed during conversion. If your XML contains meaningful whitespace in text nodes (such as preformatted text), verify that it is preserved correctly in the JSON output. CDATA sections are a reliable way to ensure whitespace is kept intact.
Parse numeric and boolean values after conversion
Since XML treats all values as text, the converted JSON will contain string values like"42" and "true" rather than proper JSON numbers and booleans. If your application needs typed values, apply a post-processing step to coerce strings to their appropriate JSON types using Number(), Boolean(), or a schema-aware parser.
Validate your XML input before converting
Malformed XML with unclosed tags, mismatched nesting, or invalid characters will cause conversion errors. The converter provides error position indicators to help you locate problems. Ensure your XML is well-formed before converting — every opening tag must have a corresponding closing tag, and attribute values must be quoted.
Related Tools
Explore more conversion and learning tools to work with XML and JSON formats:
How to Convert XML to JSON
- Paste your XML data in the input area, or upload a XML file
- Click the "Convert" button
- View the converted JSON output instantly
- Copy the result or download it as a file
Features
- ✓100% client-side - your data never leaves your browser
- ✓No login or registration required
- ✓Instant conversion with real-time preview
- ✓Supports file upload and drag-and-drop
- ✓Download converted files directly
- ✓Works on mobile and desktop
Frequently Asked Questions
How does XML to JSON conversion work?
XML elements become JSON object keys, and element content becomes the values. Attributes are preserved with a special prefix (@_). Repeated elements with the same tag name are automatically grouped into JSON arrays.
Are XML attributes preserved in the JSON output?
Yes, XML attributes are converted to JSON properties with the @_ prefix. For example, <item id="1" type="book"> becomes {"item": {"@_id": "1", "@_type": "book"}}. This convention distinguishes attributes from child elements in the resulting JSON.
How are nested XML elements handled?
Nested XML elements become nested JSON objects, preserving the full hierarchical structure. For example, <person><address><city>NY</city></address></person> becomes {"person": {"address": {"city": "NY"}}}.
What XML features are supported?
The converter supports standard XML including elements, attributes, text content, CDATA sections, and deeply nested structures. XML declarations and comments are processed appropriately and do not appear in the JSON output.
How does the converter handle XML namespaces?
XML namespaces are preserved in the JSON output as part of the element or attribute name. For example, <soap:Envelope> becomes a key named "soap:Envelope" in the JSON. The namespace prefix is retained so you can identify which namespace each element belongs to.
What happens when an XML element has both text content and attributes?
When an element has both attributes and text content, the attributes are stored with the @_ prefix and the text content is stored under the #text key. For example, <price currency="USD">29.99</price> becomes {"price": {"@_currency": "USD", "#text": "29.99"}}.
How are repeated XML elements converted?
When multiple sibling elements share the same tag name, they are automatically grouped into a JSON array. For example, <items><item>A</item><item>B</item></items> becomes {"items": {"item": ["A", "B"]}}. A single element remains a standalone value unless explicitly configured otherwise.
Is whitespace in XML preserved during conversion?
By default, insignificant whitespace (indentation, line breaks between elements) is trimmed during conversion. Significant whitespace within text content is preserved. CDATA sections preserve their content exactly as written, including any whitespace.