You are on page 1of 18

JavaScript

Learning outcome1:

Use Fundamental Features of JavaScript

CHAP XII:
Use of JavaScript Libraries
Usage of AJAX and JSON
Table of Contents
1. Usage of ajax and json .................................................................................................................... 3
1.1. Introduction ............................................................................................................................ 3
1.1.1. How AJAX Works ............................................................................................................. 3
1.2. AJAX - Synchronous vs Asynchronous..................................................................................... 3
1.2.1. AJAX Synchronous (Old Approach): ................................................................................ 4
1.2.2. AJAX Asynchronous (Modern Approach using Fetch API): ............................................. 4
1.3. AJAX – Technologies ............................................................................................................... 5
1.3.1. JavaScript ........................................................................................................................ 5
1.3.2. HTML/XHTML and CSS .................................................................................................... 5
1.3.3. DOM ................................................................................................................................ 6
1.3.4. XMLHttpRequest ............................................................................................................. 6
1.3.5. XML or JSON .................................................................................................................... 6
1.4. AJAX – Action .......................................................................................................................... 6
1.4.1. A Client Event Occurs ...................................................................................................... 6
1.4.2. The XMLHttpRequest Object is Created ......................................................................... 7
1.4.3. The XMLHttpRequest Object is Configured .................................................................... 7
1.4.4. Making Asynchronous Request to the Webserver ......................................................... 8
1.4.5. Webserver Returns the Result Containing XML Document ............................................ 8
1.4.6. Callback Function processRequest() is Called ................................................................. 9
1.4.7. The HTML DOM is Updated ............................................................................................ 9
1.5. AJAX – XMLHttpRequest ....................................................................................................... 10
1.5.1. XMLHttpRequest Methods ........................................................................................... 10
1.5.2. XMLHttpRequest Properties ......................................................................................... 10
1.6. AJAX - Database Operations ................................................................................................. 11
1.6.1. Client Side HTML File .................................................................................................... 11
1.6.2. Server Side PHP File ...................................................................................................... 13
1.7. AJAX – Security ...................................................................................................................... 14
1.7.1. AJAX Security: Server Side............................................................................................. 14
1.7.2. AJAX Security: Client Side.............................................................................................. 14
1.8. AJAX – Issues ......................................................................................................................... 14
1.9. JSON vs XML .......................................................................................................................... 15
1.10. JSON vs BSON .................................................................................................................... 16
1.10.1. Difference between JSON vs BSON ............................................................................... 16
1. Usage of ajax and json

1.1. Introduction

 AJAX is not a programming language.


 AJAX is a technique for accessing web servers from a web page.
 AJAX stands for Asynchronous JavaScript And XML.
 AJAX just uses a combination of:
 A browser built-in XMLHttpRequest object (to request data from a web server)
 JavaScript and HTML DOM (to display or use the data)

AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind
the scenes. This means that it is possible to update parts of a web page, without reloading the
whole page.

1.1.1. How AJAX Works

1.2. AJAX - Synchronous vs Asynchronous

 Synchronous requests block the execution of the rest of the code until the response is
received. This means that the user will have to wait for the response before they can do
anything else on the page.
 Asynchronous requests, on the other hand, do not block the execution of the rest of the
code. This means that the user can continue to interact with the page while the request is
being processed.
1.2.1. AJAX Synchronous (Old Approach):

Synchronous AJAX is the traditional way of making AJAX requests where the browser blocks the
execution of JavaScript code until the response is received from the server.

It's not recommended to use synchronous AJAX in modern web development because it can cause
the UI to freeze and negatively affect the user experience.

<!DOCTYPE html>
<html>
<head>
<title>AJAX Synchronous vs Asynchronous</title>
</head>
<body>
<h1>Result:</h1>
<div id="result"></div>
<script>
const url = 'https://api.example.com/data'; // Replace with your
API endpoint
// AJAX Synchronous Request
const xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // false indicates a synchronous
request
xhr.send();
if (xhr.status === 200) {
document.getElementById('result').innerText = xhr.responseText;
}
</script>
</body>
</html>

1.2.2. AJAX Asynchronous (Modern Approach using Fetch API):

Asynchronous AJAX is the preferred approach in modern web development because it does not
block the execution of JavaScript code and allows the UI to remain responsive while the request is
being processed. The Fetch API is a modern replacement for the old XMLHttpRequest object,
providing a more straightforward and promise-based approach.

