New 'json_string' value type for Metafield object

Ryan
Shopify Staff
492 42 113

The Metafield object has been updated with a new 'json_string' value type. This makes it easy to store arbitrary structured data within the Shopify platform and access it inside Liquid templates. Notably, when accessing the value of 'json_string' value type metafields, the data is deserialized to an iterable hash or array.

 

See more examples: https://help.shopify.com/en/themes/liquid/objects/metafield

 

Happy Coding!

Ryan | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

Replies 10 (10)
ClementG
Shopify Partner
659 0 136

So does that mean the REST api for metafield now supports 'json_string' as well?

 

https://help.shopify.com/en/api/reference/metafield

HunkyBill
Shopify Expert
4611 54 531

Since JSON is just a string, and Metafields already supported Strings, at first this was just a MEH announcement, but the idea that the Liquid render would deserialize the data is actually great and useful!

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Ryan
Shopify Staff
492 42 113

So does that mean the REST api for metafield now supports 'json_string' as well?

I believe it does

Ryan | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

Timothy3
Shopify Partner
4 0 0

Is the metafield value field still limited to 64kb?  We put large json data (>64kb) in multiple metafields because of this limit.  Could we use json_string for json segments (invalid json)?

Also, does not appear the api docs have been updated yet. https://help.shopify.com/en/api/reference/metafield

Jason
Shopify Expert
10438 169 2081

The limit hasn't changed. If you really need to store more than that I'd have to question if you're storing the data in the right place. Would be interested in knowing your use case - but that's better for a new thread.

You won't be able to segment your JSON across multiple metafields.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.
Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.
★ http://freakdesign.com.au ★
Ryan
Shopify Staff
492 42 113

^ Limit hasn't changed.

 

Doc update is in progress.

Ryan | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

David_Lopez
New Member
2 0 0

What's an example of a valid json_string value? I've tried valid inputs of JSON.parse and outputs of JSON.stringify and I get an error 419: unexpected token.

I'm trying to do a PUT to update a metafield to a json_string value type with the API. I've also tried on the developers desktop app with the same results.

 

const fetchOptionsUpdate = {
      method: "PUT",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      credentials: "include",
      body: {
        metafield: {
          value: '{"chicken":"2oz","steak":"4oz"}',
          value_type: "json_string"
        }
      }
    };

fetch(
      "/shopify/api/products/{product_id}/metafields/{metafield_id}.json",
      fetchOptionsUpdate
    )
      .then(response => response.json())
      .then(object => {
        console.log(object);
      });

 

Ryan
Shopify Staff
492 42 113

Hi David,

You'd need escape characters in your example, here's a working example:

 

{
  "metafield": {
    "namespace": "parazoology",
    "key": "supernatural_powers",
    "value_type": "json_string",
        "value": "{\"valid\": \"json\"}"
  }
}

 

Ryan | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

timd_mackey
Shopify Partner
50 1 23

Wow, this is a game changer! To my knowledge, this is the first time we've had the ability to create real data hashes that can be manipulated in liquid. No more using the split filter to create pseudo-hashes (at least with metafields)!

This isn't made clear in the docs, but you can reference properties within a json_string metafield just as if they were native liquid objects. For example, given the following product metafield data for a metafield pricing.preferred:

// namespace: pricing
// name: preferred
{
  "price_each": 50,
  "price_per_unit": 2500,
  "price_per_case": 10000
}

you can reference each value like so:

{{ product.metafields.pricing.preferred.price_each }}
{{ product.metafields.pricing.preferred.price_per_unit }}
{{ product.metafields.pricing.preferred.price_per_case }}

You can even assign the metafield to a variable, and the variable will remain iterable as a liquid object:

{% assign m_preferred = product.metafields.pricing.preferred %}
{{ m_preferred.price_each }}
{{ m_preferred.price_per_unit }}
{{ m_preferred.price_per_case }}

 Fantastic!!