- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Magento 2.3 - Unable to unserialize value. Error: Syntax error
How to solve this error :
Unable to unserialize value. Error: Syntax error
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2.3 - Unable to unserialize value. Error: Syntax error
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
https://www.manishmittal.com/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2.3 - Unable to unserialize value. Error: Syntax error
Hello Aveeva,
Magento 2 serializes data using the serialize() method. However, some users faced an error like "unable to unserialize value" while working with system configuration. It's because of the Json class or the \Magento\Framework\Serialize\Serializer\Json class.
The error can appear when the file vendor/magento/framework/Serialize/Serializer/Json.php is causing the issue. The function unserialize($string) is used to check if the string is already serialized or not.
In order to resolve the error, you must follow the steps below,
- Firstly, open the below file,
vendor/magento/framework/Serialize/Serializer/Json.php
- Now, locate the following function in the file,
public function unserialize($string) { $result = json_decode($string, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \InvalidArgumentException('Unable to unserialize value.'); } return $result; }
- Then, modify the function by adding the below code,
public function unserialize($string) { /* Added this code to resolve the issue */ 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; }
- Lastly, add the is_serialized() function on the below file at the end,
vendor/magento/framework/Serialize/Serializer/Json.php
function is_serialized($value, &$result = null) { // Bit of a give away this one if (!is_string($value)) { return false; } // Serialized false, return true. unserialize() returns false on an // invalid string or it could return false if the string is serialized // false, eliminate that possibility. 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': // This looks odd but it is quicker than isset()ing $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; }
Thus, with the help of json, you can unserialize the previous values and serialize them back. I hope the above method helps you to fix the syntax error: unable to unserialize value in Magento 2.
--------------------------
Regards,
Rex M