mysql中的json_extract怎么使用

前端开发   发布日期:2025年03月10日   浏览次数:209

这篇文章主要介绍“mysql中的json_extract怎么使用”,在日常操作中,相信很多人在mysql中的json_extract怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mysql中的json_extract怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    一、前言

    mysql5.7版本开始支持JSON类型字段
    json_extract可以完全简写为 ->
    json_unquote(json_extract())可以完全简写为 ->>
    下面介绍中大部分会利用简写

    二、创建示例表

    1. CREATE TABLE `test_json` (
    2. `id` int(11) NOT NULL AUTO_INCREMENT,
    3. `content` json DEFAULT NULL,
    4. PRIMARY KEY (`id`)
    5. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
    1. # 插入两条测试用的记录
    2. INSERT INTO `test_json` (`content`) VALUES ('{"name":"tom","age":18,"score":[100,90,87],"address":{"province":"湖南","city":"长沙"}}');
    3. INSERT INTO `test_json` (`content`) VALUES ('[1, "apple", "red", {"age": 18, "name": "tom"}]');
    id content
    1 {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
    2 [1, “apple”, “red”, {“age”: 18, “name”: “tom”}]

    三、基本语法

    - 获取JSON对象中某个key对应的value值

    • json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$.name就表示获取json中key为name的value值

    • 可以利用 -> 表达式来代替json_extract

    • 若获取的val本身为字符串,那么获取的val会被引号包起来,比如"tom",这种数据被解析到程序对象中时,可能会被转义为“tom”。为了解决这个问题了,可以在外面再包上一层json_unquote函数,或者使用 ->> 代替->

    content:
    {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

    1. # 得到"tom"
    2. select json_extract(content,'$.name') from test_json where id = 1;
    3. # 简写方式:字段名->表达式等价于json_extract(字段名,表达式)
    4. select content->'$.name' from test_json where id = 1;
    5. # 结果:
    6. +--------------------------------+
    7. | json_extract(content,'$.name') |
    8. +--------------------------------+
    9. | "tom" |
    10. +--------------------------------+
    11. +-------------------+
    12. | content->'$.name' |
    13. +-------------------+
    14. | "tom" |
    15. +-------------------+
    16. # 解除双引号,得到tom
    17. select json_unquote(json_extract(content,'$.name')) from test_json where id = 1;
    18. # 简写方式:字段名->>表达式等价于json_unquote(json_extract(字段名,表达式))
    19. select content->>'$.name' from test_json where id = 1;
    20. # 结果:
    21. +----------------------------------------------+
    22. | json_unquote(json_extract(content,'$.name')) |
    23. +----------------------------------------------+
    24. | tom |
    25. +----------------------------------------------+
    26. +--------------------+
    27. | content->>'$.name' |
    28. +--------------------+
    29. | tom |
    30. +--------------------+

    - 获取JSON数组中某个元素

    • json_extract函数中,第一个参数content表示json数据,第二个参数为json路径,其中$表示该json数据本身,$[i]表示获取该json数组索引为i的元素(索引从0开始)

    • 与获取key-val一样,若获取的元素为字符串,默认的方式也会得到双引号包起来的字符,导致程序转义,方法也是利用json_unquote函数,或者使用 ->> 代替->

    content:
    [1, “apple”, “red”, {“age”: 18, “name”: “tom”}]

    1. # 得到"apple"
    2. select json_extract(content,'$[1]') from test_json where id = 2;
    3. # 简写,效果同上
    4. select content->'$[1]' from test_json where id = 2;
    5. # 结果:
    6. +------------------------------+
    7. | json_extract(content,'$[1]') |
    8. +------------------------------+
    9. | "apple" |
    10. +------------------------------+
    11. +-----------------+
    12. | content->'$[1]' |
    13. +-----------------+
    14. | "apple" |
    15. +-----------------+
    16. # 解除双引号,得到apple
    17. select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2;
    18. # 简写,效果同上
    19. select content->>'$[1]' from test_json where id = 2;
    20. # 结果:
    21. +--------------------------------------------+
    22. | json_unquote(json_extract(content,'$[1]')) |
    23. +--------------------------------------------+
    24. | apple |
    25. +--------------------------------------------+
    26. +------------------+
    27. | content->>'$[1]' |
    28. +------------------+
    29. | apple |
    30. +------------------+

    - 获取JSON中的嵌套数据

    结合前面介绍的两种获取方式,可以获取json数据中的嵌套数据

    content: id=1
    {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}
    content: id=2
    [1, “apple”, “red”, {“age”: 18, “name”: “tom”}]

    1. # 得到:87
    2. select content->'$.score[2]' from test_json where id = 1;
    3. # 结果:
    4. +-----------------------+
    5. | content->'$.score[2]' |
    6. +-----------------------+
    7. | 87 |
    8. +-----------------------+
    9. # 得到:18
    10. select content->'$[3].age' from test_json where id = 2;
    11. # 结果:
    12. +---------------------+
    13. | content->'$[3].age' |
    14. +---------------------+
    15. | 18 |
    16. +---------------------+

    四、渐入佳境

    - 获取JSON多个路径的数据

    将会把多个路径的数据组合成数组返回

    content: id=1
    {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

    1. select json_extract(content,'$.age','$.score') from test_json where id = 1;
    2. # 结果:
    3. +-----------------------------------------+
    4. | json_extract(content,'$.age','$.score') |
    5. +-----------------------------------------+
    6. | [18, [100, 90, 87]] |
    7. +-----------------------------------------+
    8. select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1;
    9. # 结果:
    10. +----------------------------------------------------------------------+
    11. | json_extract(content,'$.name','$.address.province','$.address.city') |
    12. +----------------------------------------------------------------------+
    13. | ["tom", "湖南", "长沙"] |
    14. +----------------------------------------------------------------------+

    - 路径表达式*的使用

    将会把多个路径的数据组合成数组返回

    1. # 先插入一条用于测试的数据
    2. INSERT INTO `test_json` (`id`,`content`) VALUES(3,'{"name":"tom","address":{"name":"中央公园","city":"长沙"},"class":{"id":3,"name":"一年三班"},"friend":[{"age":20,"name":"marry"},{"age":21,"name":"Bob"}]}')

    content: id=3
    {“name”: “tom”, “class”: {“id”: 3, “name”: “一年三班”}, “friend”: [{“age”: 20, “name”: “marry”}, {“age”: 21, “name”: “Bob”}], “address”: {“city”: “长沙”, “name”: “中央公园”}}

    1. # 获取所有二级嵌套中key=name的值
    2. # 由于friend的二级嵌套是一个数组,所以.name获取不到其中的所有name值
    3. select content->'$.*.name' from test_json where id = 3;
    4. +----------------------------------+
    5. | content->'$.*.name' |
    6. +----------------------------------+
    7. | ["一年三班", "中央公园"] |
    8. +----------------------------------+```
    9. # 获取所有key为name值的数据,包括任何嵌套内的name
    10. select content->'$**.name' from test_json where id = 3;
    11. +---------------------------------------------------------+
    12. | content->'$**.name' |
    13. +---------------------------------------------------------+
    14. | ["tom", "一年三班", "marry", "Bob", "中央公园"] |
    15. +---------------------------------------------------------+
    16. # 获取数组中所有的name值
    17. select content->'$.friend[*].name' from test_json where id = 3;
    18. +-----------------------------+
    19. | content->'$.friend[*].name' |
    20. +-----------------------------+
    21. | ["marry", "Bob"] |
    22. +-----------------------------+

    - 返回NULL值

    content: id=1
    {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “长沙”, “province”: “湖南”}}

    寻找的JSON路径都不存在

    1. # age路径不存在,返回NULL
    2. # 若有多个路径,只要有一个路径存在则不会返回NULL
    3. select json_extract(content,'$.price') from test_json where id = 1;
    4. +---------------------------------+
    5. | json_extract(content,'$.price') |
    6. +---------------------------------+
    7. | NULL |
    8. +---------------------------------+

    路径中有NULL

    1. # 存在任意路径为NULL则返回NULL
    2. select json_extract(content,'$.age',NULL) from test_json where id = 1;
    3. +------------------------------------+
    4. | json_extract(content,'$.age',NULL) |
    5. +------------------------------------+
    6. | NULL |
    7. +------------------------------------+

    - 返回错误

    若第一个参数不是JSON类型的数据,则返回错误

    1. select json_extract('{1,2]',$[0])

    若路径表达式不规范,则返回错误

    1. select content->'$age' from test_json where id = 1;
    2. # 结果:
    3. ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.

    五、使用场景

    JSON_EXTRACT函数通常用于要获取JSON中某个特定的数据或者要根据它作为判断条件时使用

    以上就是mysql中的json_extract怎么使用的详细内容,更多关于mysql中的json_extract怎么使用的资料请关注九品源码其它相关文章!