Magento 2.4.5 Upgrade Error in PHP 8.1: json_decode() Passing Null to Parameter #1

deprecatedjsonmagento2.4.5

I am facing after an issue after upgrading version magneto 2.4.2 to 2.4.5 When I run this command I got error

json_decode(): Passing null to parameter #1 ($json) of type string is deprecated

app/code/Vendor/Module/Helper/Data.php(495): json_decode(NULL)

Best Answer

Try below code 1. Add string (string)

public function getParametersAttribute()
{
   return $cookieValue=json_decode((string)$cookieValue);
}

2. Add condition

public function getParametersAttribute(){
if (isset($cookieValue)) {
   return $cookieValue=json_decode($cookieValue);
 }else{
    return [];
  }
}

3. Try this one

public function getParametersAttribute(){
   return $cookieValue= json_decode($cookieValue ?? '');
}
Related Topic