How to Create and Parse JSON Data with PHP
JSON is a data-interchange format, which can be generated and parsed by PHP or other programming languages. See how to create and parse JSON data by PHP.
In this snippet, you can find a step-by-step guide on how to create and parse JSON data with PHP. Follow the steps and you’ll manage to meet your goal easily.
The data structures of JSON are identical to PHP arrays. There exist specific built-in functions that allow encoding and decoding JSON data. They are <kbd class="highlighted">json_encode()</kbd> and <kbd class="highlighted">json_decode()</kbd> functions. But, take into account that these functions only operate with string data encoded in UTF-8.
In PHP, the first function is applied for encoding a value into the format of JSON. In the example below, you can see the process of encoding an associative PHP array into an object of JSON:
encode php associative array to JSON object
<?php
// An associative array
$marks = ["John" => 68, "George" => 75, "David" => 65, "Roger" => 40];
echo json_encode($marks);
?>Its output will look as follows:
Console output:
{"John":68,"George":75,"David":65,"Roger":40}In the same way, an indexed PHP array may be encoded into an array of JSON:
encode php indexed array to JSON array
<?php
// An indexed array
$colors = ["Blue", "White", "Green", "Black", "Yellow"];
echo json_encode($colors);
?>And, the output is the following:
Console output:
["Blue","White","Green","Black","Yellow"]An important note: while an associative array is encoded as a JSON object, an indexed (non-associative) array is encoded as a JSON array.
For parsing a JSON data you can use decoding. It is as simple as encoding. It is mainly applied for converting a JSON encoded string into a matching data type of PHP.
For a better understanding, check out the example below:
convert json string to php
<?php
// Storing JSON data inside a PHP variable
$json = '{"George":63,"David":75,"Harold":60,"Jack":80}';
var_dump(json_decode($json));
?>Here is the output of the example above:
Console output:
object(stdClass)#1 (4) { ["George"]=> int(63) ["David"]=> int(75) ["Harold"]=> int(60) ["Jack"]=> int(80) }So, by default, an object is returned by the <kbd class="highlighted">json_decode()</kbd> function. However, an optional parameter such as $assoc can be indicated. Here is how it can look like:
json decode php
<?php
// Store JSON data in a PHP variable
$json = '{"George":63,"David":75,"Harold":60,"Clark":90}';
var_dump(json_decode($json, true));
?>Its output will be the following:
Console output:
array(4) { ["George"]=> int(63) ["David"]=> int(75) ["Harold"]=> int(60) ["Clark"]=> int(90) }In the next example, it is illustrated how to decode the data of JSON, accessing individual elements of an array or an object:
decode json string to php variable
<?php
// Assigning JSON encoded string into a PHP variable
$json = '{"George":63,"David":75,"Harold":60,"Clark":90}';
// Decode JSON data to PHP associative array
$arr = json_decode($json, true);
// Accessing values from the associative array
echo $arr["George"]; // Output: 63
echo $arr["David"]; // Output: 75
echo $arr["Harold"]; // Output: 60
echo $arr["Clark"]; // Output: 90
// Decoding JSON data to a PHP object
$obj = json_decode($json);
// Access values from the returned object
echo $obj->George; // Output: 63
echo $obj->David; // Output: 75
echo $obj->Harold; // Output: 60
echo $obj->Clark; // Output: 90
?>Also, the decoded data may be looped through using the foreach() loop in this way:
json loop decoded data
<?php
// Assigning JSON encoded string to a PHP variable
$json = '{"George":63,"David":75,"Harold":60,"Clark":90}';
// Decode JSON data to PHP associative array
$arr = json_decode($json, true);
// Loop through the associative array
foreach ($arr as $key => $value) {
echo $key . "=>" . $value . "<br>";
}
echo "<hr>";
// Decode JSON data to PHP object
$obj = json_decode($json);
// Loop through the object
foreach ($obj as $key => $value) {
echo $key . "=>" . $value . "<br>";
}
?>Describing JSON
JSON or the JavaScript Object Notation is a data-interchange format allowing to parse and create straightforwardly. It is a text-based format, simple in writing and understanding for computers and humans.
JSON stands on two primary structures: object and array. The first one is known as a collection of value/key pairs. The second one is an ordered value-list. Keys are always considered strings in the JSON format, while the values can be something like an object, array, string, number, and so on.