How to Create and Parse JSON Data with 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 json_encode() and json_decode() functions. But, take into account that these functions only operate with string data, operated only with UTF-8.

Watch a course Learn object oriented PHP

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:

<?php

// An associative array
$marks = ["John" => 68, "George" => 75, "David" => 65, "Roger" => 40];

echo json_encode($marks);

?>

Its output will look as follows:

{"John":68,"George":75,"David":65,"Roger":40}

In the same way, an indexed PHP array may be encoded into an array of JSON:

<?php
// An indexed array
$colors = ["Blue", "White", "Green", "Black", "Yellow"];

echo json_encode($colors);

?>

And, the output is the following:

["Blue","White","Green","Black","Yellow"]

An important note: while an associative array may get encoded as an object, a non-associative one- both as an object and an 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:

<?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:

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 json_decode() function. However, an optional parameter such as $assoc can be indicated. Here is how it can look like:

<?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:

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:

<?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:

<?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.