How can I correctly extract metafields values in JSON code?

Topic summary

Main issue: extracting specific product metafield values and passing them into an OpenAI prompt within a Shopify Plus workflow.

Key points and guidance:

  • Shopify’s products query cannot search/filter by metafields yet; only a query string is supported for product search.
  • In a workflow, once a product is in context, metafields can be accessed directly in Liquid within the OpenAI action without a separate API query.
  • To get a single metafield value by namespace/key in Liquid:
    • Assign and output: {% assign mf_object = product.metafields | where: "namespace", "my_fields" | where: "key", "pattern" | first %} {{ mf_object.value }}
  • If you need all possible choices, that refers to the metafield definition (validations), not the current value. Accessing definitions/validations may require GraphiQL/GraphQL and could be costly due to API limits.

Context and intent:

  • The goal is to pass selected metafields (e.g., style1, year, season1, special, waist, closure, pattern) as variables into the AI prompt to generate product descriptions.

Status:

  • A concrete Liquid solution for retrieving specific metafield values was provided.
  • Questions about extracting all validation choices remain open; screenshots illustrate the metafield UI but are not essential to the Liquid approach.
Summarized with AI on January 23. AI used: gpt-5.

How to update this part of the code to extract metafields values correctly? Or what to paste to the edit query instead?

{
“query”: {
“objectType”: “product”,
“id”: “product.id”,
“metafields”: [
{
“namespace”: “my_fields”,
“key”: “style1”
},
{
“namespace”: “my_fields”,
“key”: “year”
},
{
“namespace”: “my_fields”,
“key”: “season1”
},
{
“namespace”: “my_fields”,
“key”: “special”
},
{
“namespace”: “my_fields”,
“key”: “waist”
},
{
“namespace”: “my_fields”,
“key”: “closure”
}
]
}
}

Look at the example queries and the search syntax. You don’t need to build the whole query … just the query string

To add… Shopify’s product query does not allow searching by metafields yet. https://shopify.dev/docs/api/admin-graphql/2023-07/queries/products#argument-products-query

What are you trying to do in your workflow?

Pass data from metafields to Open AI for text generation.

Yes, but hot to take a date from Metafields? I don’t need all of them.

You might not need a query for this. When should the workflow run? Once you have a product in the workflow, you can access all of it’s metafields in liquid directly in the OpenAI action

See https://community.shopify.com/post/1826703 for some code

I know, but how to filter them out? Because they look like this:

I’m not sure where / how you want to filter them out. If you mean access a single value in liquid, my preferred approach is like this:

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%}

If you mean in a condition, here’s example example for order metafields:

Things are more complex in my case. I am already checking if the required values are in the product:

Then I need to pass exact metafields variables to the Ai prompt here, not just one specific value, to generate a product description based on different parameters Metafield1, Metafield2, Metafield3, and so on:

Each metafield can have many variations, not just one:

I’m not sure what you mean by “variables”. I think, b/c of that screenshot, you want all of the possible values for the metafield in that Open AI prompt? That isn’t a metafield value, but I think a metafield definition. I think the choices are stored in validations. You might need to use GraphiQL or a GraphQL client to find the right key name to use for that. Also, you may run into an API limit as query metafields this way is very costly.

Variables are metafields: like the one I show on the print screen, how do I add “product.metafields.my_fields.pattern” to the place Metafield1, as I show on the print screen?

What exact piece of code do I have to add to that place?

This gets the metafield by namespace/key and outputs the value. “first” is needed because “where” returns a list.

{%- assign mf_object = product.metafields | where: "namespace", "my_fields" | where: "key", "pattern" | first -%} 
{{ mf_object.value }}