JSON

JSON

Introduction

The web services paradigm works with a number of technologies and services. In those data types that are used for exchanging information between the client and the server play a vital role. There are multiple formats available in REST for requests and responses. In this article we are discussing JSON which is a popular data interchange format.

JSON

JSON is a lightweight data interchange format that is used in exchanging data between client and server in web services implementations. JSON stands for Javascript Object Notation. The JSON files will end with a .json extension. While specifying the HTTP media type it is represented as ‘application/json’.

Why JSON

Javascript Object Notation is popular compared to its counterparts. This is mainly due to the way JSON stores and processes information. The information in JSON is well organised and easy to access which is in human readable format. You can store any type of data in this format. When compared to other data storing formats, JSON is very light weight and does not need any complicated parsing processes to retrieve information.

Creating a JSON Object

You can create a JSON object like the same way you create a generic object. For example, in Javascript you will be creating a JSON object like as follows:

  • An empty object:  var JSONObj = {};
  •  A new object: var JSONObj = new Object();
  • Create & Initialize an object:  var JSONObj = {“car”: “Honda City”, “color”: “red”}

JSON Data Storing Mechanism

The data in JSON is stored in a Key-Value pair. This is the simplest storage option for network transmission. For example if I create a JSON block with my details, it would be something like as given below:

                                                                             

1 vivek = {

2 “age” : “30”,

3 “country” : India,IN”,

4 “gender” : “male”

5 };

This will create an object ‘vivek’ through which one can access all the related information. Here the “name” can have multiple “values” of different types. It can be an integer, an array, a string, etc.

While retrieving the information it only requires to refer to the object and the corresponding property of that object. For example if we would like to know the age of ‘vivek’ you can get it by accessing the return value of ‘vivek.age’ property.

                 var age = vivek.age;

This way accessing the information is pretty simple.

The below given diagram shows the types of data that  JSON can support.

 

 

The main two data structures that JSON support are:

  1. A collection: This is the collection of data in name value pairs.
  2. An ordered list: This type of data structure can be seen when we store an array, list, etc. in a JSON object.

JSON and JSONP

One of the biggest advantages of using JSON in client side programming is its ability to make cross-domain data exchange. This is achieved using JSONP which is known as JSON-Padding.When you send a JSONP enabled request to the server, you will also pass a callback parameter that will enable the server to send the response in such a way that the client will be able to handle the response and execute further actions. The only problem with this method is that once you send the request, you don’t have any handle over it. The response may come or may not, you won’t get any notification or acknowledgement that you can share with your end user.  So you end up using timers and that is not the best choice always.

Summary

We have seen the JSON format and its use in the webservices world. There are many use cases where JSON is really a time saver and makes the process ease and fast. The support for both the client side and the server side for this format is extensive and without doubt JSON is one of the most popular data exchange formats now.