Skip to main content

JSON : Tutorial and Usage

Json Introduction

What is JSON?

  • JSON stands for JavaScript Object Notation
  • JSON is lightweight text-data interchange format
  • JSON is language independent *
  • JSON is "self-describing" and easy to understand
JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Json Format Example :
    "employees": [
            "firstName": "John",
            "lastName": "Doe"
            "firstName": "Anna",
            "lastName": "Smith"

For AJAX applications, JSON is faster and easier than XML:
Using XML
  • Fetch an XML document
  • Use the XML DOM to loop through the document
  • Extract values and store in variables
Using JSON
  • Fetch a JSON string
  • eval() the JSON string

JSON Values
JSON values can be:
  • A number (integer or floating point)
  • A string (in double quotes)
  • A Boolean (true or false)
  • An array (in square brackets)
  • An object (in curly brackets)
  • null
More JSON Introduction 
Json Validator
JSON Lint is a web based validator and reformatter for JSON, a lightweight data-interchange format.

Json Usage 


Returns the JSON representation of a value
Decodes a JSON string

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json, true));

Output: array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)


Library Require : $tar xvfz JSON-2.53.tar.gz
converts the given Perl data structure to a UTF-8 encoded, binary string
used for decoding JSON in Per

use JSON;
use Data::Dumper;

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

$text = decode_json($json);
print  Dumper($text);


$VAR1 = {
          'e' => 5,
          'c' => 3,
          'a' => 1,
          'b' => 2,
          'd' => 4


Library Required : JSON.simple 

Encoding in JAVA

import org.json.simple.JSONObject;

class JsonEncodeDemo
   public static void main(String[] args)
      JSONObject obj = new JSONObject();
      obj.put("name", "foo");
      obj.put("num", new Integer(100));
      obj.put("balance", new Double(1000.21));
      obj.put("is_vip", new Boolean(true));

While compile and executing above program, this will produce following result:
{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"} 

Decoding Json in JAVA 

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

class JsonDecodeDemo
   public static void main(String[] args)
      JSONParser parser=new JSONParser();
      String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
         Object obj = parser.parse(s);
         JSONArray array = (JSONArray)obj;
         System.out.println("The 2nd element of array");

         JSONObject obj2 = (JSONObject)array.get(1);
         System.out.println("Field \"1\"");

         s = "{}";
         obj = parser.parse(s);

         s= "[5,]";
         obj = parser.parse(s);

         s= "[5,,2]";
         obj = parser.parse(s);
      }catch(ParseException pe){
         System.out.println("position: " + pe.getPosition());
While compile and executing above program, this will produce following result:
The 2nd element of array

Field "1"


No library required  
var jsonObj = JSON.parse(json_string);
var name =;
var country =;

You can also check our Microservices post

Introduction to Microservices


Popular posts from this blog

What is MonGoDB??

Share on Google Plus Now a Days, we are hearing a lot about MongoDB.

So, in this post I will try to briefly introduce to MongoDB.

MonGoDB is "Non-Relational, JSON, Document Store".

Explaining in detail,

Non-Relational, --> The DB we use most like SQl, Oracle are Relational DBs(RDBMS). They do have the fixed schemas, lots of tables. So Non-relational in nothing like that.

JSON : (JavaScript Object Notation) : It is basically a document with information in the form of key value pair.

for example,


thats it. 
A simple JSON document where name and address are key
and "lorem" and "ipsum" are corresponding values.

So, MongoDB is the Non Relational database which stores information using DOCUMENT and that document is a JSON document.

MongoDB stores collections of documents.
(Consider the above exxample as collection  "Person"  with a document)(will elaborate it later more)

Need of MongoDB:
The Databases we use…

Login with Google Account to Website using OAuth2.0 with PHP / Javascript

This post have Complete Code for Login / Sign-in  with google Account  using PHP / Javascript with oAuth2.0

Basically today we have seen almost every website needs you to register yourself before you can post or take part in any discussions to the website. But it become a tedious task to register and login to many different sites. Solution is to provide the users the option to Login with existing Google / Facebook account as almost everyone have Facebook and Google account.

In this post, I am going to explain how to integrate the Google Login / Sign in  for your website.

Download Source Code

Live Demo

For this,  First you need to create your Client ID, Client Secret and your developer API key.

For this go to

Click on the button Create Project. A new window will open up like this :

Enter the Project name and submit on Create.

Wait for new seconds, the new page will open like this.

On the left menu, click on API & Auth and click on credentails. Then…