This JSON Tutorial is a quick overview of JSON concepts and its usages.This simple brief JSON Tutorial Will be very beneficial for beginners.
JSON stands for JavaScript Object Notation is a language independent lightweight data-interchange format.The syntax of JSON is based upon JavaScript script syntax.
JSON objects are used for the exchange of data between applications and services.
XML is another known data exchange format.
But JSON has several advantages over XML in data object handling, mainly that JSON objects are very lightweight.
Table of Contents
Below given JSON sample format shows how JSON syntax represents the information of 3 employees in an organization.
{
"Employee":
[
{ "EmpId":"E0001","Name": "Peter", "Designation": "Programmer","Salary": "20000"} ,
{ "EmpId":"E0002","Name": "Tom","Designation": "System Administrator","Salary": "25000"},
{ "EmpId":"E0003","Name": "Mary", "Designation": "Web Designer","Salary": "20000"}
]
}
From the above JSON structure , it is clear that how simpler the schema is compared to XML.
As like all other programming languages, JSON support data types such as,
string
number (integer,fraction,exponent)
boolean (true, false)
object
array
null
JSON values can be any of the following types. JSON don’t support DATE data type,
JSON doesn’t support DATE data type.So to store date value in JSON, we need to use a format that can be easily converted as a date object.
JSON has support for mainly two data structures which are common collections in conventional programming.
JSON Objects (Name-value pairs collection), which are similar to hash tables, dictionary, keyed list, associative array etc. in other programming languages.
JSON Arrays (Ordered list), similar to the array, list, vector, etc in other programming languages.
This data structure similarity with modern programming languages also one key factor in JSON becoming a popular data-exchange format.
A JSON object variable called “Location” is defined below,
var = Location {
"City" : "Bangalore",
"State" : "Karnataka",
"Zip" : "560061"
};
Now the JSON object can be accessed from javascript to extract the information encapsulated in the object as,
Location.City, Location.State etc. See below,
document.writeln("City: " + Location.City);
document.writeln("State: " + Location.State);
document.writeln("Country: "+ Location.Zip);
For looping through JSON Object properties(keys) we can use for in loop as,
for (obj in Location)
{
document.write(obj);
}
Result: City State Country
Also, we can use square bracket to fetch the values as,
for (key in Location)
{
document.write(Location[key]);
}
Result: Bangalore Karnataka 560061
As like normal array objects, JSON object arrays are to store a set of JSON elements which are sequential in nature and can be accessed using the index.
JSON Array is an ordered collection of data elements, enclosed in square brackets with individual data elements separated by a comma.
See a simple example below to create a JSON array object,
var JsonBooks =
[
{ "name" : "JSON Basics", "price" : "25$" },
{ "name" : "JSON Simplified", "price" : "30$" },
{ "name" : "JSON Fundamentals", "price" : "20$" }
];
In Javascript, to extract the internal data from JSON array object, we can do as like
x = JsonBooks[0].name;
y = JsonBooks[1].price;
The result of above statements is JSON Basics & 30$ respectively.
JSON nested objects are nothing but JSON objects within JSON object
var Players =
{
"Cricket" : { "Name" : "Sachin", "Age" : "40", "Gender" : "male" },
"Batminton" : { "Name" : "Saina", "Age" : "28", "Gender" : "female" },
"Tennis" : { "Name" : "Leander", "Age" : "38", "Gender" : "male"}
}
We can access the internal information as below rather than using an index as above.
document.writeln(Players.Cricket.Name);
This simple JSON tutorial for beginners would have given you a basic understanding of JSON. Provide your valuable feedback in the comment section below.Check also the other useful JSON materials from us.
OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…
JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…
The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…
Some of the Key factors that need to consider while architecting or designing a software…
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…
The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…