Site Loader
Auckland, New Zealand
JSON.simple
JSON stands for JavaScript Object Notification, which is a lightweight text-data interchange format and completely self-describing. JSON is a subset of JavaScript and hence can be parsed using JavaScript itself (using function called eval), if you are using JAVA, you can use some JSON parsers to parse JSON(In my case I have used json.simple, other popular libraries to process JSON are Google gson and Jackson). Similarly all the language has its own JSON parses, while it comes to browsers, almost all the popular browsers has the support for JSON.

JSON vs XML

JSON Syntax

JSON has following Syntax
  1. Data in name/value pairs
  2. Curly braces holds objects
  3. Square brackets holds arrays
Let’s see what I mean all the above one by one

Data in name/value pairs

Here name and values are separated by colons “:” Name will be declared within quotes whereas value can be of any data type like
  • Integer
  • Boolean
  • String
  • Array or Object
Here is how the name/value pairs data looks like
{"Name":"Karthik KK","IsItUseful":true}
 

Curly Braces holds objects

All the curly braces holds objects, which can be a combinations of data, array etc as shown below
{

"Name":"Karthik KK","IsItUseful":true,

"Vistors":[{"Vistor3":"Ramesh","Vistor2":"Abraham","Vistor1":"John"}],

"Topic":"JSON","WebSite":"executeautomation.com","TotalVisits":100

}

Square bracket holds arrays

It looks like this
[{"Vistor3":"Ramesh","Vistor2":"Abraham","Vistor1":"John"}]

Writing a Simple JSON code in JAVA and parsing it using json-simple

Well, now from above demonstration you might have an idea of what JSON is and how easy it is than XML. Let’s write a simple program which
  • Creates a JSON Object
  • Parses the Created JSON object
This demonstration will be helpful and give you a complete understanding of how JSON is created and parsed (In Java)  
package org.jsondev;

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

public class ExecuteJSON {

	public static JSONObject object = null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Create JSON Object
		CreateJSONObject();
		// Parse JSON Object
		ParseJSON();
	}

	/*
	 * CreateJSONObject with all the details
	 */
	public static void CreateJSONObject() {
		object = new JSONObject();
		// Creating List of Visitors for my site
		JSONArray vistor = new JSONArray();
		// Details for all the Vistors
		JSONObject vistorDetails = new JSONObject();
		vistorDetails.put("Vistor1", "John");
		vistorDetails.put("Vistor2", "Abraham");
		vistorDetails.put("Vistor3", "Ramesh");
		vistor.add(vistorDetails);

		// Putting all the details
		object.put("Name", "Karthik KK");
		object.put("WebSite", "executeautomation.com");
		object.put("Topic", "JSON");
		object.put("TotalVisits", new Integer(100));
		object.put("IsItUseful", new Boolean(true));
		object.put("Vistors", vistor);

		System.out.println(object);
	}

	/*
	 * ParseJSON for parsing the created JSON object
	 */
	public static void ParseJSON() {

		JSONParser parse = new JSONParser();
		JSONObject obj;
		try {
			obj = (JSONObject) parse.parse(object.toJSONString());
			System.out.println("\n##### OUTPUT OF PARSED JSON #####");
			System.out.println(obj.get("Name"));
			System.out.println(obj.get("WebSite"));
			System.out.println(obj.get("Topic"));
			System.out.println(obj.get("TotalVisits"));
			System.out.println(obj.get("Vistors"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

After executing the code, the output will look something like this {“Name”:”Karthik KK”,”IsItUseful”:true,”Vistors”:[{“Vistor3″:”Ramesh”,”Vistor2″:”Abraham”,”Vistor1″:”John”}],”Topic”:”JSON”,”WebSite”:”executeautomation.com”,”TotalVisits”:100} ##### OUTPUT OF PARSED JSON ##### Karthik KK executeautomation.com JSON 100 [{“Vistor3″:”Ramesh”,”Vistor2″:”Abraham”,”Vistor1″:”John”}] I hope, you enjoyed reading the post and got a complete understanding of what JSON is and how to parse the same in JAVA. Please let your comments and let me know if I want to update anything in the post. Thanks, Karthik KK

Post Author: Karthik kk

2 Replies to “An Introduction to JSON”

  1. Hi,

    Can you demo on a video how c# selenium and json file can be used please?

    Thanks

    btw your vids are great keep up the good work

Leave a Reply to Sunny Cancel reply

Your email address will not be published. Required fields are marked *