JSON Tutorial

Json is a syntax used for passing objects that consist of name or value pairs, arrays and objects. This syntax is used for storing and exchanging text information. It is also called LEON (Lightweight Ecmascript Object Notation).  Json stands for JavaScript Object Notation. Json looks like an XML. Because they are both self describing meaning that values are named and thus human readable, both are hierarchical, both can be parsed and used by lots of programming languages and both can be passed around using AJAX. Json is fast and easy to understand compared to any other languages. Json is self describing and easy to understand and it is a language independent. Json is built on two structures. They are

1)      A collection of name/value pairs. In various languages, this is realized as an object, record, strut, dictionary, hash table, keyed list, or associative array.

2)      An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Eg :-

{“skillz”: {
“web”:[
{“name”: “html”,
“years”: “5”
},
{“name”: “css”,
“years”: “3”
}],
“database”:[
{“name”: “sql”,
“years”: “7”
}]
}}

In the above example squiggly brackets stands for containers, Square bracket stands for array, names and its values are separated by colon and array elements are separated by commas.

Structure of JSON

In javaScript we can create object

Syntax: –

“var JSONObjectName ={};”  // It will create an empty object.
“var JSONObjectName= new Object();”  // It will create a new Object.
“var JSONObjectName = { “name “:”amit”, “age”:23};  // It will create an Object with attribute name which contains value in String and age with numeric value.

Eg: –

<html>
<head>
<title>
Object creation in JSON in JavaScript
</title>
<script language=“javascript” >
var JSONObject = {  “name” : “Anju”,
“address”  : “MG Road Delhi“,
“age”  : 22,
“phone”   : “011-2424224”,
“MobileNo”  : 99000000
};
document.write(“<h2><font color=‘blue’>Name</font>::”
+JSONObject.name+”</h2>“);
document.write(“<h2><font color=‘blue’>Address</font>::”
+JSONObject.address+”</h2>“);
document.write(“<h2><font color=‘blue’>Age</font>::”
+JSONObject.age+”</h2>“);
document.write(“<h2><font color=‘blue’>Phone No.</font>::”
+JSONObject.phone+”</h2>“);
document.write(“<h2><font color=‘blue’>Mobile No.</font>::”
+JSONObject.MobileNo+”</h2>“);
</script>
</head>
<body>
<h3>Example of object creation in JSON in JavaScript</h3>
</body>
</html>

We prefer Json because it’s a good way to passing and saving data in javaScript. There is no need to call read anything, all operations are handled by Json with JS app. Json is used for serializing and transmitting structured data over the network connection and also used to transmit data between a server and web application. Json is a subset of javaScript and it is also used to parse Json text into an object by invoking javaScript function like eval() function.

Syntax: –

var p = eval(“(“ + contact + “)”);//Here the variable should be covered in parentheses and it avoid an ambiguity in javaScript.

The basic data types are Number, String, Array, Boolean, Object and null.

Advantages of JSON

  1. It’s compact
  2. It’s easy for both computers and people to read and write.
  3. It maps very easily onto the data structures used by most programming languages (numbers, strings, booleans, nulls, arrays and associative arrays)
  4. All programming languages contain functions or libraries that can read and write JSON structures
  5. JSON is easy. No really. It’s so easy, it’ll make you sick.
  6. If you’re familiar with writing classes in PHP, then you’ll most definitely be comfortable with writing JavaScript in Object      Notation
  7. JSON is nothing more than name : – value pairs assigned within an object.
  8. JSON is easy to understand because if written well, it’s a self-documenting structure.
  9. JSON is fast : – Fast object de-serialization in JavaScript (based on anecdotal evidence ).
  10. JSON organizes the ugly mess of procedural programming. Imagine having more than one init function.
  11. Simple API (available for JavaScript and many other languages).
  12. Supported by many AJAX toolkits and JavaScript libraries
  13. You can impress your friends with JSON because it’s pretty looking
  14. Your co-workers will love you for writing in JSON because it will most likely not conflict with their scripts that are being called within the same web documents.
  15.  Fully automated way of de-serializing/serializing JavaScript objects, minimum to no coding.
  16. Good support by all browsers.
  17.  Highly portable

Disadvantages of JSON

  1. No support for formal grammar definition, hence interface contracts are hard to communicate and enforce.
  2. No namespace support, hence poor extensibility.
  3. Limited development tools support.
  4. Narrow focus, used for RPC only, primarily with JavaScript clients (although one can argue that it’s one of the strengths).
  5. No support in Web services -related products (application servers, ESBs, etc), at least not yet.