XML to JSON, What Gets Lost and How to Convert Faithfully
XML and JSON describe the same thing, a tree, but they are not interchangeable models, so converting one to the other always involves decisions that can lose or change data. XML has three places to put information, elements, attributes, text. JSON has two, keys and values, primitives or arrays. Every conversion must choose how to fold attributes, repeated nodes and mixed content into JSON's narrower shape.
The XML to JSON converter supports attributes, nested elements and CDATA, which are the parts most likely to break. This guide explains the mismatches so you understand what the output means and where faithful conversion is genuinely impossible, the classic being the one-vs-many ambiguity that turns a single element into a non-array and crashes the consumer.
The structural mismatch
In XML an element carries data in three ways. As a child element, as an attribute on the tag, and as text content. JSON has no equivalent of attributes and no separate text-vs-element distinction, an object just has named values. So conversion invents a convention. Attributes are typically promoted to keys, often prefixed, like @attr, and text is mapped to a special key like #text when an element has both text and children.
None of these conventions is universal. The same XML can be converted to JSON in several legitimate ways, so an output is only meaningful together with the rules that produced it, never assume your parser maps things the way the sender's did.
The one-vs-many problem, the big silent break
XML has no arrays. A repeating node is expressed as multiple sibling elements, and you cannot tell from a single instance whether it is one item or a list of one. Converters usually make repeated elements into JSON arrays and a lone element into a single object, which means the shape changes based on count. A consumer expecting items to always be an array breaks the day it receives one item that arrives as an object.
This is the most damaging loss in XML-to-JSON work precisely because it hides until an edge case appears. The robust fix is to know the schema and force a consistent array for known-repeating elements, or use a converter whose behaviour you can verify against multi- and single-item samples.
| XML concept | Typical JSON mapping | What can go wrong |
|---|---|---|
| Element with children | nested object | Fine when structure is clear. |
| Attribute | key like @id | Convention differs between tools. |
| Text content | value or #text | Mixed text plus elements loses position. |
| Repeated element | array | single item becomes object, not array. |
| CDATA | string | usually preserved as plain text. |
| Namespaces | prefixed key | prefixes may or may not be kept. |
The converter here preserves attributes and handles CDATA as text, but the array-vs-object decision still depends on how many copies appear, always check the single-item case.
Mixed content, whitespace and CDATA
XML lets text and elements interleave inside one node, mixed content, which has no clean JSON home, flattening it discards where each piece sat. Whitespace-only text nodes can appear as stray keys unless trimmed. CDATA sections are raw character data, often embedded markup or code. The sensible handling is to expose them as ordinary string values, which is what this tool does.
Namespaces add another wrinkle. <xs:element>, do you keep the prefix, drop it, or expand the URI? Different tools differ, and dropping prefixes can collide two distinct elements into one key.
When you should not convert at all
JSON is friendlier for APIs and storage, but some XML has no good JSON twin. If you rely on document order across mixed element types, on XML schema or namespace validation, on XSLT transforms, or on SOAP, converting to JSON discards the very features you need. Keep XML where XML is load-bearing. Convert when you genuinely just want the data tree.
After conversion, verify the JSON before trusting it. Run it through the JSON validator and formatter, and to go back use the JSON to XML converter, remembering that a JSON round-trip is not guaranteed identical because of the conventions chosen on the way out.
Frequently Asked Questions
Why is my single element not an array in the JSON?
Because XML has no arrays. A converter turns repeated elements into a JSON array but a lone element into a single object. That means the shape depends on count, and code expecting an array breaks on one item. Force a consistent array for known-repeating nodes or verify against a one-item sample.
Where do XML attributes go in JSON?
JSON has no attributes, so converters promote them to keys, commonly with a prefix like @id, and put text under something like #text. The exact rule varies by tool, so an output only makes sense with the mapping rules that produced it, never assume sender and receiver agree.
Is an XML to JSON round trip lossless?
No. Going XML to JSON to XML can differ because conversion made choices, attribute prefixing, array-vs-object, namespace handling, that the reverse step cannot perfectly undo. Treat each conversion as a lossy projection, not a re-encoding.
What happens to CDATA?
CDATA is just escaped character data, and the sane result is to expose it as a plain string value, which this converter does. Problems arise only when a tool mishandles it or when CDATA contained markup you expected preserved as structure.
Should I keep data as XML or convert to JSON?
Convert for APIs and easy consumption, but keep XML when you depend on document order across mixed types, schema or namespace validation, XSLT or SOAP. If XML features are load-bearing, a JSON projection throws them away.
How do I check the converted output is correct?
Validate and inspect it. Run the result through the JSON validator, format it with the JSON formatter for readability, and diff key counts against the source, especially elements that should have become arrays and attributes that should have become keys.