Thursday, 3 January 2013

Encoding and Decoding JSON Data in Php | Php Tutorials For Beginners

In this tutorial of php code for beginners we will look at what is json and how json encode and json decode work with example.

What is JSON:- 
JSON also known as JavaScript Object Notation is a easy way to serialize data for passing through mediums that do not recognized complex data types. JSON is becoming the most popular format for information exchange. Because JSON is widely used for exchanging information across websites and web applications. The web services send and accept serialized data.  JSON is designed for JavaScript. This makes it an excellent choice for web services and AJAX requests. PHP 5.2 is bundled with compiled JSON extension.

Json encode is used for encoding the data that contain string, numbers etc. json_encode return the value that is recognized by JSON. On other hand we use json_decode to decode this json representational value and use in our code.

Json decode is used for decoding the value send by the json_encode.

Json process is divided in to two parts:
In first part we create json data using php array and encode this with json_encode.
In second part we receive the the json encoded data and decode this with help of json_decode and will use that data in our php code.

Part 1:-
$data = array('php'=>3, 'code'=>4, 'beginner'=>22,'java'=>array("javascript","jquery"));
echo json_encode($data);

Part 2:-
echo $object->php; //It will show 3
echo $object->java[0]; //It will show javascript

Hope this php tutorial is useful for you. Keep following for more help.


  1. how will you pass the encoded data? what will the url be look like? for example, i have a form, serialize the inputs and submitted it to getData.php.