Thursday, June 4, 2015

Improve knowledge about JavaScript Object Notation

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers which include C, C++, Java, Python, Perl etc.
  • JSON stands for JavaScript Object Notation.
  • This format was specified by Douglas Crockford.
  • This was designed for human-readable data interchange
  • It has been extended from the JavaScript scripting language.
  • The filename extension is .json
  • JSON Internet Media type is application/json
  • The Uniform Type Identifier is public.json

Uses of JSON

  • It is used when writing JavaScript based application which includes browser extension and websites.
  • JSON format is used for serializing & transmitting structured data over network connection.
  • This is primarily used to transmit data between server and web application.
  • Web Services and API.s use JSON format to provide public data.
  • It can be used with modern programming languages.

Characteristics of JSON

  • Easy to read and write JSON.
  • Lightweight text based interchange format
  • Language independent.

Simple Example in JSON

Example shows Books information stored using JSON considering language of books and there editions:
{
    "book": [
    {
       "id":"01",
       "language": "Java",
       "edition": "third",
       "author": "Herbert Schildt"
    },
    {
       "id":"07",
       "language": "C++",
       "edition": "second"
       "author": "E.Balagurusamy"
    }]
}
After understanding the above program we will try another example, let's save the below code asjson.htm:
<html>
<head>
<title>JSON example</title>
<script language="javascript" >
  
  var object1 = { "language" : "Java", "author"  : "herbert schildt" };
  document.write("<h1>JSON with JavaScript example</h1>");
  document.write("<br>");
  document.write("<h3>Language = " + object1.language+"</h3>");  
  document.write("<h3>Author = " + object1.author+"</h3>");   

  var object2 = { "language" : "C++", "author"  : "E-Balagurusamy" };
  document.write("<br>");
  document.write("<h3>Language = " + object2.language+"</h3>");  
  document.write("<h3>Author = " + object2.author+"</h3>");   
  
  document.write("<hr />");
  document.write(object2.language + " programming language can be studied " +
  "from book written by " + object2.author);
  document.write("<hr />");
  
</script>
</head>
<body>
</body>
</html>

JSON - Syntax

Let's have a quick look on JSON basic syntax. JSON syntax is basically considered as subset of JavaScript syntax, it includes the following:
  • Data is represented in name/value pairs
  • Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma).
  • Square brackets hold arrays and values are separated by ,(comma).
Below is a simple example:
{
    "book": [
    {
       "id":"01",
       "language": "Java",
       "edition": "third",
       "author": "Herbert Schildt"
    },
    {
       "id":"07",
       "language": "C++",
       "edition": "second"
       "author": "E.Balagurusamy"
    }]
}

No comments:

Post a Comment