Introduction

This article explains how to use the JavaScript Object Notation (JSON) extension in PHP, going step by step through a series of essential operations. First, let's see what the JSON extension is and what the advantages are of using it. JSON is an object string notation, it is defined as a subset of JavaScript's syntax and its general-purpose is to interchange data format. As you probably know, JSON was first made to be used with JavaScript for accessing remote data, but now it is used by many other languages because JSON data is platform independent data format. JSON can be used natively in JavaScript, but you can also use it in a server-client application logic. You can learn more about JSON and about the languages that use this extension here.

 

JSON supports different kind of data types like: numbers, strings, boolean, array data and object. Below is listed a simple format of JSON data which shows the details of a book:

Creating and Parsing a JSON Object in PHP

Starting with PHP 5.2.0 the JSON extension is default. To handle JSON data we have two useful functions used for converting and parsing JSON data trough PHP: json_encode() and json_decode() described below:

 

string json_encode (

 

mixed $value [, int $options = 0 [, int $depth = 512 ]] ) - Returns the JSON representation of a value where the $value argument can be any type except a resource („ A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions."). The $options argument is optional and may be one of these bitmasks: JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_HEX_QUOT, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE (learn more about JSON predefined constants). The $depth argument sets the maximum depth and must be greater than zero.

 

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) - Takes a JSON encoded string and converts it into a PHP variable trough the $json argument. The $assoc argument returns objects that will be converted into associative arrays when is set to TRUE; $depth is optional and represents the user specified recursion depth; $options is also optional and represents the bitmask of JSON decode options (currently, only JSON_BIGINT_AS_STRING is supported).

 

And now, let's create the JSON data format using a PHP array and the json_encode() method described above:

 

Listing: EncodeJSON_array.php

 

The above code generates the JSON data listed below:

 

Now, let's decode above JSON data in PHP, using the json_decode() method described above:

 

Listing: DecodeJSON_array.php

 

The output of the Listing DecodeJSON_array.php is:

 

1 : title
2 : author
3 : yearofpublication
4 : publisher
5 : price

 

Now, let's see how to work with objects. The next application encodes an PHP object, in this case $book, and then is decoded as an object, using the json_decode($result)function and as an array, by setting the True argument into the json_decode($result,true):