XML to JSON Conversion: Complete Guide with Examples
The XML to JSON Converter transforms XML documents into JSON format instantly in your browser. Whether you're migrating a legacy system that produces XML to a modern API that consumes JSON, or just need to read XML data more easily, this tool handles the conversion without any server upload.
Ready to convert? Paste your XML and get JSON instantly.
Open XML to JSON ↗XML vs JSON: Key Differences
Both XML and JSON are text-based formats for representing structured data, but they have very different designs. XML was created in 1998 for document markup and uses angle-bracket tags, attributes, and nesting. JSON emerged from JavaScript in the early 2000s and represents data as key-value pairs, arrays, and nested objects.
JSON has become the dominant format for APIs and web services because it's more compact, easier to read, and maps directly to data structures in most programming languages. XML still dominates in enterprise systems, configuration files (Maven, Spring, Android), document formats (DOCX, SVG), and SOAP web services.
| Feature | XML | JSON |
|---|---|---|
| Syntax | Tags: <name>value</name> | Key-value: {"name":"value"} |
| Verbosity | High — repeated tag names | Compact |
| Attributes | Yes — metadata on elements | No — all data is values |
| Arrays | Repeated sibling elements | Native array syntax [] |
| Comments | Yes — <!-- comment --> | No |
How to Use the Tool
Paste well-formed XML into the input panel. The XML must have a single root element. The converter handles nested elements, attributes, text content, CDATA sections, and repeated sibling elements (which become JSON arrays).
The tool parses the XML and outputs equivalent JSON. Conversion happens client-side — your data never leaves your browser.
Use the Copy button to copy to clipboard, or download the result as a .json file.
How XML Maps to JSON
Element with text content
<name>Alice</name> → "name": "Alice"
Nested elements
<person>
<name>Alice</name>
<age>30</age>
</person>
→
{"person": {"name": "Alice", "age": "30"}}
Repeated elements become arrays
<items>
<item>Apple</item>
<item>Banana</item>
</items>
→
{"items": {"item": ["Apple", "Banana"]}}
Attributes
XML attributes are typically mapped to JSON keys prefixed with @ or _, depending on the conversion library. Our tool maps them to @attribute_name keys alongside the element's text content.
<price currency="USD">9.99</price>
→
{"price": {"@currency": "USD", "#text": "9.99"}}
Worked Examples
Example 1 — Product catalog entry
<product id="P001">
<name>Wireless Headphones</name>
<price currency="USD">79.99</price>
<tags>
<tag>audio</tag>
<tag>wireless</tag>
</tags>
</product>
{
"product": {
"@id": "P001",
"name": "Wireless Headphones",
"price": { "@currency": "USD", "#text": "79.99" },
"tags": { "tag": ["audio", "wireless"] }
}
}
Handling XML Attributes
Attributes are the trickiest part of XML-to-JSON conversion because JSON has no direct equivalent. Common conventions include prefixing attribute keys with @, storing them in a separate _attributes object, or flattening them directly into the parent object. Check which convention your target system expects before converting.
Watch out for: XML namespaces (xmlns: declarations) — these often survive conversion as attribute keys and may need to be stripped if your JSON consumer doesn't expect them.
Tips and Edge Cases
- All values are strings — XML has no native number or boolean type, so all converted values are strings. If your target system needs numbers, convert them after parsing.
- Single vs multiple children — A single child element becomes an object; two or more same-named siblings become an array. This inconsistency is a known pain point — use a library like
xml2jswithexplicitArray: truein production code to force arrays always. - CDATA sections — Text inside
<![CDATA[...]]>is preserved as a string value. - Empty elements —
<tag/>and<tag></tag>both convert tonullor an empty string depending on the converter.
