How to store json object in the metafield

How to store json object in the metafield

Sachin__
Excursionist
20 2 9

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.

Replies 10 (10)

HunkyBill
Shopify Partner
4853 60 562

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! [email protected] http://www.resistorsoftware.com
Sachin__
Excursionist
20 2 9

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"
            }
          ]
        }}
HunkyBill
Shopify Partner
4853 60 562

So what is your error?

Custom Shopify Apps built just for you! [email protected] http://www.resistorsoftware.com
Sachin__
Excursionist
20 2 9

Error:- 

"userErrors": [
        {
          "field": [
            "metafields",
            "0",
            "value"
          ],
          "message": "Value for metafield test2:testkey2 is invalid JSON: 809: unexpected token at '{'key1': 'value1','key2':'value2'}'"
        }
      ]
HunkyBill
Shopify Partner
4853 60 562

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

Custom Shopify Apps built just for you! [email protected] http://www.resistorsoftware.com
Sachin__
Excursionist
20 2 9

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.

 

Sachin__
Excursionist
20 2 9

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 !.

HunkyBill
Shopify Partner
4853 60 562

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! [email protected] http://www.resistorsoftware.com
Sachin__
Excursionist
20 2 9

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

Sachin__
Excursionist
20 2 9

It worked  ,Is is just not working in the shopify-graphiql-app .