Tajs
Tajs

Reputation: 621

Cannot access JSON array stored in native Shopify metafields

I'm trying to access metafields containing an array that I want to loop over.

I've tried solutions online to no avail. Do you have any suggestions?

This is the JSON.

[
  {
    "id": 1,
    "title": "Why do pets love CBD so much?",
    "pub": "ScienceDirect",
    "url": "https://www.efsa.europa.eu/en/news/cannabidiol-novel-food-evaluations-hold-pending-new-data"
  },
  {
    "id": 2,
    "title": "When will CBD take over the world",
    "pub": "Anna Research Institute",
    "url": "https://www.efsa.europa.eu/en/news/cannabidiol-novel-food-evaluations-hold-pending-new-data"
  }
]

The metafield is article.metafields.custom.source_snippet

In my article template I'm trying to access it via {% assign sources = article.metafields.custom.source_snippet %}

This usually worked with the old "Metafields Guru".

Thanks.

Upvotes: 2

Views: 659

Answers (1)

Fabio Filippi
Fabio Filippi

Reputation: 1965

In shopify there are actually two types of JSON metafields: json and json_string.

json_string is the old and deprecated type and json is the new version.

If your metafield was a json_string your code would have worked, but with json to access the actual value you need to add .value so your code should look like this

{% assign sources = article.metafields.custom.source_snippet.value %}

Upvotes: 3

Related Questions