JSON format is very much English like. I can be termed as "self-describing" and it is very easy and intuitive to understand. JSON is easy to read and write than XML format. JSON is not dependent on the calling programming language and it supports array, object, string, number and values. JSON is designed for future wherein machines can parse and generate code. It is heavily influenced by JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999. Although JSON is a completely language independent format but it uses conventions that commonly used in the C-family of languages i.e. C, C++, Python, Java etc.
JSON structure has two components:
- Multiple instances of name/value pairs 🡪 this can be an object, record, struct, dictionary, hash table, keyed list, or associative array.
- A value list that is ordered 🡪 this can be an array, vector, list, or sequence.
These are universal data structures commonly adopted for data exchange. JSON is used in almost all places where data exchange is needed between two modules, be it JavaScript based applications that includes browser extensions and websites, be it Rest API calls, be it Flask based Python applications or be in Java based micro service.
JSON format is used to serialize and transfer structured data over network connection from one machine to another in a truly portable way. For e.g. JSON is used to transmit data between a server and web applications. Some common characteristics of JSON are:
-
JSON is easy to read and write.
-
It is a lightweight text-based interchange format.
-
JSON is language independent.
-
JSON is portable.
-
JSON is standardized.
Let us see an example of a JSON object. The following representation shows JSON object to store an employee record
{
"employee": [
{
"id": 55027103,
"name": "Harish Jain",
"email": "harish.jain@xxx.com",
"department": "Cloud Engineering",
"base location": "Gurugram",
"Date of joining": "2014-05-11T09:28:56.321-05:30",
"position": "Senior Manager",
"supervisor": "Ajay Makhen",
"hod": "Sanjeev Puri"
},
{
"id": 55026900,
"name": "Ritu Mittal",
"email": "ritu.mittal@xxx.com",
"department": "Cloud Engineering",
"base location": "Gurugram",
"Date of joining": "2019-05-11T09:28:56.321-05:30",
"position": "Engineer",
"supervisor": "Harish Jain",
"hod": "Sanjeev Puri"
}
]
}
This JSON object has a list of 2 records. Each record has two values separated by a colon. The value in the left column is the key (id, name etc.) and the value in the right column is the key's value (5502710, Harish Jain etc.). The values are written within double quotes “..” are of type String.
Thus in JSON all keys must be strings. Further you add a new record after putting comma in the end of the last record. Also, there’s a comma after each key-value pair except for the last one, indicating the end of the current record. Let us make a simple webpage for this JSON and see how it looks. Save the following code in a file namesjson.html and open the file in Google chrome.
<html>
<head>
<title>Employee Record</title>
<script language = "javascript" >
var object1 =
{
"id": 55027103,
"name": "Harish Jain",
"email": "harish.jain@xxx.com",
"department": "Cloud Engineering",
"base location": "Gurugram",
"Date of joining": "2014-05-11T09:28:56.321-05:30",
"position": "Senior Manager",
"supervisor": "Ajay Makhen",
"hod": "Sanjeev Puri"};
document.write("<h1>Employee Record using JSON</h1>");
document.write("<br>");
document.write("<h2>Employee Record 1</h2>");
document.write("<h3>Name = " + object1.name+"</h3>");
document.write("<h3>Id = " + object1.id+"</h3>");
document.write("<h3>E-mail = " + object1.name+"</h3>");
document.write("<h3>Department = " + object1.id+"</h3>");
document.write("<h3>Base Location = " + object1.name+"</h3>");
document.write("<br>");
document.write("<br>");
var object2 =
{
"id": 55026900,
"name": "Ritu Mittal",
"email": "ritu.mittal@xxx.com",
"department": "Cloud Engineering",
"base location": "Gurugram",
"Date of joining": "2019-05-11T09:28:56.321-05:30",
"position": "Engineer",
"supervisor": "Harish Jain",
"hod": "Sanjeev Puri"
};
document.write("<h2>Employee Record 2</h2>");
document.write("<h3>Name = " + object2.name+"</h3>");
document.write("<h3>Id = " + object2.id+"</h3>");
document.write("<h3>E-mail = " + object2.name+"</h3>");
document.write("<h3>Department = " + object2.id+"</h3>");
document.write("<h3>Base Location = " + object2.name+"</h3>");
</script>
</head>
</html>
You should be getting an output like this.