We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

graphQL - metaobjectUpsert and metaobjectCreate, error 'Value is invalid JSON: unexpected token'

graphQL - metaobjectUpsert and metaobjectCreate, error 'Value is invalid JSON: unexpected token'

dartacus
Shopify Partner
33 2 3

Hi,

 

Have got an irritating issue with graphQL calls to either create or upsert metaobjects. They always give the same error: Value is invalid JSON: unexpected token at 'gid://shopify/TaxonomyValue/1'.

 

Here's the variables object I'm passing in:

dartacus_0-1750089345421.png

 

...and that matches up pretty exactly to the example in https://shopify.dev/docs/api/admin-graphql/latest/mutations/metaobjectcreate

 

When I query this metaobject, the existing entries it returns are all in that format. For example here's a dump of the 'green' entry, as it is returned from the metaobject API call:

 

[4] => stdClass Object
(
[node] => stdClass Object
(
[id] => gid://shopify/Metaobject/167683359049
[handle] => green-reference
[type] => shopify--color-pattern
[displayName] => Green reference
[fields] => Array
(
[0] => stdClass Object
(
[key] => label
[value] => Green reference
)

[1] => stdClass Object
(
[key] => color
[value] => #05AA3D
)

[2] => stdClass Object
(
[key] => image
[value] =>
)

[3] => stdClass Object
(
[key] => color_taxonomy_reference
[value] => ["gid://shopify/TaxonomyValue/9"]
)

[4] => stdClass Object
(
[key] => pattern_taxonomy_reference
[value] => gid://shopify/TaxonomyValue/2874
)

)

)

)

 

The sharper-eyed among you will have noted that color_taxonomy_reference is an array: I've tried providing that field as an array when I try to create one and it doesn't work either.

 

Any ideas anyone?

 

(PS: respectfully, please don't pitch your service or app to me, this is a technical question and I'm hoping for answers/suggestions, I am not looking to install anything or outsource anything, thank you)

 

Many thanks,

 

G

 

Replies 2 (2)
dartacus
Shopify Partner
33 2 3

Thanks Samuel. I forgot to add that the query string and variables object are json-encoded before being POSTed. The same method works for other mutations that have a query string and a variables object that contain arrays, such as productCreateMedia, but I'll try it anyway.

 

G

dartacus
Shopify Partner
33 2 3

Well, I'm utterly amazed - that worked. Brilliant suggestion, thank you.

 

Completely strange and weird, though, to be honest. It's just bonkers that certain input variables have to be encoded as json and then encoded as json AGAIN when creating the payload. I've never had to do this for any other API and I haven't even had to do it for other graphQL calls that take arrays as inputs.

 

If any Shopify staff are reading this, please, please update your documentation to mention this when it's required for a mutation. This cost me hours of time.

 

Thanks again Samuel.

 

G