Convert XML to JSON (and back) instantly with the free XML Formatter — paste your XML and click XML→JSON.
Why Convert XML to JSON?
JSON has become the dominant format for data exchange in web APIs, partly because JavaScript can parse it natively and its syntax is less verbose than XML. When you're working with a legacy SOAP service, an RSS feed, or any XML-based system from a JavaScript or Python application, converting to JSON lets you work with the data using familiar dictionary/object access patterns.
The challenge is that XML and JSON have different data models — XML supports attributes, mixed content, comments, and processing instructions that have no direct JSON equivalent. Several conventions have been proposed to bridge the gap.
The Badgerfish Convention
Badgerfish is one of the most widely-referenced XML-to-JSON mapping conventions. It prioritizes predictability and round-trip fidelity over brevity. The rules are consistent and mechanical, making it easy to implement and reason about.
Rule 1: Element names become JSON keys
XML: <user>Alice</user>
JSON: { "user": "Alice" }
Rule 2: Attributes are prefixed with @
Every XML attribute becomes a JSON key prefixed with @, collected alongside child elements in the same object:
XML: <user id="42" role="admin">Alice</user>
JSON: {
"user": {
"@id": "42",
"@role": "admin",
"#text": "Alice"
}
}
Note that when an element has both attributes and text content, the text is stored under the special key #text.
Rule 3: Simple text content becomes the value directly
When an element contains only text and no attributes, the text is the value:
XML: <name>Alice</name>
JSON: { "name": "Alice" }
Rule 4: Repeated sibling elements become a JSON array
When the same tag name appears multiple times as siblings, they are collected into an array:
XML:
<items>
<item>first</item>
<item>second</item>
<item>third</item>
</items>
JSON:
{
"items": {
"item": ["first", "second", "third"]
}
}
Rule 5: Mixed content uses #text
Mixed content — elements that contain both text and child elements — is stored with text fragments under #text. Multiple text fragments are concatenated:
XML: <p>Hello <em>world</em> today</p>
JSON: {
"p": {
"#text": "Hello today",
"em": "world"
}
}
A Full Example
XML:
<user id="42">
<name>Alice</name>
<email>alice@example.com</email>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
</user>
JSON:
{
"user": {
"@id": "42",
"name": "Alice",
"email": "alice@example.com",
"roles": {
"role": ["admin", "editor"]
}
}
}
Round-Trip Limitations
Most XML structures survive the XML→JSON→XML round-trip faithfully. However, a few constructs cannot be recovered:
- Mixed-content ordering: text fragments interleaved with child elements are concatenated into a single
#textstring. The original position of each text node relative to child elements is lost when converting back to XML. - Comments: XML comments (
<!-- ... -->) are discarded during conversion — there is no JSON equivalent. - Processing instructions:
<?xml-stylesheet href="..."?>and similar instructions are not preserved. - Namespace prefixes: namespace declarations (
xmlns:ns="...") are treated as attributes with the@prefix, which may conflict with real attributes of the same name in edge cases.
For most API data, configuration, and feed XML — where none of these edge cases apply — the round-trip is lossless.
JSON→XML Conversion
The XML Formatter also converts in the reverse direction: paste a Badgerfish-convention JSON object and click JSON→XML to produce formatted XML. The same rules apply in reverse — @-prefixed keys become attributes, arrays become repeated sibling elements, and #text becomes the element's text content.