How to store json object in the metafield

Sachin__
Excursionist
19 2 1

How i can store a json object inside the value of metafield of type json_string by graphql its showing error.

when i tried this way:

 

 

 

"metafields": [
            {
              "namespace": "test2",
              "key": "testkey2",
              "value": "{'key1': 'value1','key2':'value2'}",
              "valueType": "JSON_STRING"
            }
          ]

 

 

 


"value_type": "json_string",

 

Please let me know how to solve this .how i can pass json i tried with \ this but want to store clean object.

0 Likes
HunkyBill
Shopify Expert
4481 45 485

Would be helpful if you showed off more than than that snippet. Try starting off with the exact error, and also more of the code you use to setup and execute the GraphQL mutation.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
Sachin__
Excursionist
19 2 1

Mutation :- 

mutation productUpdate($input: ProductInput!) {
  productUpdate(input: $input) {
    product {
      id
      title
      metafields(first: 10) {
        edges {
          node {
            id
            value
            valueType
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Query Variable:-

{ "input": {
          "id": "gid://shopify/Product/6688456376473",
          "metafields": [
             {
              "namespace": "test2",
              "key": "testkey2",
              "value": "{'key1': 'value1','key2':'value2'}",
              "valueType": "JSON_STRING"
            }
          ]
        }}
0 Likes
HunkyBill
Shopify Expert
4481 45 485

So what is your error?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Sachin__
Excursionist
19 2 1

Error:- 

"userErrors": [
        {
          "field": [
            "metafields",
            "0",
            "value"
          ],
          "message": "Value for metafield test2:testkey2 is invalid JSON: 809: unexpected token at '{'key1': 'value1','key2':'value2'}'"
        }
      ]
0 Likes
HunkyBill
Shopify Expert
4481 45 485

Probably your use of those single quotes. Try using the usual quotes. Always works for me.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Sachin__
Excursionist
19 2 1

Not working After adding double quotes

Sachin___1-1618595637696.png

 

Working only by adding / but i don't want to do like this .

Sachin___0-1618595582917.png

 

Can you share your code snippet that worked for you.

 

0 Likes
Sachin__
Excursionist
19 2 1

Anyone have any solution of this i want to store data in metafield in the form of the object how to do that.Please help !.

0 Likes
HunkyBill
Shopify Expert
4481 45 485

Make sure you're saving JSON as your value. That is all. It works. I have made tens of thousands of JSON_STRING value type Metafields lately in just one App and they work fine. It seems your problem continues to be how you format your JSON.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Sachin__
Excursionist
19 2 1

Please can you show me any example of json format i can enter here.

0 Likes