Subtract

Parsing JSON with PHP

Parsing JSON with PHP

The function `json_decode()` can be used to convert JSON data to PHP variables. Arrays as well as objects can be converted to their equivalent PHP literals.

Here are some examples :

JSON Arrays

// Hello World
$json = '["Hello","World"]'; 
$array = json_decode($json);

echo($array[0]); // Hello
$json = [ 100, 500, 300, 200, 400 ] ; 
$array = json_decode($json);

echo($array[3]); // 200
echo($array[1]); // 500

JSON Objects

JSON objects are converted to PHP objects by default

$jsonObject = '{
                    "Name":"John",
                    "Surname":"Doe"
                }';

$obj = json_decode($jsonObject);
echo($obj->Surname);   // Doe

The function `json_decode()` provides an optional boolean second-argument that if passed as `true` converts objects into PHP associative arrays. Using the example from above, and decoding it as such, the elements can be accessed using the respective keys.

$jsonObject = '{
"Name":"John",
"Surname":"Doe"
}';

$assocArray = json_decode($jsonObject, true); 

// Accessing elements using standard array syntax
echo($assocArray['Name']); // John

Handling Multi-dimensional Objects

`json_decode()` can recursively decode JSON strings. The third optional argument tells the function the depth to which decoding should happen.

Consider a JSON string that represents an indexed array of objects.

$json = '[ {"id":"1", "name":"foo" }, {"id":"2", "name":"bar" } ]';
$baz = json_decode($json);

echo($baz[0]->name); // foo

If true were to be passed as the second argument to `json_decode()` above, one would end up with a numerically indexed array of associative arrays.

$json = '[ {"id":"1", "name":"foo" }, {"id":"2", "name":"bar" } ]';
$baz = json_decode($json,true);

echo($baz[1]['name']); //bar

Iterating over Multidimensional arrays

Consider a JSON string that when decoded results in a multidimensional PHP literal. Such a literal can be efficiently iterated over using a `RecursiveIteratorIterator`.

$json = '{ "John":{ "gender":"male", "age":"12" }, "Jen":{ "gender":"female", "age":"13" } }';

$iter = new RecursiveIteratorIterator( new RecursiveArrayIterator(jsondecode($json,true)), RecursiveIteratorIterator::SELFFIRST);

foreach($iter as $key=>$value) { 
   if(is_array($value))
     { echo "$key:\n"; }
   else
     { echo "$key => $value\n"; } 
}

/*
The output will be :
John:
gender => male
age => 12

Jen:
gender => female
age => 13
*/

Handling Errors

`json_decode()` returns `NULL` if the JSON string cannot be decoded or the encoded data is deeper than the recursion limit. In such cases, the functions `json_last_error()` and `json_last_error_msg()` provide useful information.

$json = '["1"2]';

$var = json_decode($json);
var_dump($var); // NULL

echo json_last_error(); // 4
echo json_last_error_msg(); // Syntax error

References

  1. JSON Decode
  2. Recursive Iterator

Related Posts

Scroll to Top