Table of Content

JSON syntax



How JSON Works ?

In JSON we can have 2 conventions to represent Data.

  • We can store data in JSON using two types of notations.
  • Using Array Notation we can store data in array and can easily parse data using array index.
  • Object notation creates key-value pair to store data and retrieves each record using Key.

Different Types of Notations :

  1. Object Representation
  2. Array Representation

1.Array Representation :

var language = [ "english", "hindi", "marathi" ];

We have created array of languages. Our Variable name is language.

Accessing Array :

language[0] ====>  english
language[1] ====>  hindi
language[2] ====>  marathi

thus we can have access to json array data using index and [] .

2.Object Representation :

var example = { "flower": "rose" };

In above example we have created key-value pair. we have created object example which contain “flower” as “Key” and “rose” as “Value

example =======> Name of Variable/Object
flower  =======> Key
 rose =======> Value 

How to access JSON Object :

example["flower"]

Or

example.flower

We can represent more and more complex data structure using these JSON elements.

Example Complex Data Structure :

var author =
{
      "firstName": "Pritesh",
      "lastName" : "Taral",
      "site":
      [
         {
         "title": "c4learn.com",
         "year": "2009"
         },
         {
         "title": "javaprogramming4learn.com",
         "year": "2011"
         }
      ]
};

Accessing Various Elements from JSON Object :

1. Accessing name of author :

author.firstname

2. Accessing Last name of author :

author.lastname

3. Accessing c4learn.com :

author.site[0].title

4. Accessing Year :

author.site[0].year

Must Read External Articles –

  1. http://codular.com/json
  2. Working on JSON objects in jQuery and MVC
  3. JSON with Java Tutorials