If you are choosing a format for an API payload or a document, compare how JSON and XML represent the same data.

The examples below show how that choice affects structure, validation and the code that reads it.

The same data in both

{
"book": {
"id": 42,
"title": "Refactoring",
"authors": ["Martin Fowler", "Kent Beck"],
"inPrint": true
}
}

<book id="42">
<title>Refactoring</title>
<authors>
<author>Martin Fowler</author>
<author>Kent Beck</author>
</authors>
<inPrint>true</inPrint>
</book>

Two things jump out. The XML is longer because every element name is written twice.

And the XML has a choice JSON does not: id could be an attribute or a child element, and different people will choose differently.

Where JSON wins

Size. The example above uses fewer characters in JSON. The difference depends on field names, data shape, whitespace and compression; measure your actual payload.

Maps directly onto data structures. Objects become dictionaries and arrays become lists in every language. XML needs a mapping layer to decide what becomes an attribute, an element or text.

Native types. JSON distinguishes the number 42 from the string "42" and has real booleans. In XML everything is text until a schema says otherwise.

Arrays. JSON has an array type. XML can represent lists with repeated elements. When mapping XML to objects, agree which elements represent lists so that a single item stays a list.

Parsing in the browser. JSON.parse returns JavaScript values. DOMParser.parseFromString parses XML into a document tree that your code can read.

Where XML wins

Documents with mixed content. Text with markup inside it, like a paragraph containing bold words and links, is natural in XML and awkward in JSON. HTML and XML have different parsing rules, as the HTML specification explains.

Mature schema and validation. XSD and Relax NG have been standard for twenty years. JSON Schema exists and works well, covered in our JSON Schema introduction, but XML validation tooling in enterprise stacks is deeper.

Namespaces. XML namespaces distinguish element and attribute names using a namespace URI. JSON object keys are strings, so you need your own naming convention when combining vocabularies.

Transformation. XSLT can turn one XML document into another declaratively. There is no standard JSON equivalent, though jq comes close for many tasks.

Comments and metadata. XML allows comments and processing instructions. JSON has neither.

Existing standards. SOAP, SAML, RSS, SVG, Office documents, Android layouts and Maven use XML. Check the format your integration requires before choosing another one.

Size and speed in practice

Compare the UTF-8 byte lengths of equivalent documents before and after compression. Then measure parsing in your target runtime.

RFC 8259 and the XML specification define the formats, but neither gives a universal size or speed ratio.

Security notes

XML supports entity definitions that can reference other content. Before parsing untrusted XML, check your parser's settings for external entity loading and entity expansion limits.

For either format, limit the input your application accepts and validate the parsed values before using them.

JavaScript can also lose precision when parsing very large JSON numbers. Our JSON.stringify and JSON.parse guide shows that data correctness issue.

Recommendation

Situation | Use

New web or mobile API | JSON

Configuration files | JSON or YAML

Rich text documents, publishing | XML

Integrating with SOAP or an XML based enterprise interface | XML

Data that needs schema validation | Either, with a schema supported by your tools

Data that requires XML namespaces | XML

Logs and event streams | JSON, one object per line

Most of the time, JSON.

Use our JSON formatter to pretty print and validate a document in your browser. The JSON visualizer draws its nested structure as a diagram.