The asynchronous approach, the fetch () function returns a Promise, allowing us to use .then () and
.catch () to handle the response and any potential errors. This way, the rest of the JavaScript code
can continue executing without waiting for the server response. It results in a more efficient and
responsive user experience compared to the synchronous approach.
<!DOCTYPE html>
<html>
<head>
<title>AJAX Synchronous vs Asynchronous</title>
</head>
<body>
<h1>Result:</h1>
<div id="result"></div>
<script>
const url = 'https://api.example.com/data'; // Replace with your
API endpoint
// AJAX Asynchronous Request using Fetch API
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
document.getElementById('result').innerText = data;
})
.catch(error => {
console.error('Fetch Error:', error);
});
</script>
</body>
</html>

1.3. AJAX – Technologies

AJAX is a set of interactive and interrelated technologies that makes multitasking easier than
conventional technologies. AJAX technologies include -

1.3.1. JavaScript

AJAX heavily relies on JavaScript to make asynchronous requests to the server, handle responses,
and manipulate the Document Object Model (DOM) to update the content on the web page
dynamically.
Role in AJAX: JavaScript is the core scripting language that allows for the creation of dynamic and
interactive web applications. It is used to send asynchronous requests to the server, handle
responses, and update the user interface accordingly.
1.3.2. HTML/XHTML and CSS

These technologies are responsible for the presentation of the web content such as its style, font,
etc.

1.3.3. DOM

DOM is an API that displays web pages and interacts with data.

1.3.4. XMLHttpRequest

The XMLHttpRequest object is a browser feature that allows JavaScript to make HTTP requests to
the server asynchronously. It is the key component for making AJAX requests.

Role in AJAX: XMLHttpRequest is used to send HTTP requests to the server without requiring a page
reload. It can handle various types of data, not just XML, despite its name.

1.3.5. XML or JSON

XML is used for communicating to and from the server. It is being replaced by JSON or Javascript
Object Notation these days. JSON is lightweight and faster than XML.

1.4. AJAX – Action

This is AJAX-Action gives you a clear picture of the exact steps of AJAX operation.
Steps of AJAX Operation

 A client event occurs.


 An XMLHttpRequest object is created.
 The XMLHttpRequest object is configured.
 The XMLHttpRequest object makes an asynchronous request to the Webserver.
 The Webserver returns the result containing XML document.
 The XMLHttpRequest object calls the callback() function and processes the result.
 The HTML DOM is updated.

Let us take these steps one by one.

1.4.1. A Client Event Occurs

A JavaScript function is called as the result of an event.


Example − validateUserId() JavaScript function is mapped as an event handler to an onkeyup event
on input form field whose id is set to "userid"
<input type = "text" size = "20" id = "userid" name = "id" onkeyup = "validateUserId();">.

1.4.2. The XMLHttpRequest Object is Created

var ajaxRequest; // The variable that makes Ajax possible!


function ajaxFunction() {
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
}

1.4.3. The XMLHttpRequest Object is Configured

In this step, we will write a function that will be triggered by the client event and a callback function
processRequest() will be registered.

function validateUserId() {
ajaxFunction();

// Here processRequest() is the callback function.


ajaxRequest.onreadystatechange = processRequest;

if (!target) target = document.getElementById("userid");


var url = "validate?id=" + escape(target.value);

ajaxRequest.open("GET", url, true);


ajaxRequest.send(null);
}
1.4.4. Making Asynchronous Request to the Webserver

Source code is available in the above piece of code. Code written in bold typeface is responsible to
make a request to the webserver. This is all being done using the XMLHttpRequest object
ajaxRequest.

function validateUserId() {
ajaxFunction();

// Here processRequest() is the callback function.


ajaxRequest.onreadystatechange = processRequest;

if (!target) target = document.getElementById("userid");


var url = "validate?id = " + escape(target.value);

ajaxRequest.open("GET", url, true);


ajaxRequest.send(null);
}

Assume you enter Zara in the userid box, then in the above request, the URL is set to "validate?id =
Zara".

1.4.5. Webserver Returns the Result Containing XML Document

You can implement your server-side script in any language, however its logic should be as follows.

 Get a request from the client.


 Parse the input from the client.
 Do required processing.
 Send the output to the client.

If we assume that you are going to write a servlet, then here is the piece of code.

public void doGet(HttpServletRequest request,


HttpServletResponse response) throws IOException, ServletException {
String targetId = request.getParameter("id");

if ((targetId != null) && !accounts.containsKey(targetId.trim())) {


response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid>true</valid>");
} else {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<valid>false</valid>");
}
}
1.4.6. Callback Function processRequest() is Called

The XMLHttpRequest object was configured to call the processRequest() function when there is a
state change to the readyState of the XMLHttpRequest object. Now this function will receive the
result from the server and will do the required processing. As in the following example, it sets a
variable message on true or false based on the returned value from the Webserver.

