Seems I cannot use JSON inside MYSQL tables – there is no such an option when choosing data type for a field – phpMyAdmin ver 4.8.3
.
Question – if I use php functions json_encode
and json_decode
to get and set values inside varchar
or text
field – do I need JSON fields at all?
3
Answers
You can store you JSON Object as a string in a
string
field or you can use it in a field which hold data of JSON typeThis is valide with MySQL version 8
Advantage
You can read the entire documentation here The JSON Data Type
The Cons
If you choose to use a
VARCHAR
ORTEXT
field to store you JSON data as a String, It’s up to you to parse the Data correctly after you have retrieve It from the database for that to behave like a correct JSON objectYes you do, unless you don’t want to perform searches on the key-values of those json strings. MySQL Json Type allows you to do that, apart from storing the json in an efficient way.
You could use a blob or a text type. Varchar is too short for a json, unless you have jsons that are less than 255 chars long.
A blob could be a good idea. You can map that to a resource in php (not loading the whole file into memory) and then use a json stream parser like this one to read the contents of your file.
But again, storing a json as a string will not allow you to query in an efficient way using your json data.
As per the official documentation
Check your MySQL version to find out if it supports JSON datatype.
An answer to your question:
You can
json_encode
your data and store it in atext
field. After retrieving it from the database table, do ajson_decode
.