JSON_EXTRACT_STRING
描述
JSON_EXTRACT_STRING
从 JSON 对象中提取 <json_path>
指定的字段,并将其转换为 STRING
类型。
语法
JSON_EXTRACT_STRING(<json_object>, <json_path>)
参数
<json_object>
JSON 类型,要提取的目标参数。<json_path>
String 类型,要从目标 JSON 中提取目标元素的 JSON 路径。
返回值
Nullable(STRING)
返回提取出的 STRING 值,某些情况会得到 NULL
使用说明
- 如果
<json_object>
或则<json_path>
为 NULL,返回 NULL。 - 如果
<json_path>
指定的元素不存在返回 NULL。 - 如果
<json_path>
指定的元素无法转换为 STRING 返回 NULL。 - 其行为与 "cast + json_extract" 一致,即等价于:
所以即使
CAST(JSON_EXTRACT(<json_object>, <json_path>) as STRING)
<json_path>
指向的对象不是 STRING 类型,但是只要支持转换为 STRING 类型也能得到转换后的值。 - 这里返回的 STRING 是不带有双引号的(")。
- 对于 JSON 对象中的 null 值,得到的不是 NULL 而是字符串 null,如果想要判断一个元素是否为 null 请使用函数
JSON_EXTRACT_ISNULL
。
示例
- 正常参数
SELECT json_extract_string('{"id": 123, "name": "doris"}', '$.name');
+---------------------------------------------------------------+
| json_extract_string('{"id": 123, "name": "doris"}', '$.name') |
+---------------------------------------------------------------+
| doris |
+---------------------------------------------------------------+ - 路径不存在的情况
SELECT json_extract_string('{"id": 123, "name": "doris"}', '$.name2');
+----------------------------------------------------------------+
| json_extract_string('{"id": 123, "name": "doris"}', '$.name2') |
+----------------------------------------------------------------+
| NULL |
+----------------------------------------------------------------+ - NULL 参数
SELECT json_extract_string('{"id": 123, "name": "doris"}', NULl);
+-----------------------------------------------------------+
| json_extract_string('{"id": 123, "name": "doris"}', NULl) |
+-----------------------------------------------------------+
| NULL |
+-----------------------------------------------------------+SELECT json_extract_string(NULL, '$.id2');
+------------------------------------+
| json_extract_string(NULL, '$.id2') |
+------------------------------------+
| NULL |
+------------------------------------+ - 其他类型被转换为 STRING 的情况
SELECT json_extract_string('{"id": 123, "name": "doris"}','$.id');
+------------------------------------------------------------+
| json_extract_string('{"id": 123, "name": "doris"}','$.id') |
+------------------------------------------------------------+
| 123 |
+------------------------------------------------------------+ - null 值会被转换为字符串 "null" 而不是 NULL
SELECT json_extract_string('{"id": null, "name": "doris"}','$.id');
+-------------------------------------------------------------+
| json_extract_string('{"id": null, "name": "doris"}','$.id') |
+-------------------------------------------------------------+
| null |
+-------------------------------------------------------------+