productCreate mutation with json_string metafield returns invalid JSON error

Highlighted
New Member
7 0 0

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. 

0 Likes
Highlighted
Shopify Partner
520 38 109

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.

0 Likes
Highlighted
New Member
7 0 0

Hi @Greg_Kujawa 

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!

0 Likes
Highlighted
Shopify Partner
520 38 109

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

0 Likes
Highlighted
New Member
7 0 0

Hi @Greg_Kujawa 

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

0 Likes
Highlighted
Shopify Partner
520 38 109

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? 

0 Likes
Highlighted
New Member
7 0 0

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

0 Likes
Highlighted
Shopify Partner
520 38 109

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?  

0 Likes
Highlighted
New Member
7 0 0

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

0 Likes