New 'json_string' value type for Metafield object

Shopify Staff
Shopify Staff
469 36 92

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!

Developer Experience @ Shopify
5 Likes
Shopify Partner
660 0 104

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

 

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

0 Likes
Shopify Expert
3937 16 325

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
1 Like
Shopify Staff
Shopify Staff
469 36 92

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

I believe it does

Developer Experience @ Shopify
0 Likes
Shopify Partner
2 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

0 Likes
Shopify Expert
9778 86 1524

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.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Staff
Shopify Staff
469 36 92

^ Limit hasn't changed.

 

Doc update is in progress.

Developer Experience @ Shopify
0 Likes
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);
      });

 

0 Likes
Shopify Staff
Shopify Staff
469 36 92

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\"}"
  }
}

 

Developer Experience @ Shopify
1 Like
Shopify Partner
27 0 9

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

0 Likes