The conversion finished, the JSON looks valid, and yet nothing downstream works. The id field your code expects is missing. The list you loop over turns out to be a single object. A price shows up as a small object instead of a number-like string. None of these are bugs in the converter. They are the predictable result of four mapping rules that XML to JSON conversion follows, and each one catches people out at first.
This guide walks through the four traps one by one, shows a small record that falls into two of them at once, and explains how to read the output of the free XML to JSON Converter on Ovatly with the rules in mind.
Contents
- Quick answer
- Trap 1: attributes become @ prefixed keys
- Trap 2: a single element is not an array
- Trap 3: text beside attributes lands in #text
- Trap 4: namespaces and comments are not carried over
- How to convert XML to JSON, step by step
- Worked example: one record, two traps
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
XML and JSON model data differently, so conversion follows fixed mapping rules rather than copying shapes across. In the XML to JSON Converter, attributes become keys prefixed with @, repeated elements become arrays while a single occurrence stays a single value, text that sits beside attributes or child elements is stored under the #text key, and namespaces, comments and processing instructions are not carried over. Once you expect those four rules, the output reads exactly as designed.
Trap 1: attributes become @ prefixed keys
XML gives an element two places to store data: attributes on the opening tag and child elements inside it. JSON objects have only one place, the key. If an attribute and a child element share a name, writing both as plain keys would make one overwrite the other.
The converter avoids that collision by prefixing attribute keys with @. An attribute written as id="bk-101" on a <book> element becomes the key @id in the JSON, while a child <id> element would become plain id.
The trap is purely in the reading. Code that looks for id finds nothing, because the value lives under @id. When output seems to be missing every attribute, check for the prefixed keys first.
Trap 2: a single element is not an array
When an element repeats, the converter groups the repetitions into an array. Three <item> elements become an array of three values, in document order.
The trap is the single case. One <item> element becomes a single value, not an array containing one value. The same field is an array in a large document and a plain object or string in a small one, and code written against the large document breaks on the small one.
There is no setting that forces arrays, because the shape honestly depends on the document. The sound habit is to check what you received: if the value is an array, loop over it; if it is a single value, treat it as a list of one.
Trap 3: text beside attributes lands in #text
An element that contains only text converts cleanly: <title>Example</title> becomes the string "Example". The trouble starts when an element carries both text and something else, usually an attribute.
Consider a price written as <price currency="USD">24</price>. The element has an attribute, which becomes @currency, and it has text, the number 24. The text cannot also be the value of the object. So it moves to its own key, #text, leaving an object with @currency and #text side by side.
The same applies when text sits beside child elements: the text goes under #text and the children keep their own keys. If a converted value looks like an object where you expected a string, look for #text inside it.
Trap 4: namespaces and comments are not carried over
Some parts of an XML document have no JSON counterpart at all. Namespaces, comments and processing instructions are not carried over during conversion.
For comments, that is rarely a loss. For namespaces it can matter: feeds and document formats often place real content inside namespaced elements, and a reader who expects that content to survive conversion will find the JSON thinner than the XML. If the namespaced content is essential, XML to JSON conversion of this kind is the wrong tool for that document.
How to convert XML to JSON, step by step
- Get the XML as text. The XML to JSON Converter never fetches a URL, so copy the XML from your file, your editor or the response you already received.
- Paste it into the converter and run the conversion. It uses the browser's built in DOMParser, and everything happens on your device.
- If the XML is not well formed, the converter stops and reports the problem in plain words. Fix the XML and run it again.
- Read the result with the four rules in hand: attributes under
@keys, repeats as arrays, singles as singles, stray text under#text, and no namespaces or comments. - Copy the JSON and, before wiring it into anything, check the fields your code will read against the actual keys in this output.
Worked example: one record, two traps
Here is a small catalogue record with an attribute on the root element, one tag, and a price that carries a currency attribute:
<book id="bk-101">
<title>Everyday XML</title>
<tag>reference</tag>
<price currency="USD">24</price>
</book>
Converted, it becomes:
{
"book": {
"@id": "bk-101",
"title": "Everyday XML",
"tag": "reference",
"price": {
"@currency": "USD",
"#text": "24"
}
}
}
Walk the traps. The id attribute on <book> lands under @id, trap 1. There is exactly one <tag> element, so tag is the plain string "reference", not an array holding one string, trap 2; add a second <tag> and the same key becomes an array of two. The <price> element shows trap 3: its attribute becomes @currency and its text, 24, moves under #text.
Limitations and honest tradeoffs
The shape of the output depends on the document, so two files from the same source can produce differently shaped JSON when one has a single repeated element and the other has several.
Namespaces, comments and processing instructions are dropped, which makes the tool a poor fit for documents whose meaning lives in namespaced elements. Text values also stay strings: the 24 in the example arrives as "24", and anything that needs a number must convert it after the fact. Finally, malformed XML converts to nothing at all. The converter reports the well-formedness problem in plain words rather than guessing, so broken input is a dead end until you fix it.
Common mistakes
- Reading
idwhen the value is under@id. Attributes always carry the@prefix in the output. Check the prefixed key before declaring the field missing. - Assuming a repeatable element is always an array. A single occurrence stays single. Write reading code that accepts both shapes.
- Expecting an attributed element to be a plain string. Once an element has attributes, its text moves under
#text. Look inside the object. - Converting namespaced documents and expecting everything to survive. Namespaces are not carried over. Check whether your content depends on them first.
- Pasting a URL instead of the XML. The tool never fetches. Paste the document text itself.
FAQs
Why do my XML attributes start with @ in the JSON output? The converter turns attributes into keys prefixed with @, so an attribute named id becomes @id. The prefix keeps attributes separate from child elements, so the two never collide.
Why is the same element an object in one file and an array in another? Repeated elements become an array, and a single occurrence stays a single value. A document with three item elements produces an array of three; a document with one item produces a plain object or string. The shape depends on the document, so check before you assume.
What is the #text key in my converted JSON? It holds the text content of an element that also carries attributes or child elements. An element with only text becomes that text directly, but once attributes are present the text needs its own key: #text.
Where did my XML namespaces and comments go? They are not carried over. Namespaces, comments and processing instructions are dropped during conversion, so if your downstream code depends on namespaced elements, plan for their absence.
Does the converter fetch my XML from a URL? No. It never fetches a URL. You paste XML you already have, and the conversion runs in your browser using the built in DOMParser. If the XML is not well formed, the error is reported in plain words instead of producing broken JSON.
Conclusion
Converted JSON only looks wrong when you read it with XML expectations. Expect attributes under @ keys, single elements as single values, mixed content under #text, and namespaces to be absent, and the output of the XML to JSON Converter reads exactly as intended. The same shape rules show up when the source is a feed rather than a record, which is the subject of our companion guide on converting an RSS feed from XML to JSON. For more guides in this series, see our Data and JSON Tools guides.
References
- MDN Web Docs, on DOMParser and parsing XML in the browser: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
- W3C, Extensible Markup Language (XML) specification: https://www.w3.org/TR/xml/