Converting XML to JSON means choosing a representation for information that JSON does not model directly. An XML element can have attributes, text, and repeated children. A JSON object has keys and values, but no built-in distinction between an attribute and a child element. The XML to JSON converter uses @ for attributes and #text for text that shares an element with attributes or child elements. It runs in your browser; you can try the example below in the tool.
A worked conversion
Paste this XML into the converter:
<order id="1001" status="shipped">
<customer>Acme Corp</customer>
<item sku="A1">Widget</item>
<item sku="B2">Gadget</item>
</order>
The output is:
{
"order": {
"@id": "1001",
"@status": "shipped",
"customer": "Acme Corp",
"item": [
{ "@sku": "A1", "#text": "Widget" },
{ "@sku": "B2", "#text": "Gadget" }
]
}
}
The root element becomes the outer key. id, status, and sku are XML attributes, so their JSON keys begin with @. Each item has an attribute and text, so the text appears under #text. The two sibling <item> elements become an array. The values remain strings: XML itself does not tell this converter whether 1001 should be a number or an identifier.
The mapping this tool uses
| XML input | JSON output | Detail |
|---|---|---|
<name>Ada</name> |
{"name":"Ada"} |
A text-only element becomes a string. |
<user id="42">Ada</user> |
{"user":{"@id":"42","#text":"Ada"}} |
Attributes use @; text uses #text. |
<items><item>A</item><item>B</item></items> |
{"items":{"item":["A","B"]}} |
Repeated siblings become an array. |
<empty/> |
{"empty":""} |
An empty element becomes an empty string, not JSON null. |
A single <item> becomes a string or object, while two or more become an array. If your application always needs a list, normalize that field after conversion. A schema-aware converter can make that decision from a schema; this browser tool does not infer one.
The tool parses XML with the browser's DOMParser. A malformed document produces a parser error; that checks well-formedness, not validity against an XSD or DTD. Use the XML Validator to inspect syntax separately. The W3C XML specification defines elements and attributes; RFC 8259 defines JSON's value types.
Run the same mapping in JavaScript
For a browser application, this small function follows the converter's element, attribute, text, and repeated-sibling rules:
function convertXml(xmlString) {
const doc = new DOMParser().parseFromString(xmlString, "text/xml");
if (doc.querySelector("parsererror")) throw new Error("Malformed XML");
function elementValue(element) {
const children = Array.from(element.children);
const attributes = Array.from(element.attributes);
const directText = Array.from(element.childNodes)
.filter(node => node.nodeType === 3 || node.nodeType === 4)
.map(node => node.textContent ?? "")
.join("");
if (!children.length && !attributes.length) return directText;
const result = Object.create(null);
for (const attribute of attributes) result[`@${attribute.name}`] = attribute.value;
if (directText.trim() || (!children.length && directText)) result["#text"] = directText;
for (const child of children) {
const name = child.tagName;
const value = elementValue(child);
if (Object.hasOwn(result, name)) {
result[name] = Array.isArray(result[name])
? [...result[name], value]
: [result[name], value];
} else {
result[name] = value;
}
}
return result;
}
const root = doc.documentElement;
return { [root.tagName]: elementValue(root) };
}
Pass convertXml(xmlString) to JSON.stringify(..., null, 2) if you need formatted output. The null-prototype object lets element names such as constructor and __proto__ remain ordinary keys. MDN's DOMParser documentation explains the browser parser and its error behavior.
Where the conversion is lossy
Mixed content loses order. <p>Hello <b>world</b>!</p> contains text before and after a child element. This tool joins the direct text into "#text":"Hello !" and stores "b":"world" separately. That JSON cannot reconstruct the original sequence. Use an XML-aware document model for XHTML, publishing formats, or other document-like XML.
Namespaces need review. A qualified name such as x:item remains a JSON key named x:item, and xmlns:x appears as @xmlns:x. The tool does not resolve namespace URIs. The reverse JSON to XML converter normalizes colons in generated names, so a namespace-aware document will not round-trip unchanged.
Types and empty values change. <count>42</count> becomes the string "42"; <empty/> becomes "". If your consuming API expects numbers, booleans, null, or fixed arrays, apply those rules after conversion from its own schema. Do not infer them merely from the text.
For simple element-and-attribute data, the reverse converter recognizes @ as an attribute and #text as text. Test both directions with representative records before using the output in an integration. For a broader format comparison, see YAML vs JSON vs XML.