Now a days, JSON has become a hot topic. Developers today are really getting interesting in JSON.
So, in this post I am going to explain what is JSON.
JSON : JavaScript Object Notation is a technology / format to store the data in key - value pairs i.e. the large amount of data can be stored / transmit using a JSON.
Like for example : For a php developer, it is common to create the AJAX backend page and to send the data back to main page. Previously it was kind of tedious as user needs to create an array and needs to handle everything by himself / herself. But with JSON, it becomes very easy. JUST CREATE A JSON OBJECT containing the data, pass it to main page and use it.. simple.
and this is just a very basic example.
JSON is widely used in all Documents-Oriented Databases (like MongoDB[http://learnandsharetoall.blogspot.in/2014/01/what-is-mongodb.html], Hadoop, NoSQL etc.).
So JSON's application is very very much extended.
Now to understand JSON, lets divide it in four parts.
a) Basic Structure
b) Value Types
c) Complex Structures
So Basic Structure, a basic JSON script contains a "KEY-VALUE" pair to represent the data.
for example
{
"name" : "test",
"country" : "India"
}
So above is the basic JSON script.
Starting and ending the script with curly braces { and } and then key-value pairs separated by comma
Above name and country is key with values test and India.
So this is basic JSON structure, how a basic JSON looks like.
Moving on to value types , i.e. what are the types of values it can store.
So consider the below JSON,
{
"name " : "Test1",
"Age " : 22,
"isMarried" : false
}
So the above JSON has 3 value pairs, name with String value, Age with Integer value and isMarried with boolean values.
So as we have seen, the JSON can store a wide variety of data.
Even the key in JSON can serve as individual JSON Script and can also hold array of objects.(described in complex structure part)
Moving on to Complex Structures,
There can be two types of complex structures as told above.
a) Keys containing array of objects
b) Keys containing another JSON script(nested JSON)
Keys containing Array
Consider a JSON,
{
"name " : "test1",
" country " : "india",
"hobbies " : ["Music","Reading" , "Blah" , " Blah"]
}
So here a key hobbies containing an array of objects.So this, can be used to store the array of values.This can be really useful in real world applications.
Keys containing another JSON script(nested JSON)
Consider a JSON,
{
" name " : "test1",
"address" : {
"city" : "Noida",
"country" : "India"
}
"DOB" : "28-feb"
}
So in above script,
address is the key which itself is the json object.
You can use these as
var s = {
"name" : "test1",
"address" : {
"city" : "Noida",
"country" : "India"
},
"DOB" : "28-feb"
}
console.log(s.address); // This will print the address JSON
To get the city console.log(s.address.city);
Combining the above two complex structure, we can create a JSON which contains key having arrays of JSON String.
Hope it helps.
Thanks.
}
So, in this post I am going to explain what is JSON.
JSON : JavaScript Object Notation is a technology / format to store the data in key - value pairs i.e. the large amount of data can be stored / transmit using a JSON.
Like for example : For a php developer, it is common to create the AJAX backend page and to send the data back to main page. Previously it was kind of tedious as user needs to create an array and needs to handle everything by himself / herself. But with JSON, it becomes very easy. JUST CREATE A JSON OBJECT containing the data, pass it to main page and use it.. simple.
and this is just a very basic example.
JSON is widely used in all Documents-Oriented Databases (like MongoDB[http://learnandsharetoall.blogspot.in/2014/01/what-is-mongodb.html], Hadoop, NoSQL etc.).
So JSON's application is very very much extended.
Now to understand JSON, lets divide it in four parts.
a) Basic Structure
b) Value Types
c) Complex Structures
So Basic Structure, a basic JSON script contains a "KEY-VALUE" pair to represent the data.
for example
{
"name" : "test",
"country" : "India"
}
So above is the basic JSON script.
Starting and ending the script with curly braces { and } and then key-value pairs separated by comma
Above name and country is key with values test and India.
So this is basic JSON structure, how a basic JSON looks like.
Moving on to value types , i.e. what are the types of values it can store.
So consider the below JSON,
{
"name " : "Test1",
"Age " : 22,
"isMarried" : false
}
So the above JSON has 3 value pairs, name with String value, Age with Integer value and isMarried with boolean values.
So as we have seen, the JSON can store a wide variety of data.
Even the key in JSON can serve as individual JSON Script and can also hold array of objects.(described in complex structure part)
Moving on to Complex Structures,
There can be two types of complex structures as told above.
a) Keys containing array of objects
b) Keys containing another JSON script(nested JSON)
Keys containing Array
Consider a JSON,
{
"name " : "test1",
" country " : "india",
"hobbies " : ["Music","Reading" , "Blah" , " Blah"]
}
So here a key hobbies containing an array of objects.So this, can be used to store the array of values.This can be really useful in real world applications.
Keys containing another JSON script(nested JSON)
Consider a JSON,
{
" name " : "test1",
"address" : {
"city" : "Noida",
"country" : "India"
}
"DOB" : "28-feb"
}
So in above script,
address is the key which itself is the json object.
You can use these as
var s = {
"name" : "test1",
"address" : {
"city" : "Noida",
"country" : "India"
},
"DOB" : "28-feb"
}
console.log(s.address); // This will print the address JSON
To get the city console.log(s.address.city);
Combining the above two complex structure, we can create a JSON which contains key having arrays of JSON String.
Hope it helps.
Thanks.
}
Hello , nice introduction post for JSON .
ReplyDeleteDo follow up with its live implementation in javascript .
Thanks and keep sharing . :)
yeah sure.. very soon i will post the live implementation..
ReplyDelete