Comparison 5 min read

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.

xml vs jsondifference between xml and jsonjson or xmlxml vs json apiis json better than xml

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

FeatureXMLJSON
Basic structureNested elements with opening and closing tagsObjects (key/value) and arrays
AttributesYes, separate from child elementsNo; everything is a key
Data typesEverything is text unless a schema defines typesString, number, boolean, null, object, array
ArraysImplied by repeating an elementBuilt in with square brackets
CommentsYesNo (the standard does not allow them)
Mixed text and markupYes, e.g. a paragraph with bold words insideAwkward; must be modelled manually
NamespacesYes, to combine vocabulariesNo
Schema validationXSD, DTD, RELAX NG; long-establishedJSON Schema
QueryingXPath, XQuery, XSLT for transformsJSONPath, jq, or plain code
SizeLarger because of closing tagsUsually smaller for the same data
Parsing in browsersDOMParserJSON.parse, built into JavaScript

Where JSON wins

  • APIs and web apps. JSON.parse turns 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" to true yourself.
  • 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.xml and sitemaps all use XML.

Converting between them: what to watch for

Because the models differ, conversion is never perfectly round-trip. The common problems:

  • Attributes have nowhere obvious to go. Converters either drop them or invent keys such as @id.
  • Single items versus arrays. One <tag> element often becomes a string, while two become an array. Code expecting an array then breaks on records with only one item.
  • Everything becomes a string. XML has no number type, so a converter may give you "45.99" rather than 45.99.
  • Comments and processing instructions are lost.
  • 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

  • Paste the XML into the XML formatter (or click Load sample to try it).
  • If the XML is not well-formed, the tool shows the parser's error message instead of output. Typical causes are a missing closing tag, an unescaped &, or two root elements. How to validate XML and fix parse errors covers each one.
  • Switch between Formatted XML and JSON, and use Copy to take the result.
  • JSON

  • Paste it into the JSON formatter.
  • Fix any reported syntax error. The usual ones are trailing commas, single quotes instead of double quotes, and comments, none of which are valid 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.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool