How can I extract values from a custom JSON Metafield using Flow?

How can I extract values from a custom JSON Metafield using Flow?

carolin1
Shopify Partner
2 0 0

I've tried to update an order metafield on order created. The information I want to use for the update is stored in a custom json metafield. It contains:

{
    default: {
        shipping_days_min: 3,
        shipping_days_max: 5,
    },
    nl: {
        shipping_days_min: 6,
        shipping_days_max: 12,
    }
}

I get access within the flow to the metafield with this code:

{%- for mf in shop.metafields -%}
{%- if mf.namespace == "countries" and mf.key == "shipping_days" -%}
	{%- assign shipping_days = mf.value -%}
        {{ shipping_days }}
{%- endif -%}
{%- endfor -%}

mf.value / shipping_days contains:

{"default":{"shipping_days_min":3,"shipping_days_max":5},"nl":{"shipping_days_min":1,"shipping_days_max":3}}


I've tried to pull out the value for "nl":

{{ shipping_days["nl"] }}

But then I've got an error.
"shop.metafields.value.nl" is invalid. Replace this variable.

I also tried to loop over shipping_days, but it also failed.

 

Does anyone have any ideas? 

Kind regards, Carolin

Reply 1 (1)

paul_n
Shopify Staff
1300 148 297

All metafields are currently treated as strings because that's how they are passed to Flow through the API. In addition, you cannot access elements in an object (even if it was an object) using that syntax in Flow:

{{ shipping_days["nl"] }}

We are currently looking at ways to make using custom data (metafields and metaobjects) way better and more intuitive in Flow. Ideally here, you wouldn't need to loop over metafields (very large) and that JSON object would be accessible in Flow in some way like what you are trying to do there. 

 

Until then, you'll need to assume that value is a string. In this case, it's going to be really difficult to work with and I'd probably find a different approach until this is ready. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.