Solved: “Unable to Unserialize Value” in Magento 2.2
Ever faced an error like “unable to unserialize value” while working with system configuration in Magento 2.2 admin panel? It is because of the \Magento\Framework\Serialize\Serializer\Json class.
The file vendor/magento/framework/Serialize/Serializer/Json.php is causing the problem. The function unserialize($string) returns an exception if the string is already serialized.
Solution for “Unable to Unserialize Value” in Magento 2.2:
Open vendor/magento/framework/Serialize/Serializer/Json.php file.
Find the following funcion in file.
1 2 3 4 5 6 7 8 |
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; } |
Modify the function by adding the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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; } |
Add is_serialized() function in same file vendor/magento/framework/Serialize/Serializer/Json.php at the end:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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; } |
Unserialize the previous values and serialize them back with json. Implement the above code to solve the error “Unable to Unserialize Value” in Magento 2.2.
Use the Comments section below to post any doubts in the above solution. I’d be happy to help 🙂
Do not forget to rate the post with 5 stars!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Worked on Magento 2.4.3
Thank you so much
Hello Simon,
That is good to know that the above solution helped you.
Thank You
Hello, we have version 2.4.2 and the solution does not work, can you help me?
Hello Juan,
If the above code doesn’t work with your store then you need to debug it to find the issue in detail.
Thank You
Didn’t work for me 🙁
Hello,
Please try this:
In vendor/magento/framework/Serialize/Serializer/Serialize.php
Go to line number 42, function unserialize($string)
Find:
$result = unserialize($string, ['allowed_classes' => false]);
and replace it with:
$result = @unserialize($string, ['allowed_classes' => false]);
That’s it.
I hope it helps.
does this troubleshot work on magento 2.3.3 ??
Yes Anjani, the same solution works for Magento 2.3.3