{"id":363,"date":"2019-04-08T07:20:32","date_gmt":"2019-04-08T07:20:32","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/04\/08\/solved-unable-to-unserialize-value-in-magento-2-2\/"},"modified":"2025-05-22T16:59:18","modified_gmt":"2025-05-22T11:29:18","slug":"solved-unable-to-unserialize-value-in-magento-2-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/solved-unable-to-unserialize-value-in-magento-2-2\/","title":{"rendered":"Solved: &#8220;Unable to Unserialize Value\u201d in Magento 2.2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ever faced an error like &#8220;unable to unserialize value&#8221; while working with system configuration in Magento 2.2 admin panel? It is because of the <strong>\\Magento\\Framework\\Serialize\\Serializer\\Json<\/strong>&nbsp;class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The file &nbsp;<strong>vendor\/magento\/framework\/Serialize\/Serializer\/Json.php <\/strong>is causing the problem. The function <strong>unserialize($string)<\/strong>&nbsp;returns an exception if the string is already serialized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution for &#8220;Unable to Unserialize Value&#8221; in Magento 2.2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Open <strong>vendor\/magento\/framework\/Serialize\/Serializer\/Json.php<\/strong> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Find the following funcion in file.<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    public function unserialize($string)\n    {\n      $result = json_decode($string, true);\n      if (json_last_error() !== JSON_ERROR_NONE) {\n        throw new \\InvalidArgumentException('Unable to unserialize value.');\n      }\n       return $result;\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Modify the function by adding the following code:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    public function unserialize($string)\n    {\n        \/* Added this code to resolve the issue *\/\n        if($this->is_serialized($string)){\n           $string = $this->serialize($string);\n        }\n    \/*---------------------------------*\/\n        $result = json_decode($string, true);\n        if (json_last_error() !== JSON_ERROR_NONE) {\n         throw new \\InvalidArgumentException('Unable to unserialize value.');\n        }\n        return $result;\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add<strong> is_serialized() function <\/strong>in same file<strong> vendor\/magento\/framework\/Serialize\/Serializer\/Json.php<\/strong> at the end:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    function is_serialized($value, &amp;$result = null)\n    {\n    \/\/ Bit of a give away this one\n    if (!is_string($value)) {\n        return false;\n    }\n    \/\/ Serialized false, return true. unserialize() returns false on an\n    \/\/ invalid string or it could return false if the string is serialized\n    \/\/ false, eliminate that possibility.\n    if ($value === 'b:0;') {\n        $result = false;\n        return true;\n    }\n    $length = strlen($value);\n    $end = '';\n    switch ($value[0]) {\n        case 's':\n            if ($value[$length - 2] !== '\"') {\n                return false;\n            }\n        case 'b':\n        case 'i':\n        case 'd':\n            \/\/ This looks odd but it is quicker than isset()ing\n            $end .= ';';\n        case 'a':\n        case 'O':\n            $end .= '}';\n            if ($value[1] !== ':') {\n                return false;\n            }\n            switch ($value[2]) {\n                case 0:\n                case 1:\n                case 2:\n                case 3:\n                case 4:\n                case 5:\n                case 6:\n                case 7:\n                case 8:\n                case 9:\n                    break;\n                default:\n                    return false;\n            }\n        case 'N':\n            $end .= ';';\n            if ($value[$length - 1] !== $end[0]) {\n                return false;\n            }\n            break;\n        default:\n            return false;\n    }\n    if (($result = @unserialize($value)) === false) {\n        $result = null;\n        return false;\n    }\n    return true;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unserialize the previous values and serialize them back with json.&nbsp;Implement the above code to solve the error<i><strong> &#8220;Unable to Unserialize Value&#8221; in Magento 2.2<\/strong><\/i>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever faced an error like &#8220;unable to unserialize value&#8221; while working with system configuration in Magento 2.2 admin panel? It is because of the \\Magento\\Framework\\Serialize\\Serializer\\Json&nbsp;class&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-363","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":15477,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions\/15477"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}