Table of contents
What is JSON
JSON is an abbreviation for JavaScript Object Notation. It is a Language Independent Lightweight format for storing and transporting data. JSON is often used to send data from a server to a web page. Although JSON is Language independent, its format is derived from Javascript object notation. It is Human-readable and writable. It is a lightweight text-based data interchange format, meaning it is simpler to read and write when compared to XML(Extensible Markup Language).
JSON Example
{
"Students":[
{"firstName": "Rahul", "Lastname": "B"},
{"firstName":"Anku", "Lastname":"Negi"},
]
}
JSON Syntax:
JSON Syntax is the same as Javascript object notation. Data is stored as "key/value pair". Refer above example.
Syntax Rule for JSON
- Data is in key/value pairs
- Data is separated by commas
- Square brackets hold arrays
- Curly braces hold objects
"Name ": "Rahul B"
# Note
Everything in JSON format is a string. that's why when we want to send a JSON file to the server we need to change our objects into strings stringify function and similarly, to use the JSON file as the object we need to use JSON. parse function. [ JSON does not allow function/methods]
const sendJSON = JSON.stringify(myObj);
console.log(sendJSON);
console.log(typeof sendJSON);
const receiveJSON = JSON.parse(sendJSON);
console.log(receiveJSON);
JSON VS XML
Both JSON and XML can be used to receive data from a web server.
XML ex:
<students>
<student>
<Name>Rahul </Name> <Roll_no>45</Roll_no>
</student>
<student>
<Name>Sati</Name> <Roll_no>46</Roll_no>
</student>
<student>
<Name>Ishu</Name> <Roll_no>66</Roll_no>
</student>
</students>;
JSON ex:
{"students":[
{ "Name":"Rahul", "Roll_no":"45" },
{ "Name":"Sati", "Roll_no":"46" },
{ "Name":"Ishu", "Roll_no":"66" }
]}
# Differences :
- Both are Human Readable Or self describing
- Both are hierarchical
- Both can be parsed and Used by several Programming languages
# Similarities :
- JSON is shorter
- JSON doesn't use end tag
- JSON is quicker to read and write
Advantages of JSON
- Its syntax is very small, easy, and light-weighted that’s the reason it executes and responds in a faster than XML .
- It has a wide range for browser support compatibility with the operating systems. It doesn’t require much effort to make it all browser compatible.
- On the server-side parsing is the most important part that developers want. If the parsing will be fast on the server side then the user can get a fast response, so in this case, JSON server-side parsing is the strong point compared to others.