what-is-json

JSON stands for JavaScript Object Notation, it is just a notation format for data interchange, technically we can say its an independent data exchange format use to send or manipulate data.

Structure of JSON:

JSON data is based on key and value pairs, where key will be a string and a value may be any thing like string or numeric value or boolean etc.
JSON data is bind together in {}, opening and closing curly braces and called as JSON Object.

An Example of JSON Object:

{
“firstName”: “Lisa”,
“lastName”: “Ray”,
“age”: “24”,
“phone”: “865965223”,
“city”: “pune”
}

We can create an array of JSON objects as follows:

var somevariable=[{
“firstName”: “Lisa”,
“lastName”: “Ray”,
“age”: “24”,
“phone”: “865965223”,
“city”: “pune”
},
{
“firstName”: “Mona”,
“lastName”: “Jose”,
“age”: “20”,
“phone”: “98565223”,
“city”: “mumbai”
}]

Here you will observe that JSON array is nothing but a group of JSON objects separated by (,) comma and enclosed in [] a square brackets.

Now a days JSON is extensively used in web development replacing XML, an another format for data interchange and that is because, it is
lighter then XML, easy to use and understand. It can be used over cross platform over cross technologies for data interchange for example we can use
JSON for PHP, javascript, ASP.net and for making desktop web application, ipads, mobiles etc. That’s the reason many web services from social platforms
provides JSON support.

JSON and Javascript:

JSON is a subset of ECME-Script, a JavaScript Specification and therefore is directly supported in JavaScript. Here is a simple example of json with
javascript:

Here in the above example I am having a JSON array in which I have First Name and Last Name for two students, and using javascript DOM I showed that in html spans.

JSON and PHP:

PHP supports json and provides two predefined functions to encode and decode an array to JSON. The functions are:

1. json_encode(); To encode array as JSON object.
2. json_decode(); Tod decode json object to an php array.

Example:

json_encode();
1, ‘b’ => 2, ‘c’ => 3, ‘d’ => 4, ‘e’ => 5);
echo json_encode($arr);
?>

json_decode();

JSON with JQuery:
Jquery, one of the most popular and extensively used framework of javascript, provides full support to JSON and provides predefined functions to use it with ease.

1. jQuery.parseJSON() : Parse a JSON string.

Ex:

2. jQuery.getJSON() : Load JSON-encoded data from the server using a GET HTTP request.

Ex:

3.$.ajax(): to post data and get result in json format

Example:

Lastly, few core javascript functions to handle JSON:

1. JSON.stringify(); : converts an object to JSON notation representing it.

Syntax:

JSON.stringify(value[, replacer [, space]]);

examples:

i) assert(JSON.stringify(true) === ‘true’);
ii) assert(JSON.stringify(“foo”) === ‘”foo”‘);

2. JSON.parse(); : parses a string as JSON and returns the parsed value.

Syntax:

JSON.parse(text[, reviver]);

Examples:

i) JSON.parse(‘true’); // true
ii) JSON.parse(‘”foo”‘); // “foo”

Hope you enjoyed the article…

Cheers!!! ūüôā

–what is json —