d
Topic
Ryan O Shopify Employee
Posts:
233
Last edited 3 months ago
g
4
upvotes

New 'json_string' value type for Metafield object

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!

i
Replies
Posts:
618
3 months ago

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

 

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

Posts:
3851
3 months ago
g
1
upvotes

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 O Shopify Employee
Posts:
233
3 months ago

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

I believe it does

Timothy Member
Posts:
2
3 months ago

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 freakdesign.com.au
Posts:
9236
Last edited 3 months ago

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
Ryan O Shopify Employee
Posts:
233
3 months ago

^ Limit hasn't changed.

 

Doc update is in progress.

Posts:
2
2 months ago

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 O Shopify Employee
Posts:
233
2 months ago
g
1
upvotes

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

 

timd.mackey Shopify Partner
Posts:
23
Last edited about 2 months ago

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
Posts:
12
Last edited about 1 month ago

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