在 MySQL 中,JSON_UNQUOTE函数用于去除 JSON 对象中字符串值的引号。它接受一个 JSON 对象作为参数,并返回一个新的 JSON 对象,其中字符串值的引号被去除。
JSON_UNQUOTE(json_doc)
其中,`json_doc`是要去除引号的 JSON 对象。
这是详细信息得内容。
-- ---------------------------- -- Table structure for students -- ---------------------------- DROP TABLE IF EXISTS `students`; CREATE TABLE `students` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, `course` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of students -- ---------------------------- INSERT INTO `students` VALUES ('1', 'Odin', '{\"coursejson\": [{\"name\": \"语文\", \"score\": 81},{\"name\": \"数学\", \"score\": 74},{\"name\": \"英语\", \"score\": 95}]}'); INSERT INTO `students` VALUES ('2', '小胡', '{\"coursejson\": [{\"name\": \"语文\", \"score\": 31},{\"name\": \"数学\", \"score\": 64},{\"name\": \"英语\", \"score\": 55}]}'); INSERT INTO `students` VALUES ('3', 'Jack', '{\"coursejson\": [{\"name\": \"语文\", \"score\": 66},{\"name\": \"数学\", \"score\": 84},{\"name\": \"英语\", \"score\": 89}]}');
原始数据
mysql> select * from students; +----+--------+-------------------------------------------------------------------------------------------------------------------+ | id | name | course | +----+--------+-------------------------------------------------------------------------------------------------------------------+ | 1 | Odin | {"coursejson": [{"name": "语文", "score": 81},{"name": "数学", "score": 74},{"name": "英语", "score": 95}]} | | 2 | 小胡 | {"coursejson": [{"name": "语文", "score": 31},{"name": "数学", "score": 64},{"name": "英语", "score": 55}]} | | 3 | Jack | {"coursejson": [{"name": "语文", "score": 66},{"name": "数学", "score": 84},{"name": "英语", "score": 89}]} | +----+--------+-------------------------------------------------------------------------------------------------------------------+
先用JSON_EXTRACT 函数获取每位学生第一个对象的name属性值
mysql> SELECT id,name,JSON_EXTRACT(course,'$.coursejson[0].name') from students; +----+--------+---------------------------------------------+ | id | name | JSON_EXTRACT(course,'$.coursejson[0].name') | +----+--------+---------------------------------------------+ | 1 | Odin | "语文" | | 2 | 小胡 | "语文" | | 3 | Jack | "语文" | +----+--------+---------------------------------------------+
因为该值有引号,如果想去除可以使用JSON_UNQUOTE函数
mysql> SELECT id,name,JSON_UNQUOTE(JSON_EXTRACT(course,'$.coursejson[0].name')) from students; +----+--------+-----------------------------------------------------------+ | id | name | JSON_UNQUOTE(JSON_EXTRACT(course,'$.coursejson[0].name')) | +----+--------+-----------------------------------------------------------+ | 1 | Odin | 语文 | | 2 | 小胡 | 语文 | | 3 | Jack | 语文 | +----+--------+-----------------------------------------------------------+
或使用REPLACE函数也可以
mysql> SELECT id,name, REPLACE ( JSON_EXTRACT(course,'$.coursejson[0].name'), '"', '' ) from students; +----+--------+------------------------------------------------------------------+ | id | name | REPLACE ( JSON_EXTRACT(course,'$.coursejson[0].name'), '"', '' ) | +----+--------+------------------------------------------------------------------+ | 1 | Odin | 语文 | | 2 | 小胡 | 语文 | | 3 | Jack | 语文 | +----+--------+------------------------------------------------------------------+
需要注意的是,JSON_UNQUOTE函数只去除字符串值的引号,对于其他类型的值(如数字、布尔值、对象、数组等)不会进行任何操作。另外,如果 JSON 对象中嵌套了其他 JSON 对象或数组,JSON_UNQUOTE函数只会去除最外层 JSON 对象中字符串值的引号,而不会递归去除嵌套对象或数组中字符串值的引号。
在实际应用中,使用JSON_UNQUOTE函数时需要注意以下几点:
1. `JSON_UNQUOTE`函数不会改变 JSON 对象的结构,只是去除字符串值的引号。
2. `JSON_UNQUOTE`函数不会检查 JSON 对象的合法性,如果输入的 JSON 对象不是有效的 JSON 格式,函数可能会返回错误结果。
3. `JSON_UNQUOTE`函数不会修改原始的 JSON 对象,而是返回一个新的 JSON 对象。