DataSwitch Pro Private, in-browser data converter

How to Convert XML to JSON: Attributes, Repeated Elements and Namespaces

By the DataSwitch Pro team · · 4 min read

Quick answer

XML to JSON conversion maps each element to a JSON key, repeated elements with the same name to an array, and attributes to keys with a prefix — commonly @id — while an element's text, when it also has attributes, goes into a key such as #text. Because XML has no data types or arrays, a converter has to guess them, so check single-item lists and numeric-looking values. Use an online XML to JSON converter, or xmltodict in Python.

XML still powers RSS feeds, sitemaps, SOAP services, e-invoicing, bank statements and many enterprise exports. Modern code, however, prefers JSON. The conversion is easy for simple documents, but XML has a few features that JSON simply does not have, and every converter has to choose how to represent them.

How XML structures map to JSON

XML

<order id="1042" status="paid">
  <customer>Leila B.</customer>
  <item sku="A1">2</item>
  <item sku="B7">1</item>
  <note/>
</order>

JSON (a common convention)

{
  "order": {
    "@id": "1042",
    "@status": "paid",
    "customer": "Leila B.",
    "item": [
      { "@sku": "A1", "#text": "2" },
      { "@sku": "B7", "#text": "1" }
    ],
    "note": null
  }
}

The single-item array problem

XML has no arrays; a list is just an element that happens to repeat. When an order contains one <item>, a converter sees no repetition and produces an object instead of an array of one. Code that expects order.item[0] then breaks only for some records. Solutions:

Types: everything in XML is text

<qty>2</qty> contains the text “2”, not a number. Converters either keep every value as a string (safe, predictable) or detect numbers and booleans (convenient). Detection can damage values such as postcodes with leading zeros or amounts like 1499.00, which becomes 1499. Keep strings when exact text matters, and convert specific fields in code.

Namespaces

Feeds and business documents often use prefixes, such as <g:price> in Google Merchant feeds or <cbc:ID> in UBL invoices. Most converters keep the prefix in the key ("g:price"). That is fine, but note that the prefix is only a local alias for a namespace URL; two files can use different prefixes for the same namespace. For robust processing, rely on the namespace URL in an XML library rather than on the prefix text.

Converting in Python

Python

import json, xmltodict   # pip install xmltodict

with open("orders.xml", encoding="utf-8") as f:
    data = xmltodict.parse(f.read(), force_list=("item",))

with open("orders.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

xmltodict uses the same @attribute and #text conventions shown above. For quick, one-off conversions, the XML to JSON converter does the same in the browser without installing anything.

What gets lost

Need a table instead?

If the XML is a list of records — products, transactions, sitemap URLs — and you want to analyse it in a spreadsheet, convert it straight to a table with XML to CSV, or read how to open an XML file in Excel.

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

Can JSON be converted back to the same XML?

Only if the JSON follows the same conventions (@ for attributes, #text for text). Comments, namespace declarations and some ordering may still differ. See JSON to XML.

Why did my numbers lose their decimals?

Number detection read “1499.00” as the number 1499. Turn off type detection to keep values exactly as written.

Is there one official XML to JSON mapping?

No. Conventions differ between tools (BadgerFish, Parker, xmltodict-style). Pick one and use it consistently.

Why do I get an error about a tag?

The XML is not well-formed: a tag is not closed, tags are nested incorrectly, or an HTML entity like &nbsp; is used. Fix the reported line and try again.

More guides