magento2: Uncaught SyntaxError: Unexpected token , in JSON

Preconditions

  1. Upgrade from 2.2.3 > 2.2.4
  2. PHP 7

Steps to reproduce

  1. Go to any product page that has an image.
  2. You will see a spinning gif but the image never loads.
  3. Console error: Uncaught SyntaxError: Unexpected token , in JSON

Expected result

  1. Image gallery loads.

Actual result

  1. Just see the spinning gif. This worked fine prior to upgrade.

This is the error: Uncaught SyntaxError: Unexpected token , in JSON at position 1025, from a JSON array. Attached an image. Here is the full data array "data": [{"thumb":"https:\/\/v3.asdf.com\/pub\/media\/catalog\/product\/cache\/460a3bcbdb4cc235aac43a6f81f8f135\/s\/j\/sjm2016.jpg","img":"https:\/\/v3.asdf.com\/pub\/media\/catalog\/product\/cache\/c687aa7517cf01e65c009f6943c2b1e9\/s\/j\/sjm2016.jpg","full":"https:\/\/v3.asdf.com\/pub\/media\/catalog\/product\/cache\/926507dc7f93631a094422215b778fe0\/s\/j\/sjm2016.jpg","caption":"SJM 2016 Camaro Line Lock","position":"1","isMain":true,"type":"image","videoUrl":null}], capture

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 50 (13 by maintainers)

Most upvoted comments

problem is the JSON object is broken and cannot be parse. take a look at the line

                    "showCaption": ,

to fix this, in your custom theme file app/design/frontend/{Vendor}/{theme}/Magento_Catalog/templates/product/view/gallery.phtml find the line

                    "allowfullscreen": <?php /* @escapeNotVerified */ echo $block->getVar("gallery/allowfullscreen"); ?>,
                    "showCaption": <?php /* @escapeNotVerified */ echo $block->getVar("gallery/caption"); ?>,
                    "width": <?php /* @escapeNotVerified */ echo $image_width; ?>,

change to

                    <?php if (($block->getVar("gallery/allowfullscreen"))): ?>
                        "allowfullscreen": <?= /* @escapeNotVerified */ $block->getVar("gallery/allowfullscreen") ?>,
                    <?php endif; ?>
                    <?php if (($block->getVar("gallery/caption"))): ?>
                        "showCaption": <?= /* @escapeNotVerified */ $block->getVar("gallery/caption") ?>,
                    <?php endif; ?>
                    "width": <?php /* @escapeNotVerified */ echo $image_width; ?>,

you may need to check other property and surround it with “if” condition take a look at magento template file vendor/magento/module-catalog/view/frontend/templates/product/view/gallery.phtml for reference

Found it. Any of my products that have the quote symbol (") in the title are not loading the image. Apparently they are not being formatted into special characters and are causing the image not to load regardless of the theme.

OK - I found out that it’s my theme. I added an underscore to the filename of app/design/frontend/mytheme/Magento_Catalog/templates/product/viewgallery.phtml in hopes that it would skip using my theme’s version. I refreshed and the images now show. I guess I have to compare the native gallery.phtml to my theme’s gallery.phtml and find out what changed.

Hi,

I have the same issue with theme “Moza”. The solution to remove the file app/design/frontend/mytheme/Magento_Catalog/templates/product/gallery.phtml works for me too.

Thanks for the help.

Hi folks

There are two issues being discussed in here.

The first one from @heyepic, that one has a fix over here: https://github.com/magento/magento2/pull/16594/files

Fix is scheduled for Magento 2.2.7 (or 2.2.6, it’s a bit unclear for me)

The other one from @erichagz & @KSaho, has a fix over here: https://github.com/magento/magento2/pull/15521/files

This fix is scheduled for Magento 2.2.6 (was targeted for 2.2.5 but then they missed it somehow)

Hope this is enough info so people can try to resolve their current issues. 😃