PHP Object to JSON Object [Encode & Decode]

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.

JSON Encode/Decode in PHP

In PHP, we have predefined functions to perform JSON encode and decode operations on PHP objects.

They are:

  • json_encode()
  • json_decode()

json_encode()

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

  • First, we created an object $Student and initialized its parameters like name, age, and college.
  • Then we encoded the object using json_encode() function and passed an object $Student as a parameter.

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

  • First, we initialized an array of $animals.
  • Then we encoded an array using json_encode() function and passed an array $animals as parameter.

json_decode()

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

  • First, we encoded an array of animals at $encoded_JSON.
  • Next we used the json_decode() function to decode the JSON object into PHP object and passed the $encoded_JSON as parameter.
  • Then we used the print_r() function to print the decoded array.
  • At last we used the <pre> tag to place the data in a readable form.

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

Conclusion

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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
php mysqli prepared statement
Prepared Statement in PHP With MySQLi

A prepared statement is one of the popular methods in PHP for database management. As a backend developer, it is...

Close