Imagine that your school has 500 students. Each student has a name, age, class, email address and examination result.
How can a computer organise all this information so that another application can understand it?
Computers need organised ways to store and exchange information. Two popular formats used for this purpose are JSON and XML.
In this module, you will learn what JSON and XML mean, how they work and how applications use them to exchange data.
A data format is a particular way of arranging information so that computers and people can understand it.
Think about your school examination result sheet.
Your school does not usually write all students' names, subjects and scores randomly. The information is organised into rows and columns.
This makes it easier for teachers and students to understand the information.
Computers also need organised formats when sharing information.
| Name | Age | Class | Score |
|---|---|---|---|
| Amina | 13 | JSS2 | 85 |
| David | 14 | JSS3 | 78 |
| Chinedu | 13 | JSS2 | 92 |
JSON and XML are two ways of organising information so that applications can exchange it.
JSON stands for JavaScript Object Notation.
It is a popular format used to organise and exchange information between applications.
JSON is easy to read and is commonly used by modern web APIs.
Imagine that you want to send information about a student to another application.
You can organise the information using JSON like this:
{
"name": "Amina",
"age": 13,
"class": "JSS2",
"school": "Bright Future Academy"
}
This JSON object contains four pieces of information about Amina.
Every JSON object contains information arranged using keys and values.
A key tells us what type of information is being stored. A value is the actual information.
{
"name": "Amina",
"age": 13,
"country": "Nigeria"
}
| Key | Value |
|---|---|
| name | Amina |
| age | 13 |
| country | Nigeria |
In JSON, keys must be written inside double quotation marks. Text values also use double quotation marks.
Numbers and certain other data types do not require quotation marks.
JSON supports different types of values.
A string is text written inside double quotation marks.
{
"name": "Amina"
}
Numbers are used for values such as age, price and examination scores.
{
"age": 13,
"score": 85
}
A Boolean value can only be true or false.
{
"is_student": true,
"is_suspended": false
}
An array is a collection of values stored inside square brackets.
Imagine that you want to store the names of three school subjects.
{
"subjects": [
"Mathematics",
"English",
"Computer Studies"
]
}
An object is a collection of related information stored inside curly brackets.
{
"student": {
"name": "Amina",
"age": 13,
"class": "JSS2"
}
}
Null means that a value is intentionally empty or unavailable.
{
"middle_name": null
}
XML stands for Extensible Markup Language.
It is another format used to organise, store and exchange data.
XML uses tags to describe information.
If you have studied HTML, you have already seen tags such as heading tags, paragraph tags and body tags.
XML also uses tags, but its main purpose is to describe and organise information rather than create the structure of a webpage.
<student>
<name>Amina</name>
<age>13</age>
<class>JSS2</class>
<school>Bright Future Academy</school>
</student>
The information above describes the same student we used in our earlier JSON example.
XML uses opening and closing tags to organise information.
Consider this example:
<name>Amina</name>
The parts mean:
<name> is the opening tag.Amina is the information being stored.</name> is the closing tag.The closing tag has a forward slash before the tag name.
Both JSON and XML help applications exchange information. However, they organise information differently.
| JSON | XML |
|---|---|
| Uses keys and values. | Uses opening and closing tags. |
| Uses curly brackets for objects. | Uses tags to organise information. |
| Uses square brackets for arrays. | Uses repeated elements for collections. |
| Often shorter and easier to read. | Can contain more descriptive markup. |
| Commonly used in modern web APIs. | Used in many document and data exchange systems. |
A banking application may use JSON to exchange information about transactions, account activities and payment results.
A school application can use JSON or XML to exchange student records, examination results and attendance information.
Shopping applications can exchange product names, prices, quantities and order information.
Many mobile applications communicate with online servers using APIs that send and receive JSON data.
Businesses can use JSON and XML to transfer information between different software systems.
Imagine that you are building a school application for students in Nigeria.
Your application needs to store information about one student.
Follow these instructions:
{
"name": "Chinedu",
"age": 13,
"class": "JSS2",
"country": "Nigeria",
"subjects": [
"Mathematics",
"English",
"Basic Science"
],
"is_active": true
}
Your challenge:
Now create the same student record using XML.
Instructions:
<?xml version="1.0" encoding="UTF-8"?>
<student>
<name>Chinedu</name>
<age>13</age>
<class>JSS2</class>
<country>Nigeria</country>
<subjects>
<subject>Mathematics</subject>
<subject>English</subject>
<subject>Basic Science</subject>
</subjects>
<is_active>true</is_active>
</student>
Your challenge:
Applications do not just store JSON information. They can also read and use it.
JavaScript has a built-in tool called JSON.parse() that converts JSON text into a JavaScript object.
const studentJSON = `{
"name": "Amina",
"age": 13,
"class": "JSS2"
}`;
const student = JSON.parse(studentJSON);
console.log(student.name);
console.log(student.age);
console.log(student.class);
There is also a method called JSON.stringify(). It converts a JavaScript object into JSON text.
const student = {
name: "Amina",
age: 13,
class: "JSS2"
};
const jsonText = JSON.stringify(student);
console.log(jsonText);
Beginners sometimes make mistakes when creating JSON and XML files. Let's look at some examples.
// Incorrect JSON
{
name: "Amina"
}
Correct version:
{
"name": "Amina"
}
// Incorrect JSON
{
"name": "Amina",
"age": 13,
}
Correct version:
{
"name": "Amina",
"age": 13
}
<student>
<name>Amina
</student>
Correct version:
<student>
<name>Amina</name>
</student>
In this module, you have learned that:
Answer the following questions to test what you have learned.
What does JSON stand for?
What does XML stand for?
Which symbols are used to define a JSON object?
What is a JSON key?
Which of the following is a Boolean value?
Which format uses opening and closing tags?
Which JavaScript method converts JSON text into a JavaScript value?
Which JavaScript method converts a JavaScript value into JSON text?
What is the purpose of an array in JSON?
Which of these statements is correct?
You have successfully completed Module 4 of Introduction to APIs for Data.
You now understand how JSON and XML organise information and how applications can use these formats to exchange data.