How to convert XML to JSON
- Paste XML, drop an
.xmlfile onto the editor or load it from a URL. The JSON appears as you type. - Adjust the attribute prefix and text key, and decide whether to always use arrays, convert numbers and booleans, or ignore namespace prefixes.
- Copy the JSON or download it as a
.jsonfile.
How XML maps to JSON
| XML | JSON |
|---|---|
<name>Ada</name> | "name": "Ada" |
<price>9.90</price> | "price": 9.90 – digits kept exactly |
<item id="7">Pen</item> | "item": {"@id": 7, "#text": "Pen"} |
<tag>a</tag><tag>b</tag> | "tag": ["a", "b"] |
<note/> | "note": null |
<![CDATA[a < b]]> | the text "a < b" |
<soap:Body> with Ignore namespace prefixes | "Body" |
Why “Always use arrays” matters
An element that appears once becomes a single value, but when it appears twice it becomes an array. Code that reads the JSON then has to handle both shapes – a classic bug when a list sometimes contains only one item. Tick Always use arrays for child elements to get arrays everywhere: the structure is predictable at the cost of a few extra brackets.
XML to JSON in code
- Python:
xmltodict.parse(text)(package xmltodict) - JavaScript:
new XMLParser({ ignoreAttributes: false }).parse(text)(package fast-xml-parser) - C#:
JsonConvert.SerializeXmlNode(doc)(Newtonsoft.Json) - Command line:
yq -p xml -o json file.xml
Frequently asked questions
Is the conversion lossless?
Element names, attributes and text are kept, and numbers keep their exact digits. Comments, processing instructions and the position of text between child elements (mixed content) cannot be represented in JSON.
Can I convert JSON back to XML?
Yes, with the JSON to XML converter of our sister site JSON Formatter, which uses the same @ and #text conventions.
What happens to namespaces?
Prefixed names such as soap:Body are kept as keys by default, and xmlns declarations appear as attributes. Tick Ignore namespace prefixes to get plain names like Body without the declarations.
Why is a value a string instead of a number?
Values like 007 or 1,000 are not plain decimal numbers, so they stay strings to avoid data loss. Untick Convert numbers and true/false to keep every value as a string.