ToolConvoyToolConvoyv2.6
← Back to blog

How to Convert XML to JSON Without Losing Data

ToolConvoy

XML was the universal data format before JSON was, and a surprising amount of it is still in active production: SOAP web services, RSS and Atom feeds, sitemaps, Office document formats (DOCX, XLSX, PPTX are all ZIPs of XML), Android manifests, Spring configuration files, SAML assertions, and a long tail of legacy enterprise systems. JSON is the format every modern web stack speaks natively. The bridge between them — XML to JSON — comes up in almost every integration project, and the conversion gets tricky if you treat XML as "JSON with extra syntax" instead of a model with its own primitives.

Key Differences Between XML and JSON

The two formats model data differently, and the conversion choices matter.

  • XML has attributes; JSON does not. XML carries two parallel channels of information at every element: attributes (key-value pairs on the opening tag, like <price currency="USD">9.99</price>) and child elements. JSON has only one channel. You have to pick where attributes live in the JSON output — by convention our converter prefixes them with @ ({"@currency": "USD", "#text": "9.99"}), but other conventions exist (a nested "@attributes" object, or merging attributes into the parent object).
  • XML has namespaces; JSON has no equivalent. Namespaced XML (<svg:rect xmlns:svg="..." />) declares scope and disambiguation. The conventions are: strip prefixes, keep them, or represent them as {"xmlns:prefix": "uri"}. Our converter exposes this as a configurable option.
  • XML has mixed content; JSON does not. <p>This is <b>bold</b> text.</p> is structural in JSON because the model has no place for "mixed text + child elements." Common resolutions: drop the text content, keep just the bold element, or wrap the mixed content in a "#text" / "#children" split.
  • XML has schemas; JSON has no formal schema equivalent. XSD validates XML structure; JSON Schema, OpenAPI, and TypeScript types are the closest equivalents for JSON, but they're all optional and the validation ecosystem is fragmented.
  • XML has an order-preserving array semantics; JSON does too, but JSON arrays can't be empty. <elems></elems> (no children) is structurally different from <elems/> in some XML processors, while JSON has no concept of "the parent knows the element was supposed to be an array" — empty would just be a missing key.

How Our Converter Handles XML

Our XML to JSON converter preserves the four most important structural properties:

  • Element hierarchy as nested objects. A nested XML structure becomes a nested JSON structure with the same depth and key names.
  • Attributes as @-prefixed properties. <img src="x.png" alt="X"/> becomes {"@src": "x.png", "@alt": "X"}. The @ prefix follows the BadgerFish convention, the most widely-used XML-to-JSON mapping.
  • Text content as #text key. An element with both children and text becomes {"#text": "...", ...children}. This is the same convention as the attributes.
  • Multiple child elements as arrays. If an element has <child/> more than once, the result is an array ("child": [...]); if it has it once, it's still a single object — unless you tell the converter to always wrap in arrays, which is sometimes the safer choice for downstream code that wants to use a uniform access pattern.

The JSON output is therefore round-trippable: re-converting the JSON back to XML (with JSON to XML) produces an equivalent document, though some details (whitespace, attribute order, namespace prefixes) will differ.

Controls Available

  • Root element name. Use this to disambiguate when the input XML has multiple top-level elements or to wrap a series of fragments into a single root. Some JSON parsers are strict about the top-level value type — wrapping multiple top-level elements in a named root makes the output a single valid object.
  • Always-array mode. Switch on to force every repeated element into an array, even when there's only one. Useful when downstream code expects arrays.
  • Indentation level. Pretty-print for human review, compact for downstream API consumption.
  • Include / exclude the XML declaration. Most pipelines don't need <?xml version="1.0" ?> in the output; turn it off by default to keep the JSON clean.
  • Namespace handling. Keep, strip, or merge into keys. The default (strip prefixes) is the most common conversion, but explicit namespacing is sometimes required by downstream tools.

Common Use Cases

  • RSS / Atom feeds. Pipeline an RSS feed into a JSON-consuming system (a Slack webhook, a React UI, a custom database) without writing a parser. Our XML to JSON converter handles <rss>, <channel>, and <item> cleanly.
  • SOAP responses. Older enterprise APIs return SOAP envelopes; converting to JSON drops the envelope boilerplate (<soap:Envelope>, <soap:Body>) and gives you just the useful payload.
  • Sitemaps. A sitemap.xml is itself valid XML — converting to JSON lets you index the entries in a database or pass them to a checker that doesn't speak XML natively.
  • Configuration files. Spring, Maven, Ant, and a long tail of Java-era tools store config in XML. Converting to JSON lets you feed those values into modern applications that prefer JSON configuration.
  • DOCX / XLSX / PPTX internal structure. Office documents are ZIPs of XML. If you're programmatically inspecting one (without using a full Office parser), extracting the XML and converting it to JSON lets you write simpler logic.

Edge Cases Worth Noting

  • CDATA sections (<![CDATA[ ... ]]>) become plain text in the JSON output. Downstream code that cares about preserving the CDATA structure can re-wrap the relevant text from a marker. For most use cases, plain text is fine.
  • Processing instructions (<?something ?>) are dropped by default. They're rarely part of a data payload.
  • Comments (<!-- ... -->) are dropped. If your XML uses comments to carry metadata you care about, extract them before conversion.
  • Whitespace. The converter preserves whitespace within text content but treats whitespace between elements as insignificant by default (matching XML semantics). For pretty-printed input that came from a human-edited file, expect the output JSON to lose the visual indentation of the source.

Convert XML to JSON now: XML to JSON. Need the reverse? Try JSON to XML.

Ready to convert? Try our free EPUB to PDF tool.

Convert EPUB to PDF now →