This topic and discussion board has been closed.
To stay updated on announcements related to the API please review this post about the new Developer changelog.
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
So does that mean the REST api for metafield now supports 'json_string' as well?
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!
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
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
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.
^ 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
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);
});
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
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!!
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"
}
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023