Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Problem with Metafield value type JSON

Solved

Problem with Metafield value type JSON

Av18
Shopify Partner
14 1 0

Hello, 

I need to figure out one issue with the metafield which has value type =  'json'.

 

metafield = ShopifyAPI::Metafield.new
metafield.variant_id = custom_bundle.master_variant_id
metafield.namespace = 'custom_bundle'
metafield.key = 'product_data'
metafield.value = child_products_data.to_json
metafield.type = 'json'
metafield.save!
 
When child_products_data is empty then metafield.value set the value as [].
 
Av18_0-1708943807715.png

 

instead of [] in the metafield, how can I set it completely blank.

like:  

Av18_1-1708943901095.png

 

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

Accepted Solution (1)
SBD_
Shopify Staff
1831 273 421

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

SBD__0-1709098754789.png

Which the API returns as:

SBD__1-1709098802110.png


But setting it to blank in admin (or running metafieldDelete / REST `delete`) clears the value:

SBD__2-1709098855232.png

SBD__3-1709098866232.png

 

I.e. try .delete instead of .save of a nil value.

 

Let me know if I'm not following 🙂

 

Scott | Developer Advocate @ Shopify 

View solution in original post

Replies 4 (4)

SBD_
Shopify Staff
1831 273 421

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:

SBD__1-1708995259467.png

SBD__2-1708995306807.png

 

With blank bundle value:

SBD__3-1708995327605.png

SBD__0-1708995229485.png

 

 

Scott | Developer Advocate @ Shopify 

Av18
Shopify Partner
14 1 0

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 

metafield.value = child_products_data.to_json
metafield.type = 'json'
metafield.save!
 
to this metafield.value = nil
then it prevent like {\"value\":[\"can't be blank.\"]}.
SBD_
Shopify Staff
1831 273 421

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

SBD__0-1709098754789.png

Which the API returns as:

SBD__1-1709098802110.png


But setting it to blank in admin (or running metafieldDelete / REST `delete`) clears the value:

SBD__2-1709098855232.png

SBD__3-1709098866232.png

 

I.e. try .delete instead of .save of a nil value.

 

Let me know if I'm not following 🙂

 

Scott | Developer Advocate @ Shopify 

Av18
Shopify Partner
14 1 0

Hey @SBD_ 

 

"Thank you for providing the correct solution."

It successfully clears the values when I delete the metafield.