New 'json_string' value type for Metafield object

Ryan
Shopify Staff
Shopify Staff
499 42 120

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
660 0 144

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
4833 60 580

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
Shopify Staff
499 42 120

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
11099 217 2256

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 in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
Ryan
Shopify Staff
Shopify Staff
499 42 120

^ 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
Shopify Staff
499 42 120

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
55 1 28

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!!

RustyDev
Shopify Partner
12 0 4

This is great news!

Unfortunately, it doesn't seem to work with the bulk editor:

/admin/bulk?resource_name=Page&edit=metafields.json_test.json_key:json_string

This saves and a string instead of a json_string:

{
"id": 4090352107572,
"namespace": "json_test",
"key": "json_key",
"value": "[\"one\",\"two\",\"three\"]",
"value_type": "string",
"description": null,
"owner_id": 7266369588,
"created_at": "2018-10-27T08:27:08-07:00",
"updated_at": "2018-10-27T08:27:08-07:00",
"owner_resource": "page"
}