JSON with PHP – JSON Encode and Decode

JSON is widely used for exchanging information across websites and web applications. The web services are written to send and accept serialized data. JSON(JavaScript Object Notation) is fast becoming the most popular format for such information exchange. JSON is designed for JavaScript and is quick and easy to process. This makes it an excellent choice for web services and AJAX requests. Along with minimal size, the JSON is also quick to decode and thus suitable for mobile applications as well.

PHP makes it simple to read and write JSON with simple in-built functions. The json_encode() function writes the JSON, while json_decode() allows reading it.

$companies = array(array("name" => "Google", "website" => "www.google.com"), array("name" => "Microsoft", "website" => "www.microsoft.com") array("name" => "Yahoo", "website" => "www.yahoo.com"));

$jsonString =  json_encode($companies);

echo $jsonString;

/* Output:[{ "name":"Google","website":"www.google.com"}, {"name":"Microsoft","website":"www.microsoft.com"}, {"name":"Yahoo","website":"www.yahoo.com"}] */

$phpArray = json_decode($jsonString);


/* Output:



[0] => Array


[name] => Google

[website] => www.google.com


[1] => Array


[name] => Microsoft

[website] => www.microsoft.com


[2] => Array


[name] => Yahoo

[website] => www.yahoo.com


Above is a simple example for converting a static/hardcoded PHP array into a JSON string.

Take a look at another example of converting MYSQL resultset into JSON.

 $link = mysql_pconnect("db_server", "db_user", "db_password") or die("Could not connect");

mysql_select_db("database") or die("Could not select database");

$companies = array();

$rs = mysql_query("SELECT name, website FROM companies");

while($obj = mysql_fetch_object($rs)) {

$companies[] = $obj;


echo '{"companies":'.json_encode($companies).'}';

Looks quite easy! Yet, it is not so simple.

JSON in PHP 5.2

PHP 5.2 is bundled with compiled JSON extension. The above example will work for PHP 5.2 and higher. The problem here is that many Linux distributions are still shipped with 5.1.x.  Since, PHP version 5.3, a second optional parameter is added to both json_encode() and json_decode() functions. This optional parameter allows you to define the bitmasking options like JSON_NUMERIC_CHECK, JSON_UNESCAPED_UNICODE etc.


For older versions of PHP, you may have to use PEAR JSON package and create an instance of the package class. You will be required to include the package file before using encode and decode operation. The sample encode and decode functions for the PEAR package are

 include_once('JSON.php'); // Include package

$json = new Services_JSON(); // Create an instance of package class


echo '{"companies":'.$json->encode($companies).'}';

If you have root access, then you may use the Unix version of PECL extension or the Windows version of the PECL version. Other available options are Zend JSON, which is a part of Zend framework and XMLRPC JSON, which belong to the PHP-XMLRPC library. A comparison between popular JSON libraries can be found at http://gggeek.altervista.org/sw/article_20061113.html. You can also Google and find various simple JSON wrappers and alternatives.

6 Responses to “JSON with PHP – JSON Encode and Decode”

  1. Anonymous

    I simply love cheat sheets.jQuery has so many things and this visual cheat sheet is really helpful for quick reference. I once posted JSON Cheatsheet


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>