Metafield displaying with square brackets [] and double quotes on product grid (card-product.liquid)

Dawn Theme 7.0

I created a product metafield to display on the default collection product grid, underneath each product. The metafield is product.metafields.custom.fragrance. It is a single line, list of values. The metafield displays where it should on the product grid, but all values for the metafield are surrounded by square brackets and double quotes “”.

Like this, exactly as typed: [“Cypress Saffron Wood”]

In above example the words Cypress Saffron Wood is a single value within the metafield list of values - the list of values are entered without brackets or quotes.

I added the metafield to card-product.liquid by doing this, at line 159 (just below the show_vendor endif)

  <span class="caption-with-letter-spacing light">{{ card_product.metafields.custom.fragrance }}</span>

I also display this metafield on the product page as well, and it displays fine, without the brackets and quotes.

Can anyone suggest how I remove the brackets and quotes so they are not displayed on the product grid?

Thanks everyone!

Assuming this is because you are trying to output an array, if you know for sure you just want the first value, why not use [0] like so:

  <span class="caption-with-letter-spacing light">{{ card_product.metafields.custom.fragrance[0] }}</span>

Hi - thank you for the reply. For clarity, the output is correct - the metafield value under each unique product is correct

Such as:

product 1: metafield value = [“Cypress Saffron Wood”]

product 2: metafield value = [“Book Green Hard”]

Values are all good, just want to get rid of the square brackets and double quotes

Yeah did you see the edit I suggested?

Instead of outputting the whole array like this:

{{ card_product.metafields.custom.fragrance }}

Output only the first item and therefore just the value and not the array

{{ card_product.metafields.custom.fragrance[0] }} 

Another way of writing it is this:

card_product.metafields.custom.fragrance.first

Thank you. I tried both your suggestions, the metafield will not show any output with either option. To test a little more along the array theory, I changed the metafield list of values to be just one word, not a string of individual words. Unfortunately, same output such as:

Product 1 card_product.metafields.custom.fragrance value = [“blue”]

Product 2 card_product.metafields.custom.fragrance value = [“green”]

The values are correct with the right metafield value showing up under the right product. Just need to get rid of the brackets and quotes.

Joeybab3, I got it - thanks to your input. Your comments about array made me wonder why your suggestions did not work, so explored some more. I had created the metafield using the editor. As I was needing a list of values, I chose “List of Values” for the data definition. That was the mistake - I should have chosen “One Value” and just entered the values I required using ‘add item’. Everything works fine now - no brackets or quotations!

{{ card_product.metafields.custom.fragrance | metafield_text }}

This would also fix that problem for anyone else looking