PHP JSON Functions | Encode and Decode JSON in PHP

JSON stands for JavaScript Object Notation. It is a lightweight format to store and transport data. PHP supports the use of JSON strings by default. In this article, we will discuss PHP JSON Functions to encode and decode objects to a JSON string.

Sample JSON

A sample JSON string looks something like this:

{
	"Users": [{
		"First": "Chandler",
		"Last": "Bing"
	}, {
		"First": "Rachael",
		"Last": "Green"
	}, {
		"First": "Ross",
		"Last": "Geller"
	}]
}

We can convert a PHP array or object to a JSON string and vice versa using PHP JSON Functions.

Encoding Objects to JSON

You can use PHP json_encode Function to convert an object/array to JSON in PHP. It returns a JSON string on success. However, it returns false on failure. For instance, the following code demonstrates the use of the json_encode function.

<?php
	
	$testArray = array(
		'Users' => array(
			array('First' => 'Chandler', 'Last' => 'Bing'),
			array('First' => 'Rachael', 'Last' => 'Green'),
			array('First' => 'Ross', 'Last' => 'Geller')
		)
	);
	echo json_encode($testArray);
?>

The above code will produce the same JSON as shown in the sample section.

{
"Users": [{
"First": "Chandler",
"Last": "Bing"
},
{
"First": "Rachael",
"Last": "Green"
},
{
"First": "Ross",
"Last": "Geller"
}
]
}

Similarly, you can convert class objects to JSON also.

<?php

	class User {
		public $name;
		public $age;
	}
	$user = new User();
	$user->name = 'Chandler';
	$user->age = 26;
	echo json_encode($user);

?>

The above code produces the following JSON string:

{"name":"Chandler","age":26}

Decoding JSON String

You can convert a JSON String back to the original object using PHP json_decode Function. The function returns an object or array based on the second parameter.

<?php
$jsonString = '{"Users":[{"First":"Chandler","Last":"Bing"},{"First":"Rachael","Last":"Green"},{"First":"Ross","Last":"Geller"}]}';

print_r(json_decode($jsonString, true));
print_r(json_decode($jsonString));

?>

The above example produces the following result:

Array
(
[Users] => Array
(
[0] => Array
(
[First] => Chandler
[Last] => Bing
)
[1] => Array
(
[First] => Rachael
[Last] => Green
)
[2] => Array
(
[First] => Ross
[Last] => Geller
)
)
)


stdClass Object
(
[Users] => Array
(
[0] => stdClass Object
(
[First] => Chandler
[Last] => Bing
)
[1] => stdClass Object
(
[First] => Rachael
[Last] => Green
)
[2] => stdClass Object
(
[First] => Ross
[Last] => Geller
)
)
)

Note: The json_decode function returns an array when the second parameter is passed as true. However, by default it returns an stdClass Object.

Why Use JSON?

Since JSON is only a text string, it can be easily sent from one server to another. Also, it can be used as a data format in any programming language. JSON is widely using in REST APIs as well.

Conclusion

In conclusion, we discussed using JSON in PHP. The two functions json_encode and json_decode can be used to convert objects to strings and back. JSON is very helpful and can be used as a data format to communicate between servers or different programming languages. You can read more about JSON on Wikipedia.

Also, you can learn more about PHP on Concatly!

References:

Spread the Knowledge

Leave a Reply

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