productCreate mutation with json_string metafield returns invalid JSON error

productCreate mutation with json_string metafield returns invalid JSON error

lthomas1
Shopify Partner
30 0 5

Hey,

I receive an error -

message": "Value is invalid JSON: 434: unexpected token at '{\"Name\": \"Overall Measurements\", \"Value\": \"30\" x 14.5\" (76.2cm x 37cm)\", \"Seq\": \"10\"}, {\"Name\": \"Handles\", \"Value\": \"Shoulder Straps, Top Grab Handle\", \"Seq\": \"60\"}]}'"}],

on the below mutation.

mutation { SKUAKNB135COUNT4:productCreate(input :{title:"Mesh Backpack (AKB135)" metafields:[{value:"""{"Spec": [{"Name": "Overall Measurements", "Value": "30\" x 14.5\" (76.2cm x 37cm)", "Seq": "10"}, {"Name": "Handles", "Value": "Shoulder Straps, Top Grab Handle", "Seq": "60"}]}""" key:"specs" valueType:JSON_STRING namespace:"pdct_custom_fields" }] vendor:"Akona" handle:"AKNB135-product_handle" productType:"Gear Bags" images:[] variants : [] }) { userErrors { field message } product { id } }}

I checked the metafield value and it appears to be a valid json. Can anyone help me on this. 

Replies 8 (8)

Gregarican
Shopify Partner
1033 86 291

Can you try out pasting your code into the Shopify GraphiQL app and see how it goes? https://shopify.dev/tools/graphiql-admin-api  My first guess would be all of the quotation marks and their escaping might be awry.

lthomas1
Shopify Partner
30 0 5

Hi @Gregarican 

Tried that too, get the same error there too. 

lthomas1_0-1597792402855.png



Also the metafield value has a double quote inside - which is actually defining inch(") and I escaped it. So the value is a valid json it seems.

lthomas1_1-1597792524846.png


Thanks!

Gregarican
Shopify Partner
1033 86 291

I would suggest checking the Metafield fields you are passing into your request. I don’t see anything listed as a Seq property in the API docs for example. Here are some screen shots from the GraphQL API docs for Metafields. Namespace, Key, Value, and ValueType are about it.

A44C6344-0CE2-40DE-90C3-6766B002AB65.png

6FD9FD95-D62B-4C5F-8E91-C2D85841B9E9.png

lthomas1
Shopify Partner
30 0 5

Hi @Gregarican 

Seq is not a part of graphql propery. It's just the value of a metafield. And the metafield type is json_string.

metafields:[{value:"""{"Spec": [{"Name": "Overall Measurements", "Value": "30\" x 14.5\" (76.2cm x 37cm)", "Seq": "10"}, {"Name": "Handles", "Value": "Shoulder Straps, Top Grab Handle", "Seq": "60"}]}""" key:"specs" valueType:JSON_STRING namespace:"pdct_custom_fields" }]

^ is how I defined metafield on graphql mutation

Gregarican
Shopify Partner
1033 86 291

I see that now...sorry I didn't look closely enough at what you were sending. And you're sure that Shopify can accept a complex JSON string as a metafield data type? I honestly have only seen basic data types such as strings. Can you try sending a simple string to see what the outcome would be like in the example in their API documentation? 

lthomas1
Shopify Partner
30 0 5

simple strings will work. It's just that my data is a bit complex 😞

Gregarican
Shopify Partner
1033 86 291

Then that tells us that you apparently cannot have a JSON string data type. An alternative would be packing everything you need into a simple string, that contains the data set you need delimited in there somehow. Something like this, where the pipe character delimits the various elements. And within each element the colon character delimits each key name and assigned value.

Value: "Key1:Value1|Key2:Value2|Key3:Value3"

Your third-party code could then parse that delimited string to break out what you are looking for. Make sense?  

lthomas1
Shopify Partner
30 0 5

After trying many ways I skipped the double quotes inside a json string value and passed it as json_sting itself.