In the last session we had an introduction to XML Fundamentals. Like XML, JSON is also used to store a structured data. So today we will have an introduction to JSON Fundamentals.
- What is JSON?
- JSON stands for Javascript Object Notation.
- It is a lightweight data interchange format.
- It is easy for humans to read and write.
- It is a text based open standard, designed for human readable data. It is readable only if it is formatted properly.
- It uses name/value pairs to store data.
- It has same value types as javascript.
- It works like XML and is an alternative to XML.
- It is not possible to decide which one is better, XML or JSON; because there are certain areas where XML is a better choice and certain other areas where JSON is a better choice.
- JSON Syntax
- In JSON all data is formatted in name/value pairs.
- Example of a name/value pair is as shown below:
- In the above example : firstname is the name, then we have a colon and then the value Brad. The name and value must be enclosed in the double quotes.
- If you have more data i.e. more than one name/value pairs, they are separated using comma. Eg: “firstname” : “Brad” , “lastname” : “Dixon”.
- JSON objects are contained in curly braces ({}).
- JSON arrays are contained in brackets ([]).
- JSON files have a .json extension.
- The MIME type is “application/json”.
- JSON Object
- Here, we have shown an example of JSON Object for a user with 3 properties:
Let’s understand the syntax of JSON in this JSON Fundamentals tutorial.
“firstname” : “Brad”
{ “firstName” : “Brad” , “lastName” : “Traversy” , “email” : “[email protected]” }
- An example of array is shown below:
{ “pets” : [ {“name” : “jack” , “animal” : “dog”}, {“name” : “john” , “animal” : “cat”}, {“name” : “joe” , “animal” : “fish”} ] }
- JSON:
- JSON is more lightweight than XML.
- It is more simple.
- Since JSON is lightweight, it is more faster than XML.
- It is a little easier to read.
- JSON is native to javascript.
- XML:
- XML is more extensible.
- It is more flixible.
- It is more complicated compared to JSON.
- It offers more compatibility between systems.
JSON is generally preffered over XML on most of the occasions.
This was all about JSON Fundamentals.