David542
David542

Reputation: 110462

Proper way to store json in database

Does json usually have enclosing quotation marks when stored in a database field?

Which of the following would be correct for the json column?

`json`
'{"Color": "Red"}'
 {"Color": "Red"}

My assumption would be the second, but just wanted to make sure that's correct. And if so, why?

Upvotes: 1

Views: 147

Answers (2)

Jakub Lortz
Jakub Lortz

Reputation: 14894

As of MySQL 5.7.8, MySQL supports a native JSON data type

If you use earlier version and store it as text, store it without the enclosing quotation marks.

Upvotes: 3

Andrew
Andrew

Reputation: 1324

While it is possible to store the data as you suggest, it would be better to store the data in a table named json with field color then insert two records, each containing the value 'red'.

It's more work now, because it involves deconstructing and reconstructing the JSON, but it saves work later if you need to serve the data in some format other than JSON, then you won't need to reformat all of the data in your database.

Upvotes: 1

Related Questions