How to get values from MySQL(5.6) column if that contains json document as string

前端 未结 10 705
萌比男神i
萌比男神i 2020-12-02 18:48

How to get values from MySQL(5.6) column if that contains JSON document as a string

For example, if we have a table - employee in that we have three columns id, nam

10条回答
  •  情书的邮戳
    2020-12-02 19:40

    To be able to do what you want to, you need MySQL 5.7.8+. Since 5.7.8 you can use JSON_EXTRACT function to extract a value from a JSON string:

    SELECT JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name');
    
    +---------------------------------------------------------+
    | JSON_EXTRACT('{"id": 14, "name": "Aztalan"}', '$.name') |
    +---------------------------------------------------------+
    | "Aztalan"                                               |
    +---------------------------------------------------------+
    

    Taken from here.

    In MySQL 5.6 you just can't get the value you want as MySQL doesn't know anything about what a JSON object is. So your options are:

    • Upgrade to 5.7.8+
    • Parse the query result with something that handles JSON:
      • Could be PHP json_decode (or equivalent in your language)
      • An online tool like http://json.parser.online.fr/

提交回复
热议问题