function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
var message = ...;
...
}

1.4.7. The HTML DOM is Updated

This is the final step and in this step, your HTML page will be updated. It happens in the following
way −

 JavaScript gets a reference to any element in a page using DOM API.


 The recommended way to gain a reference to an element is to call.
 JavaScript may now be used to modify the element's attributes; modify the element's style
properties; or add, remove, or modify the child elements. Here is an example −

<script type = "text/javascript">


function setMessageUsingDOM(message) {
var userMessageElement = document.getElementById("userIdMessage");
var messageText;
if (message == "false") {
userMessageElement.style.color = "red";
messageText = "Invalid User Id";
} else {
userMessageElement.style.color = "green";
messageText = "Valid User Id";
}
var messageBody = document.createTextNode(messageText);
// if the messageBody element has been created simple
// replace it otherwise append the new element
if (userMessageElement.childNodes[0]) {
userMessageElement.replaceChild(messageBody,
userMessageElement.childNodes[0]);
} else {
userMessageElement.appendChild(messageBody);
}
}
</script>
<body>
<div id = "userIdMessage"><div>
</body>
1.5. AJAX – XMLHttpRequest

The XMLHttpRequest object, pivotal for AJAX, emerged with Internet Explorer 5.5 in 2000 but gained
prominence with AJAX's rise in 2005. It's an API enabling JavaScript and other scripting languages to
exchange XML data via HTTP, linking webpage client-side and server-side. It handles various data
formats, like JSON.

Listed below are some of the methods and properties that you have to get familiar with.

1.5.1. XMLHttpRequest Methods

 abort(): Cancels the current request.


 getAllResponseHeaders():Returns the complete set of HTTP headers as a string.
 getResponseHeader( headerName ):Returns the value of the specified HTTP header.
 open( method, URL )
 open( method, URL, async )
 open( method, URL, async, userName )
 open( method, URL, async, userName, password ): Specifies the method, URL, and other
optional attributes of a request.

The method parameter can have a value of "GET", "POST", or "HEAD". Other HTTP methods
such as "PUT" and "DELETE" (primarily used in REST applications) may be possible.

The "async" parameter specifies whether the request should be handled asynchronously or
not. "true" means that the script processing carries on after the send() method without
waiting for a response, and "false" means that the script waits for a response before
continuing script processing.

 send( content ): Sends the request.


 setRequestHeader( label, value ): Adds a label/value pair to the HTTP header to be sent.

1.5.2. XMLHttpRequest Properties

 Onreadystatechange: An event handler for an event that fires at every state change.
 readyState:The readyState property defines the current state of the XMLHttpRequest
object.

The following table provides a list of the possible values for the readyState property –

State Description
0 The request is not initialized.
1 The request has been set up.
2 The request has been sent.
3 The request is in process.
4 The request is completed.
readyState = 0 After you have created the XMLHttpRequest object, but before you have called the
open() method.
readyState = 1 After you have called the open() method, but before you have called send().
readyState = 2 After you have called send().
readyState = 3 After the browser has established a communication with the server, but before the
server has completed the response.
readyState = 4 After the request has been completed, and the response data has been completely
received from the server.
 responseText
Returns the response as a string.
 responseXML
Returns the response as XML. This property returns an XML document object, which can be
examined and parsed using the W3C DOM node tree methods and properties.
 status
Returns the status as a number (e.g., 404 for "Not Found" and 200 for "OK").
 statusText
Returns the status as a string (e.g., "Not Found" or "OK").

1.6. AJAX - Database Operations

To clearly illustrate how easy it is to access information from a database using AJAX, we are going to
build MySQL queries on the fly and display the results on "ajax.html". But before we proceed, let us
do the ground work. Create a table using the following command.

NOTE − We are assuming you have sufficient privilege to perform the following MySQL operations.

CREATE TABLE 'ajax_example' (


'name' varchar(50) NOT NULL,
'age' int(11) NOT NULL,
'sex' varchar(1) NOT NULL,
'wpm' int(11) NOT NULL,
PRIMARY KEY ('name')
);
INSERT INTO 'ajax_example' VALUES ('Jerry', 120, 'm', 20);
INSERT INTO 'ajax_example' VALUES ('Regis', 75, 'm', 44);
INSERT INTO 'ajax_example' VALUES ('Frank', 45, 'm', 87);
INSERT INTO 'ajax_example' VALUES ('Jill', 22, 'f', 72);
INSERT INTO 'ajax_example' VALUES ('Tracy', 27, 'f', 0);
INSERT INTO 'ajax_example' VALUES ('Julie', 35, 'f', 90);

