JSONToonPro
JSON tool

XML to JSON Converter

Convert XML to JSON online instantly. Paste any XML document and get clean, structured JSON: attributes are preserved, nested elements become nested objects, and repeated tags become arrays. Everything runs 100% client-side in your browser, so your data never leaves your device.

100% client sideInstant resultNo data sent
Input/ XML
645 chars25 lines
Output/ JSON
Invalid XML
DOMParser is not defined
0 chars0 lines

Why This Conversion Needs a Convention

XML to JSON is not a symmetric operation, and understanding why saves a lot of confusion. XML has four distinct kinds of node content: attributes, text, child elements, and comments. JSON has exactly two things: keys and values. Four into two does not go, so every converter invents a convention to encode the extra distinctions.

The most widely used convention prefixes attribute names with an @ sign and stores an element's own text under a key such as #text. Both characters are chosen precisely because they cannot appear at the start of an XML element name, so there is no risk of a collision with a real child.

Side by Side Example

An RSS style feed fragment showing attributes, repeated elements, and mixed content in one document.

XML input

<channel lang="en-GB">
<title>Field Notes</title>
<item id="a1">
<headline>Spring survey</headline>
<tag>ecology</tag>
<tag>survey</tag>
</item>
<item id="a2">
<headline>Autumn counts</headline>
<tag>ecology</tag>
</item>
</channel>

JSON output

{
"channel": {
"@lang": "en-GB",
"title": "Field Notes",
"item": [
{ "@id": "a1",
"headline": "Spring survey",
"tag": ["ecology", "survey"] },
{ "@id": "a2",
"headline": "Autumn counts",
"tag": "ecology" }
]
}
}

Look closely at the two tag values. The first item has an array because it had two tag children. The second has a bare string because it had one. Identical structure in the source produces different types in the output, and that is the single biggest hazard of this conversion.

The Single Element Ambiguity

Nothing in an XML document says whether an element is repeatable. Repeatability lives in the schema, and most documents arrive without one. A converter can only look at what is present, so one occurrence looks like a scalar and two look like a list.

Downstream code then breaks in a way that passes every test on the sample file and fails in production the first time a customer sends a document with only one item. There are three practical defences.

  1. Configure a forced array list, naming the elements that are always arrays regardless of how many occurrences appear. This is the correct fix when you know the schema.
  2. Force every repeatable looking element to an array unconditionally, which produces uglier JSON but is completely predictable.
  3. Normalise defensively in the consuming code, wrapping any non array value in a one element array before iterating.

Mapping Conventions Reference

XML constructCommon JSON representationNotes
Attribute"@name": "value"Prefix keeps attributes distinct from child elements
Text content of a leaf"element": "value"Collapsed to a plain value when there is nothing else
Text alongside children"#text": "value"Needed because the element also has real child keys
Repeated elementsArray of values or objectsOnly produced when more than one occurrence is seen
Empty elementnull or empty stringConverters differ, check which one yours emits
Self closing with attributesObject holding only @ keysNo text key at all
CommentDroppedRarely preserved, treat comments as lost
Processing instructionDroppedThe XML declaration disappears with it
CDATA sectionPlain stringThe wrapper is transport syntax, not content

Whitespace and Mixed Content

Pretty printed XML is full of whitespace text nodes: the newline and indentation between a parent tag and its first child are, strictly speaking, text content. Converters trim them, which is almost always what you want and is technically lossy for elements where whitespace is significant.

Mixed content is the genuinely awkward case. Document oriented XML lets text and elements interleave freely, and JSON has no way to express interleaving order.

Mixed content that JSON cannot represent faithfully

<p>Please read the <em>full</em> terms before you <b>sign</b>.</p>
 
Order is lost. A typical result:
{ "p": { "#text": "Please read the terms before you .",
"em": "full", "b": "sign" } }

If your XML is document oriented rather than data oriented, JSON may be the wrong target entirely. Keep the markup as a string and parse it with an HTML or XML aware renderer instead.

Namespaces and CDATA

Namespaces exist so that two vocabularies can be mixed in one document without their element names colliding. A prefix such as dc: is bound to a URI by an xmlns declaration, and the prefix itself is arbitrary: what identifies the element is the URI it resolves to.

JSON has no namespace mechanism, so converters typically flatten. Either the prefix is kept as part of the key, giving dc:creator as a literal key name, or it is stripped entirely, giving creator. Stripping is tidier and risks collisions if two namespaces use the same local name. The xmlns declarations themselves usually survive as ordinary attributes, so you may see @xmlns:dc in the output.

CDATA sections are simpler than they look. A CDATA block is just a way of writing text that contains angle brackets and ampersands without escaping each one, commonly used for embedded HTML or scripts. It carries no meaning of its own, so in JSON it becomes an ordinary string and the wrapper disappears. If the original CDATA held markup, that markup is now a string inside your JSON and still needs escaping before you insert it into a page.

Converting legacy feeds into modern payloads usually leads somewhere next. See the full converter collection for JSON to XML to go back the other way, JSON to TypeScript to type the result before you consume it, and JSON to CSV when the feed items really wanted to be a spreadsheet all along.

Frequently asked questions

4 answers
XML attributes have no direct JSON equivalent, so the converter maps them to prefixed keys on the element's object (commonly written as "@name" or a similar marker). An element like <book id="1">Title</book> becomes an object that keeps both the id attribute and the text content, so no information is lost even though JSON has no attribute concept of its own.

More JSON Tools

About the XML to JSON Converter

Legacy SOAP services, RSS feeds, sitemaps, configuration files, and enterprise integrations still speak XML, while modern applications and REST APIs expect JSON. This XML to JSON converter translates between the two instantly: paste an XML document and get JSON that mirrors the original structure, with attributes preserved, nesting kept intact, and repeated elements grouped into arrays. The parser flags malformed XML with a clear error rather than guessing, so a missing closing tag is caught immediately. Because the converter is free and runs entirely in your browser, it is safe for API responses, invoices, and any XML payload containing sensitive data.