XML-to-JSON Conversion — Lessons from Reddit

November 28, 2026 · XML.now Editorial · Reddit Guides

An evidence-led XML guide distilling genuine Reddit experiences, technical limits, privacy checks, and a reproducible workflow.

Outline for XML-to-JSON Conversion

  • The structural mismatch that makes lossless conversion impossible by default
  • Why well-formed XML is necessary but insufficient
  • Mixed content: the silent data-loss vector
  • Namespaces and attributes: metadata without a JSON home
  • Repeated elements and the array-or-scalar trap
  • Element order versus JSON’s unordered objects
  • Security boundaries when choosing a conversion tool
  • The four mapping decisions every conversion demands
  • When retaining XML is the better engineering choice

A developer pulls raw XML from a legacy endpoint, pastes it into a browser-based converter, and receives JSON that passes a quick glance. The same operation run against a second payload from the same API fails downstream because a field that was a string in the first response is now an array. Nothing was lost at the byte level, yet the consumer broke. The root cause is not a bug in the converter. It is the irreducible gap between the XML Information Set and the JSON data model. One is a node-labeled tree with ordered children, mixed content, and a metadata layer carried by attributes and namespaces. The other is a collection of name-value pairs and ordered lists with no attribute-child distinction and no native concept of sequence among keys. Mapping the first onto the second is never automatic; it is always a design decision with trade-offs.

Well-formed is the entry ticket, not the guarantee

Every XML-to-JSON pipeline begins with a parser, and every conformant parser rejects input that is not well-formed. A missing end tag, an unescaped & outside an entity declaration, or a bare < in an attribute value halts processing before a single token reaches conversion logic. The W3C XML 1.0 specification defines well-formedness as a hard gate, and tools such as xmllint (libxml2) or PowerShell’s [xml] type accelerator enforce it strictly. Passing that gate, however, says nothing about whether the resulting JSON will be usable. A document that is perfectly valid against its schema can still produce JSON that silently drops content, reorders semantically significant elements, or collapses namespaces into ambiguous strings.

Mixed content loses structure silently

Consider an XML fragment drawn from a documentation format:

<para>Set the <code>DEBUG</code> flag to true.</para>

The para element contains text, a child element, and more text. This is mixed content, explicitly permitted by the XML specification. JSON has no native representation for interleaved text nodes and child objects. Most generic converters handle this by extracting only the child element, concatenating all text nodes into a single string and discarding child structure, or producing an array of strings and objects that no downstream consumer expects. In every case, the original structure cannot be reconstructed from the JSON output. The failure is silent: the converter emits valid JSON, and the data loss becomes visible only when a consumer needs the full content.

Preservation of mixed content demands a custom mapping that explicitly models text runs and elements, for example as an ordered array of { "type": "text", "value": "..." } and { "type": "element", "name": "code", "children": [...] } objects. No off-the-shelf converter makes this choice by default because it is verbose and domain-specific.

Namespaces and attributes collapse into flat keys

XML attributes carry metadata: lang, id, units, and namespace declarations such as xmlns:xsd. A naive converter maps every attribute to a JSON property with the same name, losing the distinction between data that was an attribute and data that was a child element. Namespace prefixes may be stripped, preserved as part of the key name, or expanded to their full URI. Each choice has consequences.

If the source document uses xsd:date and sql:date to distinguish schema domains, a converter that strips prefixes produces two properties named date, and the second overwrites the first. A converter that preserves prefixes produces keys such as xsd:date and sql:date, which are valid JSON strings but break conventions that expect alphanumeric identifiers. Expanding to full namespace URIs yields keys like {http://www.w3.org/2001/XMLSchema}date, which are unwieldy and require consumers to understand the URI convention.

Operational check: Before converting, audit every attribute in the source XML. If an attribute carries semantic weight beyond display formatting, decide explicitly how it will survive the transition. There is no universal answer, only a documented trade-off.

Repeated elements and the array-or-scalar heuristic

XML permits an element to appear zero, one, or many times without syntactic distinction. JSON requires that a given key hold either a single value or an array. The common converter heuristic—emit a scalar when the document contains one instance, emit an array when it contains more than one—creates a schema that changes shape based on data volume. A consumer that receives a string in development and an array in production will fail at the worst possible time.

This is not a theoretical edge case. It is the most frequently reported failure mode in XML-to-JSON pipelines. The converter cannot know whether an element is intended to repeat because that information lives in a schema the converter does not read. The only safe default is to force every element that could repeat into an array, even when the array holds a single item. The resulting JSON is noisier but structurally stable across payloads.

Element order is significant; JSON object order is not

XML element order carries meaning unless a schema explicitly relaxes it with a construct such as xsd:all. A sequence of <step> elements in a procedure or <event> elements in a log is ordered by definition. JSON objects, per RFC 8259, treat keys as unordered collections. A converter that preserves order by emitting an array of key-value pairs produces JSON that is technically valid but breaks idiomatic consumption patterns. A converter that emits a standard object may reorder keys, destroying the sequence semantics.

Recoverability options: For order-dependent XML, convert to a JSON array of objects, each carrying a name and a value. This is verbose but faithful. Alternatively, embed an explicit order or position field in each object and sort on consumption. Both approaches shift complexity from the converter to the consumer, which is the honest trade-off.

Security boundary: where does the XML go?

Pasting XML into a web-based converter sends the document to a remote server unless the tool explicitly states that processing happens client-side. The W3C XML security considerations flag entity expansion, external resource fetching, and data confidentiality as risks. To those, add the risk of the converter operator logging or storing submitted payloads. Credentials, customer records, and internal API responses should never transit an untrusted third party.

Local tools eliminate network exposure: xmllint for validation and XSLT processing, PowerShell’s XML DOM for scripting, Visual Studio Code with an XML extension for interactive inspection. Oxygen XML Editor provides schema-aware conversion but requires a license. Browser-based tools that perform conversion entirely in JavaScript and document that fact are a middle ground, provided the page is loaded over HTTPS and inspected for outbound requests.

The four mapping decisions

Every XML-to-JSON conversion, whether scripted or interactive, must answer four questions. There is no default that works for all documents.

  1. Attributes: Map to sibling properties (losing the attribute-child distinction), prefix with a sigil such as @, or nest under a dedicated _attributes key.
  2. Namespaces: Strip entirely, preserve as prefix strings on property names, or expand to full URI strings.
  3. Mixed content: Drop text nodes, concatenate all text and discard child structure, or represent as an ordered array of typed segments.
  4. Repeated elements: Always emit an array, or conditionally emit a scalar when the document contains a single instance (with the documented risk of shape change).

An XSLT stylesheet that emits JSON text gives the author explicit control over every one of these decisions. Generic converters encode the author’s best guess, and that guess fails when the document deviates from the pattern the author anticipated.

When XML should stay XML

Not every document benefits from conversion. If the source relies on mixed content, namespace-qualified attributes that carry semantic distinctions, or strict element order that consumers must honor, the cost of a faithful JSON representation may exceed the cost of using an XML-aware library in the consuming code. Languages with mature XML support—Python’s lxml, Java’s JAXB, .NET’s System.Xml.Linq—allow working with the native tree without lossy transformation. The apparent convenience of JSON is not worth silent data corruption.

XML-to-JSON is a transformation, not a view layer. It requires explicit mapping decisions, documented trade-offs, and a clear understanding of what the JSON consumer actually needs. Treating it as a one-click operation guarantees that the failure modes described above will surface in production, far from the converter UI and long after the original XML is gone.

Community reports reviewed

These discussions are used as reports of real user problems, not as technical specifications: