Creating and Parsing JSON data with PHP
- Saturday, October 4, 2008, 18:10
- how-to, javascript, php, web services
- 13 comments
Yesterday, I was in a party and a guy came near to me and asked me what is JSON and how can handle it via PHP. Today, I’m going to tell you something about JSON data and how we can handle them via PHP. Although, JSON stands JavaScript Object Notation, it is used by many other technologies like PHP and Java for data interchange format over the Internet.
What is JSON?
JSON is ultra-weight data interchange data format used over the internet for transferring the data. While XML is a dominant data interchange format over the internet but JSON is less complex and light-weight data.
Though it was first made to be used with JavaScript for accessing remote data, it is now used by many other languages because JSON data is platform independent data format.
Data Types and Example of JSON data
JSON supports various kind of data types which included numbers, strings, booleans as well as array datas and obviously object (collection of key:value pairs, comma-separated and enclosed in curly brackets).
Now, let’s look at the example of simple format of JSON data for a detail of a employee,
{"id":"1","name":"mike","country":"usa","office":["microsoft","oracle"]}
Creating and Parsing JSON data format in PHP
To handle JSON data there is JSON extension in PHP which is aviable after PHP 5.2.0. Two funcitons : json_encode() and json_decode() are very useful converting and parsing JSON data through PHP.
First of all, let’s look at the PHP code to create the JSON data format of above example using array of PHP.
$json_data = array ('id'=>1,'name'=>"mike",'country'=>'usa',"office"=>array("microsoft","oracle"));
echo json_encode($json_data);
The above code generates the JSON data exactly as above. Now, let’s decode above JSON data in PHP.
$json_string='{"id":1,"name":"mike","country":"usa","office":["microsoft","oracle"]} ';
$obj=json_decode($json_string);
Now, the $obj variable contains JSON data parsed in PHP object which you can display using code below.
echo $obj->name; //displays mike echo $obj->office[0]; //displays microsoft
As you can guess,$obj->office is an array and you can loop through it using foreach loop of PHP,
foreach($obj->office as $val)
echo $val;
Popularity: 10%
Related Posts
» Parsing the XML in easy way using PHP
» Web Services and PHP - SOAP vs XML-RPC vs REST
» How did I reduce CPU overhead problem caused by MySql?
» How to filter user submitted data easily in PHP?






If you are using PHP 4, let me recommend my array2json() function.
Roshan,
Can we use JSON as an alternative to XML in each cases. Or the two has their own special case of application.
@Binny VA - cool function dude…..thanks for sharing this with us…
@shakeel - as a data transfer format JSON can be used to alternative upto some extent but namespaces and other functionality makes XML stronger ..
Roshan
In my view, both methods are usefull. JSON can be evaluated by through eval (fast, easy, you write a little aditional code), while XML requires an external source and is used for large volumes of data (you must write your function to examine the data). Both methods are equally effective, though JSON is more useful and easy for 80% of the procedures
@Felix - I agree with you man…
Good one!
I have also written on JSON in javascript:
JSON
Hey there, I thought JSON was mostly used to serialized connnections.
simple and straight to the point, never really had time to read up on JSON but this is a very good intro to the subject
Wouldn’t this be a good example too?
JSON, is an object string notation. For people who are familiar with the serialize function available in the scripting languages, JSON wouldn’t be anything new. The reason for using JSON instead of the native php serialization is that JSON will work across languages. The advantage JSON provides over traditional XML or mere url notation output is that, with JSON all interactions happens via objects, and you don’t need to do any output parsing.
@Ronald - thanks mate
@Carlos @ webbynode - I would say that is very very good example…
@Roshan - Thanks mate, I was a bit confused between the two ways
By the way, I’m sending you an email now.
Thanks