XML vs JSON: Key Differences and When to Use Each
XML vs JSON compared: syntax, data types, attributes, comments, schemas and size, with the same data shown in both formats and advice on which to choose.
The short answer
JSON is the usual choice for web APIs, configuration and data passed between JavaScript, Python or mobile apps: it is shorter, maps directly onto objects and arrays, and has real number, boolean and null types. XML is still the better fit for documents with mixed text and markup, for formats that need attributes, namespaces or mature schema validation, and for systems that already speak it, such as SOAP services, RSS feeds, SVG, Office files and many government and banking interfaces. To read either one, paste it into the XML formatter or the JSON formatter.
The same data in both formats
Here is one book record in XML, shown one line at a time:
<book id="1" lang="en"><title>Clean Code</title><author>Robert C. Martin</author><price currency="USD">45.99</price><inStock>true</inStock><tags><tag>programming</tag><tag>craft</tag></tags></book>
And a natural JSON equivalent:
{ "id": 1, "lang": "en", "title": "Clean Code", "author": "Robert C. Martin","price": { "amount": 45.99, "currency": "USD" }, "inStock": true, "tags": ["programming", "craft"] }
Notice what had to change. XML's attributes (id, lang, currency) became ordinary keys. The repeated <tag> elements became an array. And in JSON, 1, 45.99 and true are a number and a boolean, while in XML they are just text until a schema or your code says otherwise.
Side-by-side comparison
| Feature | XML | JSON |
|---|---|---|
| Basic structure | Nested elements with opening and closing tags | Objects (key/value) and arrays |
| Attributes | Yes, separate from child elements | No; everything is a key |
| Data types | Everything is text unless a schema defines types | String, number, boolean, null, object, array |
| Arrays | Implied by repeating an element | Built in with square brackets |
| Comments | Yes | No (the standard does not allow them) |
| Mixed text and markup | Yes, e.g. a paragraph with bold words inside | Awkward; must be modelled manually |
| Namespaces | Yes, to combine vocabularies | No |
| Schema validation | XSD, DTD, RELAX NG; long-established | JSON Schema |
| Querying | XPath, XQuery, XSLT for transforms | JSONPath, jq, or plain code |
| Size | Larger because of closing tags | Usually smaller for the same data |
| Parsing in browsers | DOMParser | JSON.parse, built into JavaScript |
Where JSON wins
- APIs and web apps.
JSON.parseturns a response into JavaScript objects in one call. Most REST and GraphQL APIs return JSON. - Simple data. Lists of records, settings, and key/value data map directly onto JSON without decisions about attributes versus elements.
- Typed values. Numbers and booleans arrive as numbers and booleans, so you do not have to convert
"true"totrueyourself. - Readability for developers. Less punctuation for the same data, which makes diffs and code reviews easier.
Where XML wins
- Documents, not just data. A paragraph like
<p>Pay <b>before</b> Friday</p>is natural in XML and clumsy in JSON. That is why HTML-like formats (XHTML, SVG, DOCX internals) are XML. - Metadata as attributes. Units, IDs and language codes sit cleanly on an element without changing its content.
- Strict contracts. XSD schemas let organisations define precise, versioned formats that both sides validate against. Many e-invoicing, banking and tax filing systems are built on this.
- Comments. You can explain a configuration file inline.
- Existing ecosystems. SOAP web services, RSS and Atom feeds, Android layout files, Maven
pom.xmland sitemaps all use XML.
Converting between them: what to watch for
Because the models differ, conversion is never perfectly round-trip. The common problems:
@id.<tag> element often becomes a string, while two become an array. Code expecting an array then breaks on records with only one item."45.99" rather than 45.99.The Lazyblink XML formatter has a JSON view that shows these effects clearly. In that view, child elements become keys, repeated elements become an array, element text becomes a string, and attributes are not included. If you paste the book example above, the id, lang and currency values do not appear in the JSON. For a full walkthrough of these cases, see how to convert XML to JSON.
How to check and tidy either format
XML
&, or two root elements. How to validate XML and fix parse errors covers each one.JSON
Which one should you choose?
- Building a new web or mobile API: JSON.
- App configuration: JSON, or YAML or TOML if you need comments.
- A document format with rich text: XML.
- Exchanging data with a partner, bank or government system: whatever their specification says, which is often XML with an XSD.
- Feeds, sitemaps, SVG graphics: XML, because those standards are defined in XML.
If you are free to choose and the data is records and lists, JSON is almost always simpler. If the data is a document, or the other side already defines an XML schema, use XML.
Frequently asked questions
What is the main difference between XML and JSON?
XML uses nested tags and attributes and treats all values as text unless a schema adds types. JSON uses objects and arrays and has built-in string, number, boolean and null types. JSON is usually shorter and easier to use in code; XML handles documents, attributes, namespaces and comments.
Is JSON better than XML?
For web APIs and simple data, JSON is usually simpler and smaller. XML is better for documents with mixed text and markup, strict schema-based exchange, and formats already defined in XML such as SVG, RSS, sitemaps and SOAP.
Can JSON have comments like XML?
No. The JSON standard does not allow comments, and JSON.parse rejects them. XML supports comments, which are written between an opening and closing comment marker.
Why are attributes missing after converting XML to JSON?
JSON has no attributes, so converters either drop them or turn them into keys. The Lazyblink XML formatter's JSON view converts elements only and leaves attributes out.
Is XML still used?
Yes. It is used in SOAP services, RSS and Atom feeds, sitemaps, SVG, Office document internals, Android layouts, build files such as pom.xml, and many banking and e-invoicing formats.
Put this guide into practice with our free online tool — no signup required.
Open tool