cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3 - Unable to unserialize value. Error: Syntax error

Magento 2.3 - Unable to unserialize value. Error: Syntax error

How to solve this error : 
Unable to unserialize value. Error: Syntax error

2 REPLIES 2

Re: Magento 2.3 - Unable to unserialize value. Error: Syntax error

@Aveeva 

 

You can follow below shared solutions in this it seems can be related to di compile or redis or might you need to modify generated.php file

 

Here you go:

https://github.com/magento/magento2/issues/21957

https://magento.stackexchange.com/questions/194010/magento-2-2-unable-to-unserialize-value

Manish Mittal
https://www.manishmittal.com/

Re: Magento 2.3 - Unable to unserialize value. Error: Syntax error

Hi @Aveeva 

Override file with your custom module :

/vendor/magento/framework/Serialize/Serializer/Json.php

Add a condition to check  if string is serialized or not with "function unserialize($string)" :

public function unserialize($string)
{
    if($this->is_serialized($string))
    {
        $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
         throw new \InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
}

Try to  add function to check if string is serialized:

function is_serialized($value, &$result = null)
{
    if (!is_string($value))
    {
        return false;
    }
    if ($value === 'b:0;')
    {
        $result = false;
        return true;
    }
    $length = strlen($value);
    $end    = '';
    switch ($value[0])
    {
        case 's':
            if ($value[$length - 2] !== '"')
            {
                return false;
            }
        case 'b':
        case 'i':
        case 'd':
            $end .= ';';
        case 'a':
        case 'O':
            $end .= '}';
            if ($value[1] !== ':')
            {
                return false;
            }
            switch ($value[2])
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    break;
                default:
                    return false;
            }
        case 'N':
            $end .= ';';
            if ($value[$length - 1] !== $end[0])
            {
                return false;
            }
            break;
        default:
            return false;
    }
    if (($result = @unserialize($value)) === false)
    {
        $result = null;
        return false;
    }
    return true;
}


It may resolve your issue.
If issue resolve, please click on 'Kudos' & Accept as Solution!