MySQL 嵌套JSON解析

MySQL自5.7之後開始支持json類型,相應的解析函數主要是json_extract()

查詢MySQL版本

select version()

示例

示例json1

未經過轉義的json串

{"l1":{"l1_1":["l1_1_1","l1_1_2"],"l1_2":{"l1_2_1":121,"l1_2_2":"122"}},"l2":{"l2_1":null,"l2_2":true,"l2_3":{}}}
示例json2

經過轉義後的json串

{"t_key":"haha","t_value":"{\"id\":\"14\",\"timestamp\":1539768556,\"type\":1}","test":2}

通過json_extract()可以獲取json裏面value對於的值:

mysql> select json_extract(field1, '$.t_key') from table1;
+----------------------------------------+
| json_extract(field1, '$.t_key')        |
+----------------------------------------+
| "haha"                                 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(field1, '$.t_value') from table1;
+-------------------------------------------------------+
| json_extract(field1, '$.t_value')              |
+-------------------------------------------------------+
| "{\"id\":\"14\",\"timestamp\":1539768556,\"type\":1}" |
+-------------------------------------------------------+
1 row in set (0.00 sec)

如果想去除兩側引號,可以先做類型轉換再做trim:

mysql> select trim(both '"' from cast(json_extract(field1, '$.t_value') as char)) from table1;
+----------------------------------------------------------------------------+
| trim(both '"' from cast(json_extract(field1, '$.t_value') as char)) |
+----------------------------------------------------------------------------+
| {\"id\":\"14\",\"timestamp\":1539768556,\"type\":1}                        |
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)

如果要在MySQL中對解析後的json再進行解析,則需要加上json_unquote函數以去掉escape character:

mysql> select json_unquote(json_extract(field1, '$.t_value')) from table1; 
+--------------------------------------------------------+
| json_unquote(json_extract(field1, '$.t_value')) |
+--------------------------------------------------------+
| {"id":"14","timestamp":1539768556,"type":1}            |
+--------------------------------------------------------+
1 row in set (0.00 sec)

如果要對解析過後的json繼續解析,則在上一步基礎上嵌套json_extract()

mysql> select json_extract(json_unquote(json_extract(field1, '$.t_value')), '$.timestamp') from table1;
+-------------------------------------------------------------------------------------+
| json_extract(json_unquote(json_extract(field1, '$.t_value')), '$.timestamp') |
+-------------------------------------------------------------------------------------+
| 1539768556                                                                          |
+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
JSON_UNQUOTE() Special Character Escape Sequences
Escape Sequence Character Represented by Sequence
\" A double quote (") character
\b A backspace character
\f A formfeed character
\n A newline (linefeed) character
\r A carriage return character
\t A tab character
\\ A backslash () character
\uXXXX UTF-8 bytes for Unicode value XXXX

json_extract的等效操作符是->;
json_unquote(json_extract())的等效操作符是->>;

參考資料1
參考資料2
參考資料3

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章