This is an absolutely normal behaviour of json_decode
.
It will throw this exception if the given string is not a valid JSON String.
As you already mentioned, an empty string is also not a valid JSON String.
json_decode('Hello') // invalid
json_decode("Hello") //invalid
But:
json_decode("'Hello'") // valid JSON string
Empty strings will raise an exception since PHP7!
«Calling json_decode with 1st argument equal to empty PHP string or value that after casting to string is empty string (NULL, FALSE) results in JSON syntax error.»
So… to answer your question: In my opinion, your application has the problem you need to solve, if downgrading your PHP version is not an option.
The Magenta function needs to check for a valid representation of a JSON string BEFORE passing it to the json_decode
function.
Try to set the variable to {}
if it is not a string (is_string
) or if it is empty.
If this is not the problem, it might be possible that your string is not encoded as it should be. Many times encoding of the strings passed to json_decode
will result into that exception also.
I recently switched from Apache/PHP 5.5 to a new nginx/PHP 7.0.4 server and now I am getting once in a while a «Decoding failed: Syntax error» when customers try to checkout, apparently when they click the submit order button.
I am unable to reproduce the bug on my side, it happens in about 1 of 50 orders, sometimes also in the customer panel, so it seems to affect only logged users or SSL connections. As I am unable to reproduce the behaviour, I don’t know what is happening on the frontend, but customers seem to be able to click the button again, because in most cases the order gets through after 2 error messages. The store is currently running Magento 1.9.2.1 and the server Centos7.
I know that the error is generated in Json.php in the function «decode».
Does anyone have an idea what could cause this and how to debug this? One way would be to log all JSONS with an error and then look at the syntax, but maybe someone already has a solution?
Here is an excerpt of the trace:
#0 /usr/share/nginx/html/app/code/core/Mage/Core/Helper/Data.php(659): Zend_Json::decode('', 1)
#1 /usr/share/nginx/html/app/code/core/Mage/Persistent/Model/Session.php(133): Mage_Core_Helper_Data->jsonDecode(NULL)
#2 /usr/share/nginx/html/app/code/core/Mage/Core/Model/Abstract.php(226): Mage_Persistent_Model_Session->_afterLoad()
#3 /usr/share/nginx/html/app/code/core/Mage/Persistent/Model/Session.php(154): Mage_Core_Model_Abstract->load('deleted', 'key')
#4 /usr/share/nginx/html/app/code/core/Mage/Persistent/Helper/Session.php(67): Mage_Persistent_Model_Session->loadByCookieKey()
#5 /usr/share/nginx/html/app/code/core/Mage/Persistent/Helper/Session.php(91): Mage_Persistent_Helper_Session->getSession()
#6 /usr/share/nginx/html/app/code/core/Mage/Persistent/Model/Observer.php(177): Mage_Persistent_Helper_Session->isPersistent()
#7 /usr/share/nginx/html/app/code/core/Mage/Core/Model/App.php(1357): Mage_Persistent_Model_Observer->emulateQuote(Object(Varien_Event_Observer))
#8 /usr/share/nginx/html/app/code/core/Mage/Core/Model/App.php(1336): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Persistent_Model_Observer), 'emulateQuote', Object(Varien_Event_Observer))
#9 /usr/share/nginx/html/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('controller_acti...', Array)
#10 /usr/share/nginx/html/app/code/core/Mage/Core/Controller/Varien/Action.php(527): Mage::dispatchEvent('controller_acti...', Array)
#11 /usr/share/nginx/html/app/code/core/Mage/Core/Controller/Front/Action.php(69): Mage_Core_Controller_Varien_Action->preDispatch()
We have just encountered a JSON Syntax Error on our Magento CE v1.9.1.1 site when saving products in admin view. Product updates and changes will now not save at all, instead generating a ‘Zend_Json_Exception — Decoding failed: Syntax error’. No configuration or programming changes were made immediately prior to this error first occurring and the site has been running without issue for months. The MySQL database seems healthy (the current database runs without error on an earlier fileset). It appears the problem is somewhere in the fileset. This may have been triggered by running out of disk space, which occurred around the same time frame. Has anyone come across this specific error before and have any ideas how to fix it? Or, have any helpful suggestions how to debug and track it down?
The error Stack Trace is as follows:
2016-08-21T02:01:55+00:00 ERR (3): exception ‘Zend_Json_Exception’ with message ‘Decoding failed: Syntax error’ in /var/www/html/magentostore/lib/Zend/Json.php:97 Stack trace: #0 /var/www/html/magentostore/app/code/local/Magestore/Webpos/Model/Observer.php(223): Zend_Json::decode(‘{«3568»:1469961…’)
#1 /var/www/html/magentostore/app/code/core/Mage/Core/Model/App.php(1339): Magestore_Webpos_Model_Observer->catalogProductSaveAfter(Object(Varien_Event_Observer))
#2 /var/www/html/magentostore/app/code/core/Mage/Core/Model/App.php(1318): Mage_Core_Model_App->_callObserverMethod(Object(Magestore_Webpos_Model_Observer), ‘catalogProductS…’, Object(Varien_Event_Observer))
#3 /var/www/html/magentostore/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent(‘catalog_product…’, Array)
#4 /var/www/html/magentostore/app/code/core/Mage/Core/Model/Abstract.php(466): Mage::dispatchEvent(‘catalog_product…’, Array)
#5 /var/www/html/magentostore/app/code/core/Mage/Catalog/Model/Product.php(549): Mage_Core_Model_Abstract->_afterSave()
#6 /var/www/html/magentostore/app/code/core/Mage/Core/Model/Abstract.php(319): Mage_Catalog_Model_Product->_afterSave()
#7 /var/www/html/magentostore/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php(728): Mage_Core_Model_Abstract->save()
#8 /var/www/html/magentostore/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Adminhtml_Catalog_ProductController->saveAction()
#9 /var/www/html/magentostore/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch(‘save’)
#10 /var/www/html/magentostore/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#11 /var/www/html/magentostore/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#12 /var/www/html/magentostore/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#13 /var/www/html/magentostore/index.php(87): Mage::run(», ‘store’)
#14 {main}
We are running:
Linux Debian GNU/Linux 8 n l
Magento CE v1.9.1.1
PHP v5.6.17
Extensions installed include:
Magestore — Rewards Points 0.2.5, WebPOS 0.2.3, Gift Voucher 0.4.4 & OneStepCheckout 3.4.0
Ecomus Mediabox (YouTube Easy Light Box)
I’m glad you’ve flagged it because I thought it was something broken with my install. I’ve had no joy at all trying to track it down so your links could prove useful. This is an example of one of the errors I would see in the var/report folder — the user is seeing a 500 error page at this point, which since it’s happening during checkout is not very reassuring…
URL: https://www.somewebsite.co.uk:443/checkout/onepage/index/
IP Address: 11.22.33.44
Time: 2016-03-02 12:44:52 GMT
Error:
Decoding failed: Syntax error
Trace:
#0 /home/magento/public_html/app/code/core/Mage/Core/Helper/Data.php(659): Zend_Json::decode('', 1)
#1 /home/magento/public_html/app/code/core/Mage/Persistent/Model/Session.php(133): Mage_Core_Helper_Data->jsonDecode(NULL)
#2 /home/magento/public_html/app/code/core/Mage/Core/Model/Abstract.php(226): Mage_Persistent_Model_Session->_afterLoad()
#3 /home/magento/public_html/app/code/core/Mage/Persistent/Model/Session.php(154): Mage_Core_Model_Abstract->load('deleted', 'key')
#4 /home/magento/public_html/app/code/core/Mage/Persistent/Helper/Session.php(67): Mage_Persistent_Model_Session->loadByCookieKey()
#5 /home/magento/public_html/app/code/core/Mage/Persistent/Helper/Session.php(91): Mage_Persistent_Helper_Session->getSession()
#6 /home/magento/public_html/app/code/core/Mage/Persistent/Model/Observer.php(177): Mage_Persistent_Helper_Session->isPersistent()
#7 /home/magento/public_html/app/code/core/Mage/Core/Model/App.php(1358): Mage_Persistent_Model_Observer->emulateQuote(Object(Varien_Event_Observer))
#8 /home/magento/public_html/app/code/core/Mage/Core/Model/App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Mage_Persistent_Model_Observer), 'emulateQuote', Object(Varien_Event_Observer))
#9 /home/magento/public_html/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('controller_acti...', Array)
#10 /home/magento/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(527): Mage::dispatchEvent('controller_acti...', Array)
#11 /home/magento/public_html/app/code/core/Mage/Core/Controller/Front/Action.php(69): Mage_Core_Controller_Varien_Action->preDispatch()
#12 /home/magento/public_html/app/code/core/Mage/Checkout/controllers/OnepageController.php(59): Mage_Core_Controller_Front_Action->preDispatch()
#13 /home/magento/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(407): Mage_Checkout_OnepageController->preDispatch()
#14 /home/magento/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#15 /home/magento/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#16 /home/magento/public_html/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#17 /home/magento/public_html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#18 /home/magento/public_html/index.php(83): Mage::run('be', 'website')
#19 {main}
Using Zend_JSON for parse invalid JSON format like “pain in the neck”. If you got this kind of error while parse JSON :
1 |
Uncaught exception ‘Zend_Json_Exception’ with message ‘Decoding failed: Syntax error |
Or maybe :
1 |
Uncaught exception ‘Zend_Json_Exception’ with message ‘Decoding failed: Unexpected control character found |
I suggest to you, move into json_decode() from native PHP. Here are snippets to handle JSON invalid format exception.
1 |
function json_downloader($url) {
|
How to use?
Just insert your URL which contain JSON into this function.
1 |
$url = ‘put your JSON url here’; |
Problem solved! 😀