What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Would it make sense to store an array of JSON objects in a metafield with type string?

Solved

Would it make sense to store an array of JSON objects in a metafield with type string?

andy-a
Tourist
10 0 1

To cut back on query costs in graphql, I was thinking about storing data in a single metafield. All the data is related, and I often need to get it all at once. According to the documents on metafield types, the JSON string type is depreciated so I guess I would have to store the data as a long string with type single_line_text_field. So I would have to make the JSON object, stringify it, then add it to the metafield. However, JSON Objects and the metafields require double quotes so I would have to escape all the quotes when turning the object into a string (Unsure of this - JSON.stringify() and/or graphql may take care of that for me?).

 

 

var data_obj = {"key1": "value1", "key2": "value2"};

{ 
  "metafield": {
    "value": "{\"key1\": \"value1\", \"key2\": \"value2\"}"
  }
}

 

 

I could then use JSON.parse() on the value to turn it back into an object when I need to read/edit a certain key.

 

It will get a little more complicated when I want an array of objects as I will have to figure out how to go from a string to an array without messing up the objects (probably using some regular expressions).

 

Will this work or is it a bad idea? The docs say that the character limit for a string is 5 million, so I don't need to worry about that. But I'm worried that having to go back and forth between a string and an array of objects will cause some errors. It would be nice to just use the JSON string metafield type, but I'm assuming I shouldn't use something that is depreciated.

Accepted Solution (1)

BenSehl
Shopify Staff
3 3 3

This is an accepted solution.

Hey Andy, 

 

I'm going to double check on this for you to make sure I'm correct here (if I'm wrong, I'll update this), but `json` is a valid type. The note you mentioned about the deprecated `json_string` is referring to a previous type that had existed. If you look at the column "API Name", there previously was a type there with that json_string namespace.

 

You’re correct that the Javascript methods JSON.parse() and JSON.stringify() will convert a JSON object from/to a string and add those quotes for you. Check out the Mozilla docs for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

 

Good luck! 

 

Ben

Ben Sehl

View solution in original post

Replies 2 (2)

BenSehl
Shopify Staff
3 3 3

This is an accepted solution.

Hey Andy, 

 

I'm going to double check on this for you to make sure I'm correct here (if I'm wrong, I'll update this), but `json` is a valid type. The note you mentioned about the deprecated `json_string` is referring to a previous type that had existed. If you look at the column "API Name", there previously was a type there with that json_string namespace.

 

You’re correct that the Javascript methods JSON.parse() and JSON.stringify() will convert a JSON object from/to a string and add those quotes for you. Check out the Mozilla docs for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

 

Good luck! 

 

Ben

Ben Sehl
andy-a
Tourist
10 0 1

This helps a lot, thank you!