Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to APIs for Data ยท Lesson 5 of 9

Module Four

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
5
Module 4 - Introduction to JSON and XML

MODULE 4: Introduction to JSON and XML Data Formats

Welcome to Module 4!

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.

4.1 What Is a Data Format?

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.

Example of Organised Student 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.

4.2 What Is JSON?

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.

  • name: The student's name.
  • age: The student's age.
  • class: The student's school class.
  • school: The name of the school.
Remember: JSON allows applications to exchange information in an organised and readable format.

4.3 Understanding JSON Keys and Values

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.

4.4 Different Types of JSON Values

JSON supports different types of values.

1. String

A string is text written inside double quotation marks.

{
    "name": "Amina"
}

2. Number

Numbers are used for values such as age, price and examination scores.

{
    "age": 13,
    "score": 85
}

3. Boolean

A Boolean value can only be true or false.

{
    "is_student": true,
    "is_suspended": false
}

4. Array

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"
    ]
}

5. Object

An object is a collection of related information stored inside curly brackets.

{
    "student": {
        "name": "Amina",
        "age": 13,
        "class": "JSS2"
    }
}

6. Null

Null means that a value is intentionally empty or unavailable.

{
    "middle_name": null
}

4.5 What Is XML?

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.

4.6 Understanding XML Tags

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.

Important: XML tags must be properly opened and closed. They must also be nested correctly.

4.7 JSON vs XML: What Is the Difference?

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.

4.8 Real-Life Applications of JSON and XML

1. Banking Applications

A banking application may use JSON to exchange information about transactions, account activities and payment results.

2. School Management Systems

A school application can use JSON or XML to exchange student records, examination results and attendance information.

3. Online Shopping

Shopping applications can exchange product names, prices, quantities and order information.

4. Mobile Applications

Many mobile applications communicate with online servers using APIs that send and receive JSON data.

5. Business Software

Businesses can use JSON and XML to transfer information between different software systems.

4.9 Practical Lab: Create Your First JSON Record

Lab Title: Create a Student Record Using JSON

Imagine that you are building a school application for students in Nigeria.

Your application needs to store information about one student.

Follow these instructions:

  1. Open a text editor such as Notepad or VS Code.
  2. Create a new file.
  3. Name the file student.json.
  4. Enter the following information.
  5. Save the file.
{
    "name": "Chinedu",
    "age": 13,
    "class": "JSS2",
    "country": "Nigeria",
    "subjects": [
        "Mathematics",
        "English",
        "Basic Science"
    ],
    "is_active": true
}

Your challenge:

  1. Change the student's name to your preferred name.
  2. Change the age.
  3. Add another school subject.
  4. Change the country if you want to use another example.
  5. Set is_active to false and observe the difference.

4.10 Practical Lab: Create an XML Student Record

Lab Title: Create Your First XML Document

Now create the same student record using XML.

Instructions:

  1. Open your text editor.
  2. Create a new file.
  3. Name the file student.xml.
  4. Enter the information below.
  5. Save your file.
<?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:

  1. Change the student's name.
  2. Change the student's age.
  3. Add another subject.
  4. Change the class.
  5. Make sure every opening tag has a matching closing tag.

4.11 How Applications Read JSON Data

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);

What Does This Code Do?

  • studentJSON: Contains JSON information as text.
  • JSON.parse(): Converts the JSON text into a JavaScript object.
  • student.name: Retrieves the student's name.
  • student.age: Retrieves the student's age.
  • student.class: Retrieves the student's 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);
Remember: JSON.parse() reads JSON text and converts it into a JavaScript value. JSON.stringify() converts a JavaScript value into JSON text.

4.12 Common JSON and XML Mistakes

Beginners sometimes make mistakes when creating JSON and XML files. Let's look at some examples.

JSON Mistake 1: Missing Double Quotation Marks

// Incorrect JSON
{
    name: "Amina"
}

Correct version:

{
    "name": "Amina"
}

JSON Mistake 2: Adding an Extra Comma

// Incorrect JSON
{
    "name": "Amina",
    "age": 13,
}

Correct version:

{
    "name": "Amina",
    "age": 13
}

XML Mistake: Forgetting a Closing Tag

<student>
    <name>Amina
</student>

Correct version:

<student>
    <name>Amina</name>
</student>

4.13 Module Summary

In this module, you have learned that:

  • A data format is a way of organising information.
  • JSON means JavaScript Object Notation.
  • XML means Extensible Markup Language.
  • JSON uses keys and values to organise information.
  • XML uses opening and closing tags.
  • JSON supports strings, numbers, Boolean values, arrays, objects and null.
  • Applications commonly use JSON and XML to exchange data.
  • JSON.parse() converts JSON text into a JavaScript value.
  • JSON.stringify() converts a JavaScript value into JSON text.
Key takeaway: JSON and XML are two different ways of organising information so that computer applications can understand and exchange it.

MODULE 4: KNOWLEDGE ASSESSMENT

Answer the following questions to test what you have learned.

Question 1

What does JSON stand for?

  • A. JavaScript Object Notation
  • B. Java System Online Network
  • C. JavaScript Operating Number
  • D. Joint Software Object Name

Question 2

What does XML stand for?

  • A. Extra Machine Language
  • B. Extensible Markup Language
  • C. External Management Link
  • D. Extended Machine Logic

Question 3

Which symbols are used to define a JSON object?

  • A. Square brackets []
  • B. Parentheses ()
  • C. Curly brackets {}
  • D. Angle brackets <>

Question 4

What is a JSON key?

  • A. A computer password
  • B. A label that identifies a value
  • C. A computer keyboard
  • D. A website address

Question 5

Which of the following is a Boolean value?

  • A. "Amina"
  • B. 13
  • C. true
  • D. null

Question 6

Which format uses opening and closing tags?

  • A. JSON
  • B. XML
  • C. CSV
  • D. TXT

Question 7

Which JavaScript method converts JSON text into a JavaScript value?

  • A. JSON.parse()
  • B. JSON.remove()
  • C. JSON.delete()
  • D. JSON.print()

Question 8

Which JavaScript method converts a JavaScript value into JSON text?

  • A. JSON.read()
  • B. JSON.stringify()
  • C. JSON.open()
  • D. JSON.create()

Question 9

What is the purpose of an array in JSON?

  • A. To store a collection of values
  • B. To delete information
  • C. To shut down a server
  • D. To create a computer network

Question 10

Which of these statements is correct?

  • A. JSON and XML are only used for designing websites.
  • B. JSON and XML help organise and exchange data.
  • C. XML cannot contain text.
  • D. JSON cannot store numbers.

Congratulations!

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.

Coming Up in Module 5: Working with REST APIs and API Documentation. You will discover what REST APIs are, how they work and how developers use API documentation to connect applications.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’