JSON And XML. API’s Data Interchange Formats

Eugene Bartenev
4 min readMay 1, 2023

--

In today’s interconnected digital landscape, APIs and data interchange formats are pivotal in communication between different systems and applications.

This article delves into Application Programming Interfaces (APIs) and the various data interchange formats. We will look at and compare the two most popular data interchange formats — JSON and XML.

Let’s get it started!

What Is An Interface?

An interface is an intermediary where two systems meet and interact. The volume knob of a radio receiver is a volume control interface. It acts as an intermediary between two systems: a human and an electronic receiver.

The graphical user interface (GUI) of an operating system is another example of an interface. It displays buttons on a screen, letting users complete tasks without knowing the system’s inner workings.

When you drag a file icon to the recycle bin icon to delete it, the actual change happens in the file system. However, the action of moving an image on the screen is simply an intermediary that hides the behind-the-scenes process from the user.

An API (Application Programming Interface) is an interface for data exchange. The word “programming” means that APIs are primarily used for interaction between programs — it’s not the developers themselves who interact with the system, but their code.

APIs can communicate with each other. You might have code that requests data from the Amazon API about a particular product’s availability, and Amazon needs to call the APIs of various stores to get the data about that product.

A program that sends a request to an API is called a client. A client can be code on the server, a mobile application, a test program, or even a regular web browser.

Each service can be written in its own programming language, but thanks to APIs, services can easily communicate with each other over the network. Then a communication protocol (usually HTTP) transmits data in a format convenient both for humans and machines.

JSON

One of the most common data interchange formats is JSON.

JSON stands for JavaScript Object Notation. This format was derived from the JavaScript programming language. It’s easier to read and is a little less bulky than XML for the same amount of data. The JSON structure is very similar to the dict data type — it’s a sequence of key/value pairs that supports nesting. But JSON is more standardized. Dictionary keys in JSON can be written only in double quotes, to give just one example.

Key values can be:

  • Strings
  • Numbers
  • Boolean values
  • Lists

Let’s consider a superhero dossier in JSON:

[
{
"name": "Captain America",
"realName": "Steve Rogers",
"yearCreated": 1941,
"powers": [
"Strength",
"Healing ability"
]
}
]

A dossier on several superheroes in JSON may look something like this:

[
{
"name": "Captain America",
"realName": "Steve Rogers",
"yearCreated": 1941,
"powers": [
"Strength",
"Healing ability"
]
},
{
"name": "Spider-Man",
"realName": "Peter Parker",
"yearCreated": 1963,
"powers": [
"Danger sense",
"Speed",
"Jumping"
]
}
]

Some data types in JSON are not formalized, e.g. there is no special date format for them, meaning you can pass data in any format that’s convenient. So you can pass a birth date as a string “09–10–1988” or as an number 1623799668.

You can learn more about JSON in the documentation.

It may be difficult to find errors in JSON with the naked eye since many JSON objects can be nested inside one another. To make sure your JSON is correctly composed, you can use an online validator like this one.

XML

JSON is popular, but it’s not the only format for transferring data.

XML (eXtensible Markup Language) is also very popular and widely used in software development. This language looks somewhat similar to HTML, and that is no coincidence — HTML is a direct descendant of XML. This language looks somewhat similar to HTML, and that is no coincidence — HTML is a direct descendant of XML.

One of the main differences between HTML and XML is that XML tag names are not standardized — you can name them whatever you want. Moreover, these languages serve different purposes. HTML tags serve to display information in the browser and XML tags simply structure the transmitted information.

But XML syntax is similar to HTML — tags are written inside angle brackets, there is an opening tag and (in most cases) it must be paired with a closing tag.

Here is the same superhero record from earlier in XML format:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<superhero>
<name>Captain America</name>
<realName>Steve Rogers</realName>
<yearCreated>1941</yearCreated>
<powers>Strength</powers>
<powers>Healing ability</powers>
</superhero>
<superhero>
<name>Spider-Man</name>
<realName>Peter Parker</realName>
<yearCreated>1963</yearCreated>
<powers>Danger sense</powers>
<powers>Speed</powers>
<powers>Jumping</powers>
</superhero>
</root>

You can check the validity of XML using the validator on codebeautify.org or any other online validator.

You can read more about XML in the documentation.

Which Format Is Better?

APIs can use JSON, XML, YAML, or any other text-based format that supports data structuring. Developers often include support for several data exchange formats in their APIs.

JSON is one of the most popular data interchange formats for working with REST APIs. It’s easier to read and takes up less space than XML for the same amount of data.

Moreover, JSON is supported by all popular programming languages. In APIs based on REST architecture, this format is actually a standard.

--

--

Eugene Bartenev

Author, Tech Editor and Cloud Solution Architect (Developer Advocate)