PHP

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 🙂

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

4 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago