JSON and XML are two ways to write data as plain text. Both let one program send data to another. Both can be read by people, too. But they look very different, and each one is better at different jobs.
This guide puts them side by side. You will see how they differ in syntax, size and features. You will also see where each one is still used today.
Quick answer: JSON is a light format made of objects, lists and simple values. It is the usual choice for web APIs. XML wraps data in opening and closing tags, like HTML. It also has attributes, comments and namespaces. XML is still used for RSS feeds, SOAP services and office documents.
What are JSON and XML?
JSON stands for JavaScript Object Notation. It stores data as key–value pairs. A key is a name, and the value is the data that goes with it. Pairs are grouped inside curly braces. If you are new to it, start with What is JSON?
XML stands for Extensible Markup Language. It wraps each piece of data in tags. A tag is a name inside angle brackets, such as <title>. “Extensible” means you can invent your own tag names.
JSON vs XML syntax, side by side
Here is the same book written both ways. First in JSON:
{
"book": {
"id": 42,
"title": "Emma",
"author": "Jane Austen",
"tags": ["novel", "classic"],
"inPrint": true
}
}And now in XML:
<?xml version="1.0" encoding="UTF-8"?>
<book id="42">
<title>Emma</title>
<author>Jane Austen</author>
<tags>
<tag>novel</tag>
<tag>classic</tag>
</tags>
<inPrint>true</inPrint>
</book>A few things stand out:
- In XML, every value sits between an opening tag and a closing tag. JSON just writes the key once, then the value.
- The XML version puts
idin an attribute, which is extra information inside the opening tag. JSON has no attributes. Everything is a normal key. - JSON has a real list type, called an array, written with square brackets. XML has no arrays. You repeat the same tag instead.
- JSON knows basic types.
42is a number andtrueis a boolean (a true or false value). In XML, everything is text unless a separate schema (a file that describes the data) says otherwise. You can read more in JSON data types.
Which is smaller, JSON or XML?
JSON is usually smaller. XML repeats every tag name in the closing tag. In large files, this makes a big difference. Smaller files take less time to send over a network.
The difference gets smaller when the data is compressed (packed into fewer bytes), for example with gzip. Most web servers compress what they send. So in real use, the size difference is often smaller than it first looks.
Which is easier to read?
For simple records, most people find JSON easier. There are fewer symbols, and the structure is easy to see. If your JSON arrives as one long line, paste it into the JSON Formatter to add line breaks and indentation.
XML is better at documents that mix text and markup. This is called mixed content. For example:
<p>Please read <b>all</b> of the terms.</p>JSON has no natural way to write text with markup inside it. That is one reason why document formats are built on XML.
Attributes, comments and other XML features
Attributes
XML lets you choose between a child element (a tag inside another tag) and an attribute. For example, <book id="42"> or <book><id>42</id></book>. JSON only has keys, so there is one way to write it.
Comments
XML supports comments, written like <!-- note -->. Standard JSON has no comments at all. See JSON comments for the workarounds.
Namespaces
A namespace is a short prefix added to tag names. You need it when one file uses two sets of tag names. It stops the same name from meaning two things. JSON has nothing like this.
Schemas and queries
Both formats have tools to describe and search data. XML has XML Schema (XSD) and XPath. JSON has JSON Schema and JSONPath.
When is XML still used?
XML is older than JSON, and many formats are built on it. You will still meet XML in:
- RSS and Atom feeds for blogs and podcasts.
- SOAP web services, common in banking, government and older business systems.
- Office files.
.docx,.xlsxand.pptxfiles are ZIP archives full of XML files. - SVG images, which are written in XML.
- Sitemaps (
sitemap.xml) that tell search engines about your pages. - Some config files, such as Maven’s
pom.xmland Android layout files.
When should you use JSON?
For most new projects, JSON is the simpler choice. Use it when:
- You are building or calling a web API.
- Your data goes to JavaScript in a browser.
JSON.parseis built in, so there is nothing extra to install. - Your data is records and lists, not documents with mixed text.
- You want a small, simple format that every language can read.
Use XML when a standard or a partner system needs it. Also use it when you work with documents and need attributes, namespaces or mixed content.
How to convert between JSON and XML
There is no single official way to convert between the two, because XML has attributes and JSON has arrays. So a converter has to follow its own rules.
The JSON to XML converter turns keys into elements and arrays into repeated elements. Keys that start with @ become attributes, and a #text key sets the element’s text:
{ "price": { "@currency": "EUR", "#text": 9.5 } }
<price currency="EUR">9.5</price>The XML to JSON converter follows the same rules in reverse, so you can convert back and forth. Elements that appear more than once become arrays.
Watch out for one problem. If a tag appears only once, it may become a single value instead of an array. Your code then breaks when it expects a list. Turn on “Always use arrays” in the converter to get the same shape every time.
Common questions
Is JSON replacing XML?
For new web APIs, JSON is the most common choice. But XML is not going away. RSS, SVG, sitemaps and office files are all built on it.
Is JSON faster than XML?
JSON is often smaller and quicker to read, but it depends on the data and the program that reads it. For most apps, the difference is too small to notice.
Can JSON have attributes like XML?
No. In JSON you use a normal key instead. Converters often mark attributes with a prefix, such as @id, so they can turn them back into attributes later.
Does XML have data types?
Not on its own. Every value in XML is text. An XML Schema can set types, such as numbers and dates, for a program to check.