What is an XML to JSON converter?
XML and JSON describe the same shape of data — a tree of named values — with different grammars and different assumptions. XML has attributes as well as elements, mixed content, comments and processing instructions; JSON has objects, arrays and typed scalars, and none of the rest. Converting between them is therefore never a purely mechanical translation: someone has to decide where the attributes live, when an array appears, and what happens to the text. This converter makes those decisions explicitly: the mapping rules are written out in full below, the conversion follows them exactly, and anything that cannot be represented is reported rather than quietly dropped.
Before anything is converted, the document is checked with the same strict well-formedness checker as this site's XML Formatter: malformed XML gets a line and column for the first problem, never a best-guess conversion.
How to use this tool
- Paste your XML into the box above (or press Load sample to see a worked example). You can also upload an .xml file or drop one onto the box — the file is read in your browser and never sent anywhere.
- Choose the JSON indent — 2 spaces is the common default; 4 spaces and tabs are one click away.
- Press Convert to JSON. If the document contains constructs with no JSON counterpart (comments, processing instructions), the message line tells you how many were set aside — nothing vanishes in silence.
- Use Copy result or Download result to take the JSON with you. Your input above is never modified — the result is a separate copy.
The mapping rules, in full
- The document becomes a JSON object with one key: the root element's name.
- An element with no attributes and no child elements becomes its text, trimmed of surrounding whitespace. An empty element becomes
"". - Attributes collect under
"@attributes", keyed by attribute name, values exactly as written with XML entities decoded (&becomes&), as any XML parser would present them. - Child elements are keyed by tag name. Repeated siblings with the same name become an array, in document order; a child that appears once stays a plain value — never a one-item array.
- An element that has attributes or child elements and text of its own keeps that text under
"#text". Text split by child elements is concatenated in document order. Whitespace-only text between child elements is layout, not data, and is not kept. - CDATA content is text: it lands wherever the element's text lands, characters exactly as authored.
- Comments, processing instructions, the XML declaration and doctypes have no JSON counterpart. They are counted and reported in the message line when present — never converted, never silently dropped.
- All values stay strings.
"42"does not become the number 42,"true"does not become a boolean, and"007"keeps its zeros — XML carries no types, and guessing them would change data.
Examples
This document:
<order id="1042">
<customer>Ada</customer>
<item sku="A-1">2</item>
<item sku="B-7">1</item>
<total currency="EUR">59.90</total>
</order> converts, by the rules above, into:
{
"order": {
"@attributes": { "id": "1042" },
"customer": "Ada",
"item": [
{ "@attributes": { "sku": "A-1" }, "#text": "2" },
{ "@attributes": { "sku": "B-7" }, "#text": "1" }
],
"total": { "@attributes": { "currency": "EUR" }, "#text": "59.90" }
}
}
Note what the rules did, visibly: the two <item>
siblings became an array while <customer> —
appearing once — did not; every attribute landed under
@attributes; and the prices stayed strings, zeros
intact.
Going the other way
If you have JSON and need XML, this site's JSON to XML Converter does the reverse trip with its own documented rules. Bear in mind that a round trip is not guaranteed to be the identity: XML has constructs JSON cannot hold (and this page tells you when it sets them aside), and JSON has types XML cannot express. Convert in the direction your consumer needs, and treat the output as the document of record.