How to split namespace and key in a product metafield output loop?

{% for metafield in product.metafields.namespace %}
	- {{ metafield }} 

{% endfor %}

outputs

- namespacekey

which is great, but how do I split namespace and key?

The direct access is great and all but it gets pretty cumbersome:

{% if product.metafields.knife.blade_engraving.value != 'blank' %}
	- Blade engraving: {{ product.metafields.knife.blade_engraving.value | metafield_tag }}
	

{% endif %}

repeat for all metafields (in my case 20).

It’d be more elegant if we could loop through this.

Is there a way to grab the Name and Description from the Shopify backend?

Hi @SpotterJ ,

You can’t loop over a namespace to get each individual metafield. Refer https://shopify.dev/api/liquid/objects/metafield#direct-access

So you can’t “for” it, you can just get out each metafield.

Hope it helps!

That’s not strictly true. With my example above I do get a list of all the metafields namespaces and their values, however there’s no space between the two.

I ended up with direct access, couldn’t find a way to split the two.

Hi @SpotterJ ,

Yes, I totally know this, you will be returned a text including the name and value.

But currently, there is no way to separate it. You can refer to shopify’s instructions.

If it helped you solve your issue, please mark it as a solution. Thank you and good luck.

Just checked it and your description is true… It’s counterintuitive. The mechanism they provide is .first and .last properties I works on the json metafield type and others as well:
ex>

doc: https://shopify.dev/api/liquid/objects#metafield-accessing-metafields-of-type-json
handy app for metafields: https://apps.shopify.com/metafields-editor-2

{% liquid
assign ns = shop.metafields.zzz
for k_v in ns
assign k = k_v.first
assign v = k_v.last

echo ‘-------’
echo v.value.a
echo ‘-------

endfor
%}
noting the unconventional v.value.yourprop syntax and the .last prop to get the to value in the first place. That is two extra steps but it does work and the looping works as well
so: json type:

yields the values:

-------april-------
-------bat-------
-------cat-------

Hi @MinderSolutions

You saved my day dude it is awsome Thanks For help man.

KUDOS :+1: :blush: