JSON is commonly used in web applications to send and receive data across different web applications that is, sending data from a web server to a client application so that it can be displayed to users.
JSON is a short form for JavaScript Object Notation which is basically an approved way of representing text data
JSON is popular and widely used since it is a lightweight text format for transmitting information between different web clients
In this article, we shall discuss how you can use json_encode and json_decode functions that are supported in PHP to read, create and convert data to a JSON format
JSON Encode
In PHP, JSON encode simply means converting a set of data which is either an array or an object to a representation that is JSON supported
For example, to display data that is stored in an array to the web browser, you have to either convert it to JSON or get individual array elements since when you try to output an array it will be displayed as an Array other than the components inside the array
Let’s get an example;
If we have a data in an array as this which is data of a student
array("name"=>"John Doe","class"=>"fourth year","age"=>12)
if you try to display the output of the array in PHP
<?php
$student_data=array("name"=>"John Doe","class"=>"fourth year","age"=>12);
echo $student_data;
?>
The result will be an Array other than the actual data and also you will also get an error
PHP Notice: Array to string conversion in /home/tMBt6W/prog.php on line 4
To display the components of the array in the browser and also fix the error PHP Notice: Array to string conversion in … we use the PHP function json_encode() whereby we convert the array into a JSON object which can be read by browsers and also can be easily accessed
To encode data in PHP using json_encode() function, the syntax is as below
Json_encode(array)
In the example above we use json_encode() function as follows
<?php
$student_data=array("name"=>"John Doe","class"=>"fourth year","age"=>12);
echo json_encode($student_data);
?>
The result for the above will be;
{"name":"John Doe","class":"fourth year","age":12}
Which will have converted the array into a JSON data format object
JSON Decode
JSON decode works as the reverse of JSON encode whereby it collects a JSON encoded object and converts it either into an array or any other format which you may like to work with and finally, it enables you to access elements that are inside the JSON encoded object
From the example of the result, we have shown above that is gotten after we have used json_encode() function, {"name":"John Doe","class":"fourth year","age":12}, you cannot be able to access the individual elements when it is like that
Remember what we said from the introduction, that the main purpose of JSON is to allow data transmission from the server to the web client.
For you to get the individual components of data present in a JSON object, we use the json_decode() function
But first, before we use the json_decode function, in most cases where data is received by your server from another application when an activity has happened, for example, when a payment has been made and you have authorized payment notifications, you receive data as a JSON format
To access the JSON format data, we use the file_get_contents() function as follows
<?php
$json_data=file_get_contents(json_data);
?>
Then, we use json_decode function to convert the data from a single object into an array. The syntax for json_decode() in PHP is as follows
json_decode(json_object,boolean)
The values in the Boolean can either be true, false, or null
When the value is set to true the objects in the JSON will be returned as associative arrays
When the value is set to false the objects in the JSON will be returned as objects
When the value is set to null the objects in the JSON will be returned as associative arrays or objects depending on whether the parameter JSON_OBJECT_AS_ARRAY had been declared in the flags
In this case, we shall use false as shown below
<?php
//to encode array
$student_data=array("name"=>"John Doe","class"=>"fourth year","age"=>12);
//encoded data
$encoded_data= json_encode($student_data);
//to decode data
$decoded_data=json_decode($encoded_data,false);
?>
To access individual elements from the array like name, class and age in the above example we use the following code
<?php
//to encode array
$student_data=array("name"=>"John Doe","class"=>"fourth year","age"=>12);
//encoded data
$encoded_data= json_encode($student_data);
//to decode data
$decoded_data=json_decode($encoded_data,false);
//getting individual elements
echo $name=$decoded_data->name;
echo $name=$decoded_data->class;
echo $name=$decoded_data->age;
?>
That’s how we parse JSON data using json_encode() and json_decode() functions in PHP, you can use as many examples as possible so that you can understand how well JSON is important while storing and sending data between different or same web applications
Thank you for following this article