Tuesday, 24 June 2014

JSON - The New Data Interchange Format For Web Services

What is JSON ?

JSON ( Javascript Object Notation) is a plain - text format for data storage. It has become quite popular as a data interchange format for web services, for configuration files and more. Features of JSON are, it is easy to read for humans and easy to parse by machines when compared to XML.

JSON stores data as plain text. Its grammar is subset of the grammar of java script expressions.

How to write a simple JSON object ?

Writing a JSON object is very simple. All the members of JSON are nothing but name, value pairs. Its some what simillar to Hash Map of Java language. Just begin with a '{' and all the name, value pairs will be seperated by ',' & name , value are seperated by ':'. Here is an example.

     "First Name" : "speaking" ,
     "Last Name" : "cs",
     "Type"  : "Blog",
     "Domain" : "www.speakingcs.com"

You can save a JSON object as a file with ".json" extension, just like xml whose extension is ".xml".

The grammar behind creation of JSON is as follows.
         {} - which means a JSON object can be an empty object
         { members } - or an object with members in it
         pair - which means pair is a name - value pair - so an JSON object can contain only one pair
         pair, members - or multiple pairs
         string : value - pair is nothing but name - value association just like HashMap
         [] - The value can be an empty array
         [elements] - or with elements in int like numbers, strings, arrays, JSON objects
         value, elements
         string - Which means a Value can be string ,number, another JSON object, array, boolean , null
         "" - String can be empty string
         "chars" - or non empty string
         char - a single character
         char chars - or multiple characters
         any Unicode character except \b \f \n \r \t \u four hex digits
         int - number can be a normal integer
         int frac - or a fraction just like Double or Float value in java
         int exp - or an exponention
         int frac exp - or combination of fraction and exponention e.g 14.345e+19
         digit - an integer can be single digit
         digit 1-9 digits - or a number
         .digits - fraction is a decimal number
         e digits
         digit digits
         e e+ e-
         E E+ E
Rules to follow while creating a JSON object :

1. Strings must always be double - quoted, string literals such as 'mystr' are illegal.
2. Property keys must be double - quoted.

How to create a JSON object in Java script ?

Simply follow the grammar, rules mentioned above & create a JSON object and assign it to  a variable in java script. You can get values from JSON object using . operator.

Eg: var a = {

Retrieving values from JSON object :

a.first gives result as speaking, you can also get all the values by using "." operator and key name.

How to create a JSON object in Java ?

     You can create JSON object in java using external APIs such as org.json.simple API or Google's gson API. Here is the sample code written by using org.json.simple API. The class used to create JSON object is "JSONObject". The method createJson() creates JSON object and saves to disk by using FileWriter class in java, the method readJSON() reads JSON object from disk using FileReader class in java.
package speakingcs.json;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonWrite {

public static void main(String[] args) {
JsonWrite jw = new JsonWrite();

try {
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (ParseException e) {

private void createJson() throws IOException {
JSONObject jsonObj = new JSONObject();
jsonObj.put("site name", "www.speakingcs.com");
jsonObj.put("type", "blog");
jsonObj.put("domain type", ".com");

JSONArray dob = new JSONArray();

JSONObject dateObj = new JSONObject();
dateObj.put("date", 22);
dateObj.put("month", "December");
dateObj.put("year", 1990);

jsonObj.put("dateObject", dateObj);

// saving file to disk 

FileWriter fw = new FileWriter("simple.json");

private void readJson() throws FileNotFoundException, IOException, ParseException {
JSONParser jParser = new JSONParser();
Object obj = jParser.parse(new FileReader("simple.json"));
// convert the obj into JSON Object
JSONObject jsonObj = (JSONObject) obj;
System.out.println(jsonObj.get("site name"));
JSONArray dobArray = (JSONArray) jsonObj.get("DOB");
Iterator<Object> itr = dobArray.iterator();
while(itr.hasNext()) {


Post a Comment

Note: only a member of this blog may post a comment.