HomeCoding & Programming

JSON (Simple and Universal data exchange format)

Like Tweet Pin it Share Share Email

JSON (JavaScript Object Notation) is a text-based, light-weighted, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON follows a small set of formatting rules for the portable representation of structured data. It is easy for humans to read and write and easy for machines to parse and generate. These properties make JSON an ideal data-interchange language.

 

JSON is built on two structures:

 

A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

 

An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence

 

In JSON, data can take on these forms:

* An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by, (comma).

 

* An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by, (comma).

 

* A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

 

* White space can be inserted between any pair of tokens. Excepting a few encoding details that completely describe the language.

 

A JSON text is a serialized object or array.

JSON-text         = object / array

 

These are the six structural characters used in JSON.

begin-array     by  [ left square bracket
begin-object    by  { left curly bracket
end-array       by  ] right square bracket
end-object      by  } right curly bracket
name-separator  by  : colon
value-separator by  , comma

 

The terms “object” and “array” have taken from the conventions of JavaScript. JSON’s design goals were for it to be minimal, portable, textual, and a subset of JavaScript.

 

Example

    var employees = { "operations" : [   // operations is an array in employees.
    { "firstName" : "Steve",  // First element
    "lastName"  : "Jobs",
    "age"       : 50 },

    { "firstName" : "Maria",  // Second Element
    "lastName"  : "Denial",
    "age"       : 41 }
    ], // End "operations" array. 

    "development" : [ // Development is another array.
    { "firstName" : "Kailash",
    "lastName"  : "Chaturvedi",
    "age"       : 25 },

    { "firstName" : "Mahesh",
    "lastName"  : "Yadav",
    "age"       : 42 }
    ]
    } // End JSON Employees data

Here employee is an object. That object has two properties or name/value pairs. “operations” is an array which holds two JSON objects showing the names and age of 2 employees. Likewise “development” is also an array which holds two JSON objects showing the name and ago of the two employees who work in development. All of this data exists within the employees object. There are several different ways to access this data.

 

How to access Data in JSON?

 

Using the “employee” example above, if we wanted to access the first person who worked in development.

document.writeln(employees.development[0].firstName + ‘ ‘ + employees.development[0].lastName);

 

You can also access the second person who works in “operations”
document.writeln(employees.operations[1].firstName + ‘ ‘ + employees.operations[1].lastName);

 

The most common and easy way to access JSON data is through dot notation.
This is simply the object name followed by a period and then followed by the name/property you would like to access.

 

    var myObject = { 'color' : 'cyan' };
    document.writeln(myObject.color); // output show cyan

 

If your object contains an object then just add another period and name.

var myObject = { 'color'  : 'cyan',
'house'  : {'room' : 5 }
};

document.writeln(myObject.house.room); // output will 5

Comments (1)

  • Pingback:

Comments are closed.