Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hello,
I need to figure out one issue with the metafield which has value type = 'json'.
instead of [] in the metafield, how can I set it completely blank.
like:
From API call I can not sent nil value for metafield.value if child_products_data is empty, because it gives error like #<ShopifyAPI::Errors::HttpResponseError: {\"errors\":{\"value\":[\"can't be blank.\"]}.
Have any solution for this??
Solved! Go to the solution
This is an accepted solution.
Hey @Av18
Thanks for the response. I just tried with a JSON field and saw the same results.
E.g. Here's a json field
Which the API returns as:
But setting it to blank in admin (or running metafieldDelete / REST `delete`) clears the value:
I.e. try .delete instead of .save of a nil value.
Let me know if I'm not following 🙂
Scott | Developer Advocate @ Shopify
Hey @Av18
I looked into how the native admin handles this, and it appears metafields are simply deleted when their value is removed.
E.g. With bundle value:
With blank bundle value:
Scott | Developer Advocate @ Shopify
Hey @SBD_
You are correct. Metafields are simply deleted when their value is removed. However, this only works for metafields that do not have a JSON type. For JSON type metafields, updating a nil value is prevented.
For example
when I update metafield.value
This is an accepted solution.
Hey @Av18
Thanks for the response. I just tried with a JSON field and saw the same results.
E.g. Here's a json field
Which the API returns as:
But setting it to blank in admin (or running metafieldDelete / REST `delete`) clears the value:
I.e. try .delete instead of .save of a nil value.
Let me know if I'm not following 🙂
Scott | Developer Advocate @ Shopify
Hey @SBD_
"Thank you for providing the correct solution."
It successfully clears the values when I delete the metafield.