1.6.1. Client Side HTML File

Now let us have our client side HTML file, which is ajax.html, and it will have the following code −
<html>
<body>
<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction() {
var ajaxRequest; // The variable that makes Ajax possible!
try {
ajaxRequest = new XMLHttpRequest();// Opera 8.0+, Firefox, Safari
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser broke!");// Something went wrong
return false;
}
}
}
// Create a function that will receive data sent from the server and will update div
section in the same page.
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4) {
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to server script.
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;
var queryString = "?age = " + age ;
queryString += "&wpm = " + wpm + "&sex = " + sex;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' /> <br />
Sex:
<select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
1.6.2. Server Side PHP File

Your client-side script is ready. Now, we have to write our server-side script, which will fetch age,
wpm, and sex from the database and will send it back to the client. Put the following code into the
file "ajax-example.php".

<?php
//Connect to MySQL Server
mysql_connect("localhost", "dbusername", "dbpassword");
//Select Database
mysql_select_db("dbname") or die(mysql_error());
// Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
//build query
$query = "SELECT * FROM ajax_example WHERE sex = '$sex'";
if(is_numeric($age))
$query .= " AND age <= $age";

if(is_numeric($wpm))
$query .= " AND wpm <= $wpm";

//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table> <tr> <th>Name</th> <th>Age</th> <th>Sex</th>
<th>WPM</th> </tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)) {
$display_string .= "<tr> <td>$row[name]</td> <td>$row[age]</td>
<td>$row[sex]</td> <td>$row[wpm]</td> </tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>

If you have successfully completed this lesson, then you know how to use MySQL, PHP, HTML, and
Javascript in tandem to write AJAX applications.
1.7. AJAX – Security

1.7.1. AJAX Security: Server Side

AJAX-based Web applications use the same server-side security schemes of regular Web
applications. You specify authentication, authorization, and data protection requirements in your
web.xml file (declarative) or in your program (programmatic).

AJAX-based Web applications are subject to the same security threats as regular Web applications.

1.7.2. AJAX Security: Client Side

JavaScript code is visible to a user/hacker. Hacker can use JavaScript code for inferring server-side
weaknesses. JavaScript code is downloaded from the server and executed at the client and can
compromise the client by mal-intended code.
Downloaded JavaScript code is constrained by the sand-box security model and can be relaxed for
signed JavaScript.

1.8. AJAX – Issues

AJAX is growing very fast and that is the reason that it contains many issues with it. We hope with the
passes of time, they will be resolved and AJAX will become ideal for web applications. We are listing
down a few issues that AJAX currently suffers from.
Complexity is increased
 Server-side developers will need to understand that presentation logic will be required in the
HTML client pages as well as in the server-side logic.
 Page developers must have JavaScript technology skills.
AJAX-based applications can be difficult to debug, test, and maintain

 JavaScript is hard to test - automatic testing is hard.


 Weak modularity in JavaScript.
 Lack of design patterns or best practice guidelines yet.
Toolkits/Frameworks are not mature yet

 Most of them are in beta phase.


No standardization of the XMLHttpRequest yet

 Future version of IE will address this.


No support of XMLHttpRequest in old browsers

 Iframe will help.


JavaScript technology dependency and incompatibility

 Must be enabled for applications to function.


 Still some browser incompatibilities exist.
JavaScript code is visible to a hacker

 Poorly designed JavaScript code can invite security problems.

1.9. JSON vs XML

Both JSON and XML can be used to receive data from a web server. The following JSON and XML
examples both define an employees object, with an array of 3 employees:

JSON XML
{"employees":[ <employees>
{ "firstName":"John", "lastName" <employee>
:"Doe" }, <firstName>John</firstName> <lastName>Doe
{ "firstName":"Anna", "lastName" </lastName>
:"Smith" }, </employee>
{ "firstName":"Peter", "lastName <employee>
":"Jones" } <firstName>Anna</firstName> <lastName>Smi
]} th</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jo
nes</lastName>
</employee>
</employees>

JSON is Like XML Because


 Both JSON and XML are "self describing" (human readable)
 Both JSON and XML are hierarchical (values within values)
 Both JSON and XML can be parsed and used by lots of programming languages
 Both JSON and XML can be fetched with an XMLHttpRequest

JSON is Unlike XML Because

 JSON doesn't use end tag


 JSON is shorter
 JSON is quicker to read and write
 JSON can use arrays

The biggest difference is:

XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.

Why JSON is Better Than XML

 XML is much more difficult to parse than JSON.


 JSON is parsed into a ready-to-use JavaScript object.
For AJAX applications, JSON is faster and easier than XML:

Using XML

 Fetch an XML document


 Use the XML DOM to loop through the document
 Extract values and store in variables

Using JSON

 Fetch a JSON string


 JSON.Parse the JSON string

1.10. JSON vs BSON

BSON is just binary JSON (a superset of JSON with some more data types, most importantly binary
byte array). It is a serialization format used in MongoDB. BSON may be an organization specializing in
effectively putting away of JSON-like archives, which are close to supporting the conventional JSON
information sorts it moreover underpins dates and parallel information natively. It is twofold
organized, so it is not human-readable like JSON. Equivalent BSON record isn’t continuously littler
than JSON, but it allows you to effectively skip the records that you’re not fascinated with when
perusing it, whereas with JSON you’d have to be parsed each byte. Usually, the fundamental reason
why it is utilized inside MongoDB. If you’re not working with MongoDB, and don’t require any of the
highlights then you’re most likely way better off utilizing JSON. In a single MongoDB document, you
can store up to 16MB of binary data. However, MongoDB has its own file system GridFS, which
stores binary files larger than 16MB in chunks.

JSON is a text-based, human-readable information compatibility organization utilized for speaking to


basic information structures and objects in web browser-based code. JSON is additionally in some
cases utilized in desktop and server-side programming situations. JSON was initially based on the
JavaScript programming dialect and was presented as the page scripting dialect for the Netscape
Pilot Web browser. JSON may be a language structure for serializing objects, clusters, numbers,
strings, booleans, and invalid. It is based upon JavaScript language structure but is unmistakable
from it: a few JavaScript isn’t JSON, and a few JSON isn’t JavaScript. You will change over any JSON
gotten from the server into JavaScript objects. It makes a difference in working with the information
as JavaScript objects, with no complicated parsing and translations. Now simply know what JSON is,
let us move ahead and see why we get to utilize it and what the distinctive focal points of utilizing
JSON are.

1.10.1. Difference between JSON vs BSON

JSON BSON
Standard file format Type. Binary file format Type.
The language-free organization is Binary JSON comprises a list of requested
utilized for offbeat server browser components containing a field title, sort, and
communication. esteem. Field title sorts are ordinarily a
string.
Broadly JSON comprises of question The parallel encoding method comprises
and cluster where the question could extra data such as lengths of strings and the
be a collection of key-value sets and protest subtypes. In addition, BinData and
the cluster is a requested list of values. Date information sorts are the information
sorts that are not upheld in JSON.
JSON stands for JavaScript Object BSON stands for Binary JavaScript Object
Notation. Notation.
JSON data contains its data basic in BSON gives extra datatypes over the JSON
JSON format. data.
Database like AnyDB, redis, etc stores MongoDB stores data in BSON format.
information in JSON format.
JSON uses less space in comparison to BSON uses more space as compared to JSON.
BSON.
It is slow as compared to BSON. It is faster than JSON.
It is used for the transmission of data. It is used for the storage of the data.
It has no encoding and decoding It has encoding and decoding technique.
technique.

1.11. Exercises

1. What is the main difference between synchronous and asynchronous AJAX requests?

Synchronous AJAX requests block the user interface until the request is complete, causing potential
delays. Asynchronous AJAX requests allow the user interface to remain responsive while the request
is being processed, enhancing user experience.

2. Explain what happens when an AJAX request is made in a web application.

Answer: When an AJAX request is made, the web application can communicate with the server in
the background without requiring a full-page refresh. This allows for dynamic updates to the content
of a webpage.

3. What is the XMLHttpRequest object used for in AJAX?


The XMLHttpRequest object is used to make asynchronous requests to a server from a web page. It
enables the browser to send and receive data without requiring a full page reload.

4. Provide an example of a database operation that can be performed using AJAX.

One example of a database operation using AJAX is submitting a form to insert new data into a
database table without reloading the entire page.

5. What security concerns should developers address when using AJAX?


Developers should consider implementing measures like Cross-Origin Resource Sharing (CORS) to
control which domains can access their resources. They should also guard against cross-site scripting
(XSS) attacks by sanitizing and validating user input.

6. Compare JSON and XML in terms of readability and verbosity.


JSON is generally more readable and less verbose than XML due to its simple and compact syntax.
7. Describe the basic structure of JSON.

JSON consists of key-value pairs enclosed in curly braces. Each key is a string, followed by a colon,
and then a value. Multiple key-value pairs are separated by commas.
8. How does BSON differ from JSON?
BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents. It is designed for more
efficient storage and data exchange in certain databases and applications compared to the text-
based JSON format.

You might also like