XML vs JSON: What’s the Difference and How to Convert Between Them
XML and JSON both describe structured data as text, and both are everywhere: configuration files, web APIs, data feeds, sitemaps. XML came first and dominates older enterprise and document systems. JSON is lighter and has become the default for modern web APIs. Sooner or later you have to move data from one to the other.
The same data in both formats
A small record shows the main difference. XML wraps values in tags and can carry attributes; JSON uses key–value pairs, arrays and a few basic types.
| XML | JSON |
|---|---|
| <book id="1"><title>Dune</title></book> | {"book": {"@id": "1", "title": "Dune"}} |
Key differences
| XML | JSON | |
|---|---|---|
| Structure | Tags, attributes, text and nesting | Objects, arrays, strings, numbers, true/false, null |
| Verbosity | Wordier; every value needs a closing tag | Compact |
| Data types | Everything is text | Numbers, booleans and null are built in |
| Comments | Allowed | Not allowed |
| Mixed content | Handles text mixed with tags (documents) | Awkward |
| Typical use | Documents, feeds, SOAP, configuration | Web and mobile APIs, app settings |
Where conversion gets tricky
- Attributes. JSON has no attributes. A common convention is to turn each attribute into a key starting with @, for example "@id": "1".
- Repeated elements. When the same element appears more than once under one parent, the values become a JSON array. A single occurrence becomes a plain value, which can surprise code that expects an array.
- Numbers and booleans. XML has no number type, so every value is text unless you convert it. Leave conversion off for IDs and postal codes with leading zeros.
- The root element. XML requires exactly one root. A JSON object with several top-level keys needs a wrapper element when converted to XML.
XML to JSON step by step
- Open XML & JSON Converter and choose XML to JSON.
- Paste your XML or open a file; the JSON appears as you type.
- Decide whether to keep attributes and whether to convert numbers and true/false values.
- Copy or download the result.
JSON to XML step by step
- Choose JSON to XML and paste the JSON.
- If there is a single top-level key, it becomes the root element; otherwise set a root name.
- Check the result and fix any key that is not a valid XML name, since names cannot start with a digit or contain spaces.
If you see an error
XML must be well formed: every tag closed, attribute values in quotes, and one root element. JSON must follow strict rules too, such as double quotes and no trailing commas. The parser message usually points to the line and column of the problem.
Which format should you choose?
If you are building a new web API or app setting, JSON is usually simpler. If you work with documents, standards that require it, or existing systems built on XML, stay with XML. Converting is for the moments in between.
Key terms: XML to JSON, JSON to XML, XML converter, attributes, arrays, root element, well-formed XML, data interchange, API format.
Frequently asked questions
How do I convert XML to JSON?
Choose XML to JSON, paste your XML or open a file, and the JSON appears as you type. Copy or download it.
How do I convert JSON to XML?
Choose JSON to XML and paste your JSON. A single top-level key becomes the root element; otherwise the data is wrapped in a root you can name.
What happens to XML attributes?
With the attribute option on, each attribute becomes a key that starts with @. Turn it off to leave attributes out.
How are repeated elements handled?
Repeated elements under one parent are collected into a JSON array. A single one becomes a plain value.
Is my data sent anywhere?
No. The conversion runs entirely in your browser.
Have XML or JSON to convert?
Try XML & JSON Converter →Put this guide into practice
XML & JSON Converter — Convert XML to JSON and JSON to XML, free and private.
JSON Formatter & Validator — Format, validate and minify JSON, or convert JSON to CSV.