Here, we are going to see how JSON encoding and decoding are performed in PHP with some examples. JSON encode and decode most popular and required features used in Backend development. It arranges the data in a specific manner so that we can understand and perform the data related operations in a proper way.
What is Encoding and Decoding?
Encoding and Decoding are the pair of functions that are performed in data in backend programming. Encoding is the method of bundling the data in a particular format. Decoding is precisely the reverse process of encoding. It converts the encoded data back to the original form.
Index
In PHP, we have predefined functions to perform JSON encode and decode operations on PHP objects.
They are:
It is used to convert the PHP objects into the JSON objects. It returns the result of the encoding operation.
Syntax
json_encode($data);
In the above syntax, the data must be passed as an object or as an array.
Example 1
Suppose we have an object of a student having the name, age, and college, etc and we want to encode the data using json_encode().
Let’s see how it can be performed in Core PHP:
<?php $Student->name = "Happy"; $Student->age = 20; $Student->college = "PDPU"; $encoded_JSON = json_encode($Student); echo $encoded_JSON; ?>
Output
{“name”:”Happy”,”age”:20,”college”:”PDPU”}
Explanation
Example 2
Furthermore, suppose we have an array of animals and we want to encode the data using json_encode(). Let’s see how it can be performed:
<?php $animals = array("Dog", "Cat", "Lion", "Elephant"); $encoded_JSON = json_encode($animals); echo $encoded_JSON; ?>
Output
[“Dog”,”Cat”,”Lion”,”Elephant”]
Explanation
It is the reverse process of json_encode(). It converts the JSON object into a PHP object so that we can perform further operations on that data.
Syntax
json_decode($data,$bool);
In the above syntax, the data can be passed as an object or as an array, and bool is a boolean value passed as a parameter.
Note: When the boolean value is true, the json_decode gives the decoded data as an array.
Example 1
Suppose we have an JSON object of an animals and we want to decode the data using json_decode(). Lets see how it can be performed:
<?php $animals = array("Dog", "Cat", "Lion", "Elephant"); $encoded_JSON = json_encode($animals); $decoded_JSON = json_Decode($encoded_JSON); echo "<pre>"; print_r($decoded_JSON); ?>
Output
Array
(
[0] => Dog
[1] => Cat
[2] => Lion
[3] => Elephant
)
Explanation
Example 2
In addition, consider we have a JSON object of a student having the name, age, and college, etc and we want to decode the data using json_decode(). Let’s see how it can be performed:
<?php $Student->name = "Happy"; $Student->age = 20; $Student->college = "PDPU"; $encoded_JSON = json_encode($Student); $decoded_JSON = json_decode($encoded_JSON); echo "<pre>"; print_r($decoded_JSON); ?>
Output
stdClass Object
(
[name] => Happy
[age] => 20
[college] => PDPU
)
Read Also: How to Create a Simple REST API in PHP
This is all about the Encoding and Decoding of JSON data with examples.
I am sure you have no doughts about the use of json_encode() and json_decode() functions.
I hope you found this post informative.
Thanks for reading 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…