Access App Metafield JSON by key or index

Hi everyone.

I have strange issue with rendering app metafields with type of json in app embed with liquid.

Here is the issue.

value: {{ app.metafields.shipready.app_settings.value }}
Type: {{ app.metafields.shipready.app_settings.type }}

It’s printing the following data

value: {"app_name":"Example App","app_version":"Beta"} 
Type: json

When I want to access the object by key, it’s shows nothing.

{% assign app_data = app.metafields.shipready.app_settings.value %}
name: {{ app_data['app_name'] }}

I tried this as well but no luck.
name: {{ app_data[0] }}

I also tried this but not working
{{ app.metafields.shipready.app_settings.value['app_name'] }}

What am I doing wrong? Is it even possible to access json metafield with key?

Thank you!

1 Like

Hello! Here are the instructions for accessing metafields of type JSON. What you are doing is correct, however, I’d look into why printing the value of your metafield uses : access instead of => access like my example…

This seems more like a Liquid issue, is it only reproducible within your theme app extension?

3 Likes

Hey @SelenaL-Shopify!

Thanks for the replay. Yes this issue is only in App embed. Shopify Product Metafields with json type seem to work fine.

Thank you!

1 Like

Interesting! Can you share the shop and theme you’re using so I can investigate further? If you’re not comfortable sharing publicly, you’re can go through Shopify support :slight_smile:

Hi Selena!

Honestly, I tested this on multiple development stores with different apps and still same issue. I have some students reported this as well.

I can share the development store details but it’s happening on all stores for app metafields.

Here is the store url: shipready-dev.myshopify.com
Here is theme id: 126379622475

Thank you!

1 Like

Hi Hujjat,

I’m having trouble following full context of this issue.

Do you develop these apps and are attempting to surface app_data for apps you develop inside your apps Theme Extension?

If these are not your own, and you’re trying to add app_data from 3rd party apps on your shop to your Storefront/Liquid Theme or another app, I don’t think this is possible. The Metafields would be owned by and only be accessible to the app that created them.

Let me know if I’m understanding the issue correctly or not.

Docs Use app-data metafields

An app-data metafield is a metafield that is tied to a particular app installation and can only be accessed by that app

Hey @MLDimitry !

Thanks for looking into this.

Yes, the metafields are created using my app and I’m trying to access it in app embed block that belongs to same app that created those metafields.

I’m trying to access the metafields in liquid app embed block but the json is not rendering properly.

Thank you!

{
  "metafieldsSetInput": [
    {
      "namespace": "secret_keys",
      "key": "api_key",
      "type": "json",
      "value": "{\"app_name\":\"Example App\",\"app_version\":\"Beta\"}",
      "ownerId": "gid://shopify/AppInstallation/485700010044"
    }
  ]
}
{% assign response = app.metafields.secret_keys.api_key.value %}

version: {{response.app_version}}
name: {{response.app_name}}

I can access the app metafield JSON value in the Theme App Extension.