Filter to remove square brackets and join arrays

Shopify Partner
120 2 49



I'm trying to join the values within each products metafield into a new variable and then add this to the order metafield. I think the values from each products metafields are in arrays that need to be combined in order to then pass the combined values into the correpsponding metafield on the order.




  • product A has department metafields of "Menswear" and "Womenswear"
  • product B has department metafields of "Books & Media"  and "Homeware"

I combine these into a variable which I think try to put into the order metafield using this code: 


{% capture metaDepartment %}
{% for lineItems_item in order.lineItems %}
{% for metafields_item in lineItems_item.product.metafields %}
{% if metafields_item.namespace == "product" and metafields_item.key == "department" %}
{{ metafields_item.value | remove: "[" | remove: "]" | remove: '"' | split: ", " }}
{% endif %}
{% endfor %}
{% endfor %}
{% endcapture%}

{{ metaDepartment }}


I get this error though. 





What I'm wondering is whether I can join it all together in a way that will be accepted into the new order metafield, and for this to work no matter how many products I have in the order. 


My thinking is that I need to retain it in ["value", "value2", "value3"] format but lose the square brackets except the very first and very last and then join arrays using, to get it into the above format. 


Having trouble getting there though. Anyone know? Am I barking up the wrong tree here? 


Replies 2 (2)

Shopify Partner
6 0 1

Hello! Not sure your question is still relevant. By the way, did you try to use a filter on your metafield value?  Like instead of removing all of this things, just use:

{{ metafields_item.value | metafield_text}}

 Let me know please works it in your case or not

Love helping people to get the result.
Front-end developer with over 4 years of experience.
Shopify Partner
120 2 49

Hi @moek,

That's a good suggestion. unfortunately it is one of those filters that isn't available in Flow. I did eventually get to a solution on this though.