Handling JSON Invalidity in PHP
It happens that a string with JSON
is built incorrectly. In this
case, the json_decode function
will return null. Let's
try it in practice.
Let's make invalid JSON (determine
for yourself what's wrong with it):
<?php
$json = '["a", "b", "c",]';
?>
Let's try to parse this JSON:
<?php
$data = json_decode($json);
var_dump($data); // will output null
?>
Given a string with some JSON. Parse it into a PHP data structure. Output the parsing result or an error if parsing the JSON failed.