Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: JSON parsing error: Invalid token

Solved

How to fix JSON parsing error on Collections pages?

bill_wishgarden
Tourist
8 0 1
Hi community!
I noticed an error in Google Search Console that started occurring August 16 where there is a JSON parsing error.
 
The error is:JSON parsing error: Invalid token ] } . 
 
The code snippet that caused the error: 
 

 

},

 

The error is only appearing on my Collections pages. Here is the JSON snippet from Shopify:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ItemList",
  "itemListElement": [
      {%- for product in collection.products -%}
{% if product.type=='giftbox_ghost_product' %}{%continue%}{%endif%}
        {
          "@type": "ListItem",
          "position": {{ forloop.index | json }},
          "url": {{ product.url | json }},
          "name": {{ product.title | json }}
        }
        {% unless forloop.last %},{% endunless %}
      {%- endfor -%}
  ]
}
</script>

An example page that has this error is: https://www.wishgardenherbs.com/collections/respiratory-wellness

 

I cannot figure out the error. Does anyone have a guess?

 

Thank you,

Bill

 

Accepted Solution (1)

Made4uo-Ribe
Shopify Partner
9037 2160 2664

This is an accepted solution.

Hi @bill_wishgarden 

 

Based on the code provided, you have an if statement inside the forloop. You add comma when its the last forloop, but since you have if statement which takes a filter and only continue if it meets the certain condition. So, it never meets the forloop.last. 

 

Try the edited code below instead: 

 

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ItemList",
  "itemListElement": [
      {%- for product in collection.products -%}
{% if product.type=='giftbox_ghost_product' %}
        {
          "@type": "ListItem",
          "position": {{ forloop.index | json }},
          "url": {{ product.url | json }},
          "name": {{ product.title | json }}
        }
        {% unless forloop.last %},{% endunless %}
{% endif %}
      {%- endfor -%}
  ]
}
</script>

 

Please don't forget to Like and Mark Solution to the post that helped you. Thanks!

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
Create custom Shopify pages effortlessly with PageFly's drag-and-drop ⚙️.

View solution in original post

Replies 2 (2)

Made4uo-Ribe
Shopify Partner
9037 2160 2664

This is an accepted solution.

Hi @bill_wishgarden 

 

Based on the code provided, you have an if statement inside the forloop. You add comma when its the last forloop, but since you have if statement which takes a filter and only continue if it meets the certain condition. So, it never meets the forloop.last. 

 

Try the edited code below instead: 

 

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ItemList",
  "itemListElement": [
      {%- for product in collection.products -%}
{% if product.type=='giftbox_ghost_product' %}
        {
          "@type": "ListItem",
          "position": {{ forloop.index | json }},
          "url": {{ product.url | json }},
          "name": {{ product.title | json }}
        }
        {% unless forloop.last %},{% endunless %}
{% endif %}
      {%- endfor -%}
  ]
}
</script>

 

Please don't forget to Like and Mark Solution to the post that helped you. Thanks!

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
Create custom Shopify pages effortlessly with PageFly's drag-and-drop ⚙️.
bill_wishgarden
Tourist
8 0 1

Thank you so much! I replaced the code with your code and submitted the fix to Google and it appears to have worked! Thanks again! I love this Community!