PHP: Read Write JSON

JSON stands for JavaScript Object Notation. It is a light-weight text-based open standard designed for human-readable data. It is language-independent, with parsers available for many languages.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML. [via Wikipedia:JSON]

JSON.com has a very good JSON basics with examples.

In this article, I will be showing how to convert PHP array into JSON format and then convert JSON string into PHP type.

PHP function json_encode is used to convert PHP array to JSON string.
PHP function json_deocde is used to decode a JSON string and convert it into a PHP variable.

Working with Single Dimensional Array

Suppose we have a simple single dimensional array:-

$mobiles = array('Samsung', 'Apple', 'Nokia', 'Sony', 'LG');

We encode the $mobiles array into JSON format using json_encode function:-

$jsonMobiles = json_encode($mobiles);

When we print $jsonMobiles string, we get the following output:-


echo $jsonMobiles;

// Output:-
// ["Samsung","Apple","Nokia","Sony","LG"]

Now, we use json_decode function to decode the JSON string and change it to PHP array.

From above, we have $jsonMobiles as a JSON string.

The following code will decode the JSON string and change it to PHP array:-


$phpMobiles = json_decode($jsonMobiles);
print_r($phpMobiles);

// Output:-
// Array ( [0] => Samsung [1] => Apple [2] => Nokia [3] => Sony [4] => LG ) 

COMPLETE CODE


// encoding php array to json string
$mobiles = array('Samsung', 'Apple', 'Nokia', 'Sony', 'LG');
$jsonMobiles = json_encode($mobiles);
echo $jsonMobiles;

// decoding json string to php array
$phpMobiles = json_decode($jsonMobiles);
print_r($phpMobiles);

Read & Write JSON data to a file

Saving JSON data into a file


$mobiles = array('Samsung', 'Apple', 'Nokia', 'Sony', 'LG');
$jsonMobiles = json_encode($mobiles);
file_put_contents('mobiles.json', $jsonMobiles);

Reading JSON data from a file


$fileName = 'mobiles.json';
$data = file_get_contents($fileName);
$phpMobiles = json_decode($data);
print_r($phpMobiles);

Working with Multi Dimensional Array

Encoding

Suppose, we have a multi dimensional PHP array. We convert the array into JSON format using json_encode function.


$products = array(
				'name'	=> 'Mukesh Chapagain',
				'address'	=> 'Kathmandu, Nepal',
				'website'	=> 'https://blog.chapagain.com.np',
				'mobiles' 	=> array('Samsung', 'Apple', 'Nokia', 'Sony', 'LG'),
				'furniture' => array('Chair', 'Couch', 'Ottoman'),
				'camera'	=> array('Canon', 'Nikon')
			);
$jsonProducts = json_encode($products);
echo $jsonProducts;

Decoding

We convert the JSON string back to PHP object with json_decode function. And then print the PHP object.


$phpProducts = json_decode($jsonProducts);
print_r($phpProducts);
echo "<br>";

// printing name, address, and website
echo "Name: " . $phpProducts->name . "<br />";
echo "Address: " . $phpProducts->address . "<br />";
echo "Website: " . $phpProducts->website . "<br />";

// printing mobiles name
foreach ($phpProducts->mobiles as $key => $value) {	
	echo $value . "<br />";
}

Hope it helps. Thanks.