JSON (JavaScript Object Notation) is the light weight data interchange format like XML i.e like the XML it’s also platform independent and its great alternative to XML due to followings:

  • JSON can be parsed easily rather than XML although XML is easier to read due to better hierarchical nature.
  • In the Ajax application as well as in PHP application, JSON is much faster and easy to be parsed than XML.
  • JSON uses array as well as object.
  • There is no reserved words/syntax unlike XML
  • JSON is simply plain text

A numerous Web Services and APIs use JSON to provide the public data. By using the Web Services / APIs, we can fetch the JSON data and can be used in application.

A JSON object can have following data types:

  • Array
  • Boolean
  • JSON data object
  • Number
  • String
  • Null

Creating JSON:

As JSON data is the plain text and it can be created easily with some easy rules like:

  • The square bracket/s contains arrays.
  • The curly braces contain the objects.
  • Data is being separated with commas.
  • Data is being presented with name/value pair and paired with colon(:).

Example: {“name”:”Siddhesh”, “email”:”example@example-email.com”, “country”:”India”}

Above is the simple example of JSON data and it’s easily understandable:

name = Siddhesh

email = example@example-email.com

country = India

Another example of JSON data combined with array and object:

[sourcecode language=“plain”]

“members”: [

{

"member_id":1,

"username":"user1",

"address": {"street":"s1", "city":"city1", "country":"India"}

},

{

"member_id":2,

"username":"user2",

"address": {"street":"s2", "city":"city2", "country":"India"}

}

]

}

[/sourcecode]

It seems a little more complex but we can understand it easily. As you can see the JSON data is much like the arrays of JavaScript. So we can say, JSON is a subset of JavaScript.

Here is a simple example for populating/getting the values from JSON data in JavaScript

[sourcecode language=“js”]

[/sourcecode]

In the above example you can traverse the data with for loop.

JSON and PHP:

In PHP 5.2.0+, the JSON extension is bundled and compiled within the core. For the older version than PHP 5.2.0, you will need to install JSON PECL Extension.

Here, I would like to share only the basic working of JSON and PHP. Following are the PHP functions which handle/process JSON data:

json_encode: This function return the JSON data of any values, array (multi-dimensional also), objects or anything which is UTF-8 encoded except resources.

[sourcecode language=“php”]

$students = array(

array(“name” =>

“ABC”, “class” => 1, “rollno” => 12345),

array(“name” => “XYZ”, “class” => 1, “rollno” => 67890)

);

echo json_encode($students);

// prints [{"name":"ABC","class":1,"rollno":12345}, {"name":"XYZ","class":1,"rollno":67890}]

?>

[/sourcecode]

json_decode: This function decodes the JSON data string as simple as encoding it or it takes JSON encoded string as parameter and return decoded PHP variable.

We can take the encoded JSON string in the above example of json_encode

[sourcecode language=“php”]

$json = '[{"name":"ABC","class":1,"rollno":12345},{"name":"XYZ","class":1,"rollno":67890}]';

$var = json_decode($json);

print_r($var);

// prints Array ( [0] =>

stdClass Object ( [name] => ABC [class] => 1 [rollno] => 12345 ) [1] => stdClass Object ( [name] => XYZ [class] => 1 [rollno] => 67890 ) )

?>

[/sourcecode]

json_last_error: It returns the last error occurred during JSON encoding or decoding.

JSON data file: Like the database tables, you can store the data in the format of JSON string into a file and it can be read easily.

Let, you have created a file members.json having JSON data:

[sourcecode language=“plain”]

“members”: [{"member_id":1,  "username":"user1", "address": {"street":"s1", "city":"city1", "country":"India"}}, {"member_id":2, "username":"user2", "address": {"street":"s2", "city":"city2", "country":"India"}}]}

[/sourcecode]

In the PHP, we can use file_get_contents or CURL method to get/parse the JSON file data:

[sourcecode language=“php”]

$json = file_get_contents(“members.json”);

$data = json_decode($json);

?>

[/sourcecode]

At client side using the JQuery, the JSON file data can be read/fetch from the file easily. Like:

[sourcecode language=“js”]

[/sourcecode]

$.parseJSON API of JQuery can also be used for the same purpose. Also, the fetched JSON data can be iterated via “for” loop or $.each in JQuery.

So this was the little about the JSON. I hope, it would give you a good overview about JSON so that you can use JSON in your web applications.

Thanks for reading 😊.