Using a liquid linklist object in javascript (script tag)

When using liquid variables in JavaScript, one is to add a | json filter at the end of the variable:

var myVar = {{yourLiquidVar | json}}

However, when I try to do this with a linkist object as such:

var linklist_variable = {{ linklists['footer'] | json }};
console.log(linklist_variable)

I get the following when logging to console: error: “json not allowed for this object”

I tried linklists[‘footer’].links, I tried looping the linklist object, but the error persists

How can I access linklist objects in javascript (script tags)?

P.S, I am writing the code in a custom section file

Hi @Yousif_A ,

Currently, Shopify doesn’t support this, you can use JSON for template objects like cart, collection, product,…

If you want, you need to add for and assign an array variable to it.

let linklist_variable = [];
          {% for link in linklists['footer'].links %}
            linklist_variable[{{ forloop.index0 }}] = '{{ link.handle }}';
          {% endfor %}

Hope it helps!

1 Like