首頁  >  文章  >  資料庫  >  mysql中的json_extract怎麼使用

mysql中的json_extract怎麼使用

WBOY
WBOY轉載
2023-05-31 16:58:161777瀏覽

    一、前言

    mysql5.7版本開始支援JSON類型欄位
    json_extract可以完全簡寫為->
    json_unquote(json_extract())可以完全簡寫為->>
    下面介紹大部分會利用簡寫

    #二、創建範例表格

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

    - 取得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”: “湖南”}}

    # 得到"tom"
    select json_extract(content,'$.name') from test_json where id = 1;
    # 简写方式:字段名->表达式等价于json_extract(字段名,表达式)
    select content->'$.name' from test_json where id = 1;
    # 结果:
    +--------------------------------+
    | json_extract(content,'$.name') |
    +--------------------------------+
    | "tom"                          |
    +--------------------------------+
    +-------------------+
    | content->'$.name' |
    +-------------------+
    | "tom"             |
    +-------------------+
    
    # 解除双引号,得到tom
    select json_unquote(json_extract(content,'$.name')) from test_json where id = 1;
    # 简写方式:字段名->>表达式等价于json_unquote(json_extract(字段名,表达式))
    select content->>'$.name' from test_json where id = 1;
    # 结果:
    +----------------------------------------------+
    | json_unquote(json_extract(content,'$.name')) |
    +----------------------------------------------+
    | tom                                          |
    +----------------------------------------------+
    +--------------------+
    | content->>'$.name' |
    +--------------------+
    | tom                |
    +--------------------+
    - 取得JSON數組中某個元素

    • json_extract函數中,第一個參數content表示json數據,第二個參數為json路徑,其中$表示該json資料本身,$[i]表示取得該json陣列索引為i的元素(索引從0開始)

    • 與取得key-val一樣,若取得的元素為字串,預設的方式也會得到雙引號包起來的字符,導致程式轉義,方法也是利用json_unquote函數,或使用->> 代替->

    content:

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

    # 得到"apple"
    select json_extract(content,'$[1]') from test_json where id = 2;
    # 简写,效果同上
    select content->'$[1]' from test_json where id = 2;
    # 结果:
    +------------------------------+
    | json_extract(content,'$[1]') |
    +------------------------------+
    | "apple"                      |
    +------------------------------+
    +-----------------+
    | content->'$[1]' |
    +-----------------+
    | "apple"         |
    +-----------------+
    
    # 解除双引号,得到apple 
    select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2;
    # 简写,效果同上
    select content->>'$[1]' from test_json where id = 2;
    # 结果:
    +--------------------------------------------+
    | json_unquote(json_extract(content,'$[1]')) |
    +--------------------------------------------+
    | apple                                      |
    +--------------------------------------------+
    +------------------+
    | content->>'$[1]' |
    +------------------+
    | apple            |
    +------------------+
    - 取得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”}]

    # 得到:87
    select content->'$.score[2]' from test_json where id = 1;
    # 结果:
    +-----------------------+
    | content->'$.score[2]' |
    +-----------------------+
    | 87                    |
    +-----------------------+
    
    # 得到:18
    select content->'$[3].age' from test_json where id = 2;
    # 结果:
    +---------------------+
    | content->'$[3].age' |
    +---------------------+
    | 18                  |
    +---------------------+
    四、漸入佳境

    #- 取得JSON多個路徑的資料

    將會將多個路徑的資料組合成陣列傳回

    content: id=1

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

    select json_extract(content,'$.age','$.score') from test_json where id = 1;
    # 结果:
    +-----------------------------------------+
    | json_extract(content,'$.age','$.score') |
    +-----------------------------------------+
    | [18, [100, 90, 87]]                     |
    +-----------------------------------------+
    
    select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1;
    # 结果:
    +----------------------------------------------------------------------+
    | json_extract(content,'$.name','$.address.province','$.address.city') |
    +----------------------------------------------------------------------+
    | ["tom", "湖南", "长沙"]                                              |
    +----------------------------------------------------------------------+
    - 路徑表達式*的使用

    將會將多個路徑的資料組合成陣列傳回

    # 先插入一条用于测试的数据
    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”: “中央公園”}}

    # 获取所有二级嵌套中key=name的值
    # 由于friend的二级嵌套是一个数组,所以.name获取不到其中的所有name值
    select content->'$.*.name' from test_json where id = 3;
    +----------------------------------+
    | content->'$.*.name'              |
    +----------------------------------+
    | ["一年三班", "中央公园"]         |
    +----------------------------------+```
    
    # 获取所有key为name值的数据,包括任何嵌套内的name
    select content->'$**.name' from test_json where id = 3;
    +---------------------------------------------------------+
    | content->'$**.name'                                     |
    +---------------------------------------------------------+
    | ["tom", "一年三班", "marry", "Bob", "中央公园"]         |
    +---------------------------------------------------------+
    
    # 获取数组中所有的name值
    select content->'$.friend[*].name' from test_json where id = 3;
    +-----------------------------+
    | content->'$.friend[*].name' |
    +-----------------------------+
    | ["marry", "Bob"]            |
    +-----------------------------+
    - 傳回NULL值

    content: id=1

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

    #尋找的JSON路徑都不存在

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

    路徑中有NULL

    #

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

    - 回傳錯誤

    若第一個參數不是JSON類型的數據,則回傳錯誤

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

    若路徑表達式不規範,則傳回錯誤

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

    五、使用場景

    JSON_EXTRACT函數通常用於要取得JSON中某個特定的資料或要根據它作為判斷條件時使用

    以上是mysql中的json_extract怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除