skip to Main Content

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


  1. 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 type

    This is valide with MySQL version 8

    Advantage

    1. The Advantage with this type is that It perform automatic validation of JSON documents stored in JSON columns and invalid documents produce an error.
    2. Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements

    You can read the entire documentation here The JSON Data Type

    The Cons

    If you choose to use a VARCHAR OR TEXT 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 object

    Login or Signup to reply.
  2. Yes 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.

    NOTE: Mysql supports the Json Type from 5.7.8. PhpMyAdmin is another piece of software. If your json type is not there, is probably because of your MySQL version. Run mysql -V in your terminal to check.

    Login or Signup to reply.
  3. As per the official documentation

    As of MySQL 5.7.8, MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents.

    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 a text field. After retrieving it from the database table, do ajson_decode.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search