23

I am unable to handle JSON decode errors. Here is my code:

try {
    $jsonData = file_get_contents($filePath) . ']';
    $jsonObj  = json_decode($jsonData, true);
}
catch (Exception $e) {
    echo '{"result":"FALSE","message":"Caught exception: ' . $e->getMessage() . ' ~' . $filePath . '"}';
}

I am a new PHP programmer. Sorry, if something is wrong.

Valerio Bozz
  • 1,176
  • 16
  • 32
vineet
  • 13,832
  • 10
  • 56
  • 76

4 Answers4

35

Another way to handle json decode error:-

if ($jsonObj === null && json_last_error() !== JSON_ERROR_NONE) {
   echo "json data is incorrect";
}
29

Since PHP 7.3 one can use the JSON_THROW_ON_ERROR constant.

try {
    $jsonObj = json_decode($jsonData, $associative=true, $depth=512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
    // handle exception
}

More: https://www.php.net/manual/de/function.json-decode.php#refsect1-function.json-decode-changelog

Valerio Bozz
  • 1,176
  • 16
  • 32
sergej
  • 1,077
  • 1
  • 14
  • 20
10

May be you can try, validating json_decode

try {
  $jsonData = file_get_contents($filePath) . ']';
  $jsonObj  = json_decode($jsonData, true);

  if (is_null($jsonObj)) {
    throw ('Error');
  }
} catch (Exception $e) {
  echo '{"result":"FALSE","message":"Caught exception: ' . 
    $e->getMessage() . ' ~' . $filePath . '"}';
}

Read this too

b00t
  • 409
  • 3
  • 10
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
  • 5
    Why call `is_null()` when you can just check if `$jsonObj === null`? – reformed Aug 18 '17 at 13:52
  • There is no difference between comparing is_null() and === null. I would say its just a preference. Check this one https://stackoverflow.com/questions/8228837/is-nullx-vs-x-null-in-php for more information. – Milen Gardev Apr 28 '20 at 17:13
  • @MilenGardev the answer you linked to does not state "there is no difference". It in fact lists the differences, and thus which one to chose depends on the scenario. If you do not need a callback then there's no sense in not using the more performant `=== null` than the function which has additional overhead. – James Oct 22 '21 at 15:11
  • @James I would challenge you to benchmark the 2 cases side by side over 1000 iterations. The performance difference is so negligible that it almost won't even be measurable with microtime. Even in an environment that needs to be highly optimized, this wouldn't even make my list. – Lulceltech Jun 29 '22 at 17:51
  • "If you do not need XYZ then there's no sense in not using the more performant" – James Jun 30 '22 at 14:38
4

json_decode returns null when a error occurs, like no valid json or exceeded depth size. So basically you just check with if whether the jsondata you obtained is null or not. If it is, use json_last_error to see what went wrong, if not then continue with the script.

$json_data = json_decode($source, true);

if($json_data == null){
  echo json_last_error() . "<br>";
  echo $source; // good to check what the source was, to see where it went wrong
}else{
  //continue with script
}

Something like that should work.

Verkade89
  • 373
  • 2
  • 12
  • nope, when i exec json_decode on bad content, other code is useless, php return "Exception" and nothing i can do ! – Vladimir Ch Aug 26 '22 at 12:39