JSON vs XML: Differences and When to Use Each

6 min read · Published 1 November 2026 · Updated 2 November 2026

JSON and XML both describe structured data as text, and both are everywhere: JSON in web APIs and configuration files, XML in documents, feeds, office files and older enterprise systems. They are not interchangeable in every situation, and picking the wrong one tends to cost you time later. This guide compares them on the points that actually matter in day-to-day work.

The same data in both formats

Here is one small record written in each format. Notice how XML repeats every name in an opening and a closing tag, while JSON relies on braces, brackets and quotes.

JSON

{
  "book": {
    "title": "Clean Code",
    "authors": ["Robert C. Martin"],
    "year": 2008,
    "inStock": true
  }
}

The XML version

The XML record is longer because of the closing tags, and it has a choice JSON does not: a value can be an attribute (inStock="true") or an element. That flexibility is useful for documents but also means two people can model the same data in two different shapes.

XML

<book inStock="true">
  <title>Clean Code</title>
  <authors>
    <author>Robert C. Martin</author>
  </authors>
  <year>2008</year>
</book>

Where JSON is the better choice

  • Web and mobile APIs. JSON maps directly to objects, arrays, strings, numbers, booleans and null, which is exactly what most programming languages already have. In JavaScript, JSON.parse() turns text into a usable object in one call.
  • Smaller, easier to read payloads. With no closing tags, the same data usually takes fewer bytes and fewer lines.
  • Simple configuration. Package manifests, editor settings and many build tools use JSON because it is easy to read and to generate.

Where XML is the better choice

  • Documents with mixed content. XML can hold text with inline markup, such as a paragraph with bold words and links. That is hard to express cleanly in JSON, which is why formats like XHTML, SVG, RSS and Office files are XML based.
  • Namespaces, schemas and transformations. XML has mature standards for validating structure (XSD), transforming documents (XSLT) and querying them (XPath). If your industry already standardised on these, XML is the practical answer.
  • Comments and processing instructions. XML allows comments directly in the file. Standard JSON does not allow comments at all.
  • Existing integrations. SOAP services, many banking, government and healthcare exchanges and a lot of legacy systems expect XML.

Data types and arrays

JSON has real types: a number is a number, true is a boolean and an array is an array. In XML everything is text unless a schema says otherwise, so 2008 and "2008" look identical until your code or schema interprets them. XML also has no built-in array. A list is just a repeated element, which can make a list with exactly one item look like a single value when it is converted to another format.

Validation and tooling

Both formats must be well formed before anything else works. JSON is well formed when its braces, quotes and commas are correct. XML is well formed when every tag is closed and properly nested and there is a single root element. Beyond that, XML has schema languages built into its ecosystem, while JSON validation usually uses JSON Schema, which is a separate specification. A formatter that reports the exact position of a syntax error saves a lot of guessing in either format.

Converting between them

Converting is less mechanical than it looks. XML attributes and elements both have to become JSON keys, repeated elements must become arrays, and XML text is untyped. Plan for these decisions before you build a pipeline that converts in both directions, and test with records that have one item, many items and no items.

A quick way to choose

  1. Building or consuming a web API with a modern client? Choose JSON.
  2. Storing a document with formatted text, or required to follow an existing XML standard? Choose XML.
  3. Writing a small configuration file people will edit by hand? JSON works, but if you want comments, consider a format designed for configuration.
  4. Unsure? Choose JSON for data and XML for documents.

A realistic example: an order with several items

Real data usually contains lists. The two snippets below describe the same order with two line items. In JSON the list is an array. In XML it is a repeated element, and the quantity is stored here as an attribute.

JSON

{
  "orderId": "A-1001",
  "items": [
    { "sku": "KB-01", "qty": 2 },
    { "sku": "MS-07", "qty": 1 }
  ]
}

The same order in XML, and a classic pitfall

A common bug appears when this XML is converted to JSON automatically. With two items you get an array, but an order with exactly one item has a single <item> element, and many converters then produce an object instead of an array with one element. Code that expects an array fails only for one-item orders. If you convert between the formats, define the list fields explicitly and test the one-item and empty cases.

XML

<order id="A-1001">
  <item sku="KB-01" qty="2"/>
  <item sku="MS-07" qty="1"/>
</order>

Size and speed in practice

XML text is usually longer than the equivalent JSON, but once a server compresses responses with gzip or Brotli, which web servers normally do, the gap shrinks a lot because repeated tag names compress very well. Parse speed depends more on the library than on the format. For very large files, a streaming parser (SAX-style for XML, or newline-delimited JSON where each line is one record) lets you process data without loading everything into memory.

Frequently asked questions

Is JSON replacing XML?
For web APIs, largely yes. For documents and many established standards (SVG images, RSS and Atom feeds, Office Open XML files, many industry exchange formats), XML remains the standard and is not going away.
Can a JSON file contain comments?
Not in standard JSON. Some tools accept supersets such as JSONC or JSON5 that allow comments, but a strict parser will reject them, so do not rely on comments in data that other systems will read.
Which format is more secure?
Neither is secure or insecure by itself. XML parsers have one well-known risk: external entity (XXE) attacks if the parser is configured to resolve external entities, so disable DTDs and external entities when parsing untrusted XML. With JSON, the usual problems come from how the data is used afterwards, for example building queries or HTML from it without escaping.
Does the order of keys matter in JSON?
In JSON, an object is an unordered set of name and value pairs, so you should not depend on key order. Arrays, on the other hand, are ordered. In XML, the order of child elements is significant.

Sources and further reading

Try it yourself: JSON Formatter & Validator runs in your browser, free and with no sign-up.

More guides