JSON_OBJECT
描述
生成一个或者多个包含指定 Key-Value 对的 json object, 当 Key 值为 NULL 或者传入参数为奇数个时,返回异常错误。
语法
JSON_OBJECT (<key>, <value>[,<key>, <value>, ...])
参数
参数 | 描述 |
---|---|
<key> | 指定生成的 json object 的 Key-Value 中的 Key 值 |
<value> | 指定生成的 json object 的 Key-Value 中的 Value 值 |
注意事项
- 按照惯例,参数列表由交替的键和值组成。
- Key 按照JSON 定义强制转换为文本。
- Value 参数按照可以转换为 json 的方式进行转换,现在我们支持 array/struct/map/json 作为值
返回值
返回一个 json object。特殊情况如下:
- 如果没有传入参数,返回一个空的 json object。
- 如果传入的参数个数为奇数个,返回异常错误。
- 如果传入的 Key 为 NULL,返回异常错误。
- 如果传入的 Value 为 NULL,返回的 json object 中该 Key-Value 对的 Value 值为 NULL。
示例
select json_object();
+---------------+
| json_object() |
+---------------+
| {} |
+---------------+
select json_object('time',curtime());
+--------------------------------+
| json_object('time', curtime()) |
+--------------------------------+
| {"time": "10:49:18"} |
+--------------------------------+
SELECT json_object('id', 87, 'name', 'carrot');
+-----------------------------------------+
| json_object('id', 87, 'name', 'carrot') |
+-----------------------------------------+
| {"id": 87, "name": "carrot"} |
+-----------------------------------------+
select json_object('username',null);
+---------------------------------+
| json_object('username', 'NULL') |
+---------------------------------+
| {"username": NULL} |
+---------------------------------+
select json_object(null,null);
ERROR 1105 (HY000): errCode = 2, detailMessage = json_object key can't be NULL: json_object(NULL)
-- support array as object value
SELECT json_object('id', 1, 'level', array('"aaa"','"bbb"'));
+------------------------------------------------------------------------------------------------------+
| json_object('id', cast(1 as VARCHAR(65533)), 'level', cast(array('"aaa"', '"bbb"') as JSON), '6267') |
+------------------------------------------------------------------------------------------------------+
| {"id":1,"level":["\"aaa\"","\"bbb\""]} |
+------------------------------------------------------------------------------------------------------+
-- support map as object value
SELECT json_object('id', 1, 'level', map('a', 'b', 'c', 'd'));
+------------------------------------------------------------------------------------------------------+
| json_object('id', cast(1 as VARCHAR(65533)), 'level', cast(map('a', 'b', 'c', 'd') as JSON), '6267') |
+------------------------------------------------------------------------------------------------------+
| {"id":1,"level":{"a":"b","c":"d"}} |
+------------------------------------------------------------------------------------------------------+
-- support struct as object value
SELECT json_object('id', 1, 'level', named_struct('name', 'a', 'age', 1));
+------------------------------------------------------------------------------------------------------------------+
| json_object('id', cast(1 as VARCHAR(65533)), 'level', cast(named_struct('name', 'a', 'age', 1) as JSON), '6267') |
+------------------------------------------------------------------------------------------------------------------+
| {"id":1,"level":{"name":"a","age":1}} |
+------------------------------------------------------------------------------------------------------------------+
-- support json as object value
SELECT json_object('id', 1, 'level', cast('{\"a\":\"b\"}' as JSON));
+------------------------------------------------------------------------------------------+
| json_object('id', cast(1 as VARCHAR(65533)), 'level', cast('{"a":"b"}' as JSON), '6267') |
+------------------------------------------------------------------------------------------+
| {"id":1,"level":{"a":"b"}} |
+------------------------------------------------------------------------------------------+