场景:MySQL要对'{pfAutoFinishStatus":"","shine":""挺好"","submitTime":"2023-04-05"}'这样的json字段值取数,因为shine对应的value有两个双引号,导致取出来的值为空值,并且干扰了后面的submitTime,导致其value也为空值。如何获得shine的value值:"挺好"呢?
预期结果:
分析:shine对应的value有两个双引号,一般遇到特殊字符,就要想到转义它,那么使用转义可不可以呢?接下来我们验证一下。
1、没有两个双引号,正常的一个双引号:'{ "pfAutoFinishStatus": "", "shine": "挺好", "submitTime": "2023-04-05" }'。
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "挺好", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE1;
2、 有两个双引号:'{ "pfAutoFinishStatus": "", "shine": ""挺好"", "submitTime": "2023-04-05" }'
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": ""挺好"", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE2;
3、有两个双引号加上转义:'{ "pfAutoFinishStatus": "", "shine": "\\"挺好\\"", "submitTime": "2023-04-05" }'
select JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "\\"挺好\\"", "submitTime": "2023-04-05" }', '$.shine') as JSON_VALUE3;
以上结果,与预期数据还是不一样。接着整
select JSON_UNQUOTE(JSON_EXTRACT('{ "pfAutoFinishStatus": "", "shine": "\\"挺好\\"", "submitTime": "2023-04-05" }', '$.shine')) as JSON_VALUE4;
知识储备:
1、JSON_EXTRACT函数:MySQL中json_extract函数说明
2、JSON_UNQUOTE函数:MySQL中json_extract函数说明