Looping through a metafield list of metaobjects that contain single line fields

Topic summary

A developer is struggling to dynamically loop through metaobject fields in Shopify’s Liquid templating language. They’ve created a specifications_table metaobject containing fields like width, length, height, and depth, referenced as a list metafield to generate product specification tables for variants.

Current limitation: The code only works by manually specifying each field name (e.g., specificationObject.width), preventing easy expansion when new fields are added.

Desired solution: Dynamically iterate through all fields within each metaobject without hardcoding field names, similar to PHP’s foreach($key => $value) pattern.

Attempted approaches that failed:

  • {% for specificationObjectValue in specificationObject %}
  • {% for specificationObjectValue in specificationObject.value %}

The developer notes that while the metafield contains 3 items (shown as GID references), only one iteration occurs. The post includes reversed/garbled code snippets showing their current manual approach and desired dynamic implementation. The question remains unresolved with no working solution for accessing metaobject field names and values dynamically in Liquid.

Summarized with AI on November 4. AI used: claude-sonnet-4-5-20250929.

Hi,

I’m having issues looping through a metaobject which is further defined by a list metafield.

I’ve created a metaobject called specifications_table which contains an array of single line fields such as:

  • specifications_table.width
  • specifications_table.length
  • specifications_table.height
  • specifications_table.depth

I’ve then referenced this metaobject as a list metafield definition specifications.table

As you may have guessed, it’s being used to generate a product specification table. Some products have variants that require multiple tables (hence the metafield is using list).

Data looks like this

Table A
    specifications_table.width - 1000mm
    specifications_table.length- 1100mm
    specifications_table.height- 1200mm
    specifications_table.depth- 1300mm
Table B
    specifications_table.width - 1400mm
    specifications_table.length- 1500mm
    specifications_table.height- 1600mm
    specifications_table.depth- 1700mm

The only way I can output the values so far is manually call each named index, but this doesn’t allow for future expansion such as adding weight without manually altering the table.

As it is currently:

{% if product.metafields.specifications.table != blank %}
    {% for specificationObject in product.metafields.specifications.table.value %}
    <table>
        {% if specificationObject.width!= blank %}
        <tr>
        <td>Width:</td>
        <td>{{ specificationObject.width }}</td>
        </tr>
        {% endif %}
        {% if specificationObject.length!= blank %}
        <tr>
        <td>Length:</td>
        <td>{{ specificationObject.length}}</td>
        </tr>
        {% endif %}
        ....manually repeating/hardcoding the above for all fields
    </table>
    {% endfor %}
{% endif %}

How I’d like it to be:

{% if product.metafields.specifications.table != blank %}
    {% for specificationObject in product.metafields.specifications.table.value %}
    <table>
        {% for specificationObjectValue in specificationObject.value %}
        <tr>
        <td>{{specificationObjectValue.index_name}}:</td><-- is it possible to print the index name?
        <td>{{ specificationObjectValue.value}}</td>
        </tr>
        {% endfor %}
    </table>
    {% endfor %}
{% endif %}

Is there a way to do the above or is it a lost cause? When I try

{% for specificationObjectValue in specificationObject.value %}

It results in nothing

When I try:

{% for specificationObjectValue in specificationObject %}

I get just one iteration of below (even though there are 3 items:

* table: ["gid://shopify/Metaobject/202727326061","gid://shopify/Metaobject/202727457133","gid://shopify/Metaobject/202727522669"]

Natively I’m a PHP developer and this would be achieved with:

<?php 
for($specificationObject as $specificationObjectValue){
?>    <table>
    <?php 
    for($specificationObjectValue as $key => $value){
    ?>        <tr>
        <td><?=$key ;?>:</td><-- is it possible to print the index name?
        <td><?=$value;?></td>
        </tr>
        <?php 
        } 
        ?>    </table>
<?php 
} 
?>