About This Tool
Convert any XML document to clean, properly structured JSON with this free online tool. Paste your XML, click convert, and get formatted JSON output with two-space indentation. The converter handles all standard XML features: element nesting, attributes (stored as @attributeName keys), multiple same-named child elements (automatically grouped into arrays), text content, CDATA sections, and self-closing tags. A size comparison shows the byte count for both the XML input and JSON output so you can see the difference. The JSON result includes a copy button and download option for saving the file directly. Your data stays completely private — nothing is uploaded, stored, or shared. Load the sample XML to see how a typical conversion looks before pasting your own data.
How XML to JSON Conversion Works
XML and JSON represent hierarchical data using fundamentally different syntax. XML uses opening and closing tags with optional attributes, while JSON uses key-value pairs with curly braces and square brackets. The conversion maps XML structures to their closest JSON equivalents:
- Elements: Each XML element becomes a key in the JSON object. The key name is the tag name, and the value is either a string (for text-only elements) or a nested object (for elements with children or attributes).
- Attributes: XML attributes are stored with an @ prefix. For example,
<book category="fiction">produces"@category": "fiction"in the JSON output. - Arrays: When multiple child elements share the same tag name, they are automatically grouped into a JSON array. This is one of the most common conversion challenges since XML has no native array type.
- Text content: If an element contains only text and no attributes, its value is a plain string. If it has both text and attributes (or children), the text is stored under the
#textkey. - CDATA sections:
<![CDATA[...]]>blocks are treated as plain text content, with the CDATA wrapper removed in the JSON output.
Attribute Handling and the @ Convention
XML attributes present a challenge during conversion because JSON has no built-in concept of attributes versus child elements. The most widely used convention is to prefix attribute names with the @ symbol. This approach is the de facto standard across most XML-to-JSON conversion tools in all major programming languages.
Consider this XML:
<product id="42" available="true">
<name>Widget</name>
<price currency="USD">9.99</price>
</product>The conversion produces:
{
"product": {
"@id": "42",
"@available": "true",
"name": "Widget",
"price": {
"@currency": "USD",
"#text": "9.99"
}
}
}The @-prefix convention keeps attribute data separate from child element data. Note that the price element has both an attribute and text content, so the text value is stored under #text alongside the @currency attribute.
When Arrays Are Created
XML does not distinguish between a single child element and a list of children with the same name. JSON does. This tool applies a simple rule: when a parent element contains two or more children with the same tag name, those children become a JSON array.
For example, this XML with two item children:
<list>
<item>Apple</item>
<item>Banana</item>
</list>Produces an array:
{"list": {"item": ["Apple", "Banana"]}}But a single item becomes a plain value, not a one-element array. This matches the behavior of most XML-to-JSON converters. If your application always expects an array for a particular field, you should handle the single-value case in your code by checking if the value is an array or wrapping it in one.
Common Use Cases
Developers convert XML to JSON for many reasons:
- API migration: Many legacy SOAP-based APIs return XML. When migrating to REST endpoints that consume JSON, converting response payloads is the first step.
- Configuration files: Some build tools and frameworks use XML configuration. Converting these to JSON makes them easier to process programmatically in modern projects.
- Data transformation pipelines: ETL (Extract, Transform, Load) workflows often receive XML from upstream systems and need to store the data in JSON-based databases.
- Quick inspection: JSON is generally easier to read than deeply nested XML, especially for developers accustomed to key-value formats. Converting a large XML response to JSON for quick inspection can speed up debugging.
- Frontend consumption: Modern web applications work natively with JSON. Converting XML data feeds to JSON before loading them into an app avoids the need for XML parsing in the application code.
Limitations and Edge Cases
XML-to-JSON conversion is inherently lossy because the two formats have different expressive capabilities. Some details that do not survive the conversion:
- Processing instructions: The
<?xml version="1.0"?>declaration and other processing instructions are discarded. JSON has no equivalent. - Comments: XML comments (
<!-- ... -->) are dropped. JSON does not support comments. - Namespace prefixes: Elements with namespace prefixes like
<ns:element>keep the prefix as part of the key name. The namespace URI is not resolved. - Document order: JSON objects in most environments do not guarantee key order. If the order of child elements matters, consider using arrays or a format that preserves ordering.
- Mixed content: Elements that interleave text and child elements (common in XHTML) produce a
#textkey that captures only the direct text nodes. Interleaved positioning is lost.
For round-trip fidelity (converting XML to JSON and back without data loss), you would need a specialized format like JSON-LD or BadgerFish convention, which preserves more XML metadata at the cost of a more verbose JSON structure.
Frequently Asked Questions
How are XML attributes represented in the JSON output?
<item id="5"> produces "@id": "5" in JSON. This convention separates attribute data from child element data and is used by most XML-to-JSON libraries across different programming languages.What happens when multiple elements have the same tag name?
<item> children become "item": ["value1", "value2"]. A single child element remains a plain value, not a one-element array. If your application always expects arrays, add that logic on the consumer side.