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 SequenceCharacter Represented by Sequence
\"A double quote (") character
\bA backspace character
\fA formfeed character
\nA newline (linefeed) character
\rA carriage return character
\tA tab character
\\A backslash () character
\uXXXXUTF-8 bytes for Unicode value XXXX

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

参考资料1
参考资料2
参考资料3

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gcygeeker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值