JSON-JSONB函数和操作符
新增json函数
-
在
public
中创建同名自定义函数或存储过程可能影响原函数功能,建议用户创建同名自定义函数或存储过程时指定模式名。 -
json_array([val[, val] …])
描述:输入可变长参数,输出一个 JSON 数组。
返回类型:array-json
示例:
opengauss=# select json_array(1,'a','b',true,null); json_array --------------------------- [1, "a", "b", true, null] (1 row)
-
json_object([key, val[, key, val] …])
描述:输入参数为交替出现的
key
、value
。从一个可变参数列表构造出一个 JSON 对象,使用前需设置GUC参数 dolphin.b_compatibility_mode = 1。返回类型:json
备注:
- 由于 JSON 对象中的所有键为字符串,因此
JSON_OBJECT()
会将不是字符串类型的key
转为字符串类型。为了保证程序的稳定性,我们一般使用字符串类型的key
。 key
不能为 NULL 且入参个数应为偶数个。
示例:
opengauss=# SET dolphin.b_compatibility_mode = 1; opengauss=# SELECT JSON_OBJECT( opengauss(# 'name', opengauss(# 'Tim', opengauss(# 'age', opengauss(# 20, opengauss(# 'friend', opengauss(# JSON_OBJECT('name', 'Jim', 'age', 20), opengauss(# 'hobby', opengauss(# JSON_BUILD_ARRAY('games', 'sports') opengauss(# ) AS object; object ------------------------------------------------------------------------------------------------------ {"age" : 20, "name" : "Tim", "hobby" : ["games", "sports"], "friend" : {"age" : 20, "name" : "Jim"}} (1 row) opengauss=# SET dolphin.b_compatibility_mode = 0; opengauss=# select json_object('{a,b,"a b c"}', '{a,1,1}'); json_object --------------------------------------- {"a" : "a", "b" : "1", "a b c" : "1"} (1 row)
- 由于 JSON 对象中的所有键为字符串,因此
-
json_quote(string)
描述:输入字符串,输出 JSON 文档,并用双引号修饰。
返回类型:json
备注:
- 在opengauss中,通过入参前加
E
来实现转译功能,转译前后与Mysql一致。 - 在opengauss中,json_quote函数支持数值型。
示例:
opengauss=# select json_quote('gauss'); json_quote ------------ "gauss" (1 row) #opengauss反斜杠非转译模式 opengauss=# select json_quote('\\t\\u0032'); json_quote ------------------ "\\\\t\\\\u0032" (1 row) #opengauss反斜杠转译模式 opengauss=# select json_quote(E'\\t\\u0032'); json_quote -------------- "\\t\\u0032" (1 row) #opengauss支持数值型 opengauss=# select json_quote(1); json_quote ------------ "1" (1 row)
- 在opengauss中,通过入参前加
-
json_contains(target, candidate[, path])
描述:
path
可选参数是target
参数的path
,返回target
参数是否包含candidate
参数。返回类型:bool
备注:
- 当
path
在target
中不存在时,函数返回NULL
。
示例:
openGauss=# select json_contains('[1,2,3,4,5]','[3,5]'); json_contains --------------- t (1 row) openGauss=# select json_contains('[1,2,3,4,5]','6'); json_contains --------------- f (1 row) openGauss=# select json_contains('{"a":[null,true,false]}','{"a":false}'); json_contains --------------- t (1 row) openGauss=# select json_contains('{"a":[1,2,3]}','3'); json_contains --------------- f (1 row) openGauss=# select json_contains('{"a":[1,2,3]}','3','$.a'); json_contains --------------- t (1 row) openGauss=# select json_contains('{"a":[1,2,3]}','3','$.b'); json_contains --------------- (1 row)
- 当
-
json_contains_path(json_doc, one_or_all, path[, path] …)
描述:返回目标
json_doc
参数是否存在输入的path
参数,one_or_all
参数选择模式。返回类型:bool
备注:
-
one_or_all
参数可以为one
或all
。one
则只要一个path
存在即返回true
,否则返回false
;all
则全部path
存在才返回true
,否则返回false
。 -
若
one_or_all
参数为one
,则按顺序检查path
,NULL 的path
先于任一存在的path
,则函数返回 NULL;若
one_or_all
参数为all
,则按顺序检查path
,NULL 的path
先于任一不存在的path
,则函数返回 NULL。
示例:
openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.a', '$.e'); json_contains_path -------------------- t (1 row) openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a', '$.b','$."c".d'); json_contains_path -------------------- t (1 row) openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": [3,4,5]}}', 'one', '$.c.d[3]'); json_contains_path -------------------- f (1 row) openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a.d'); json_contains_path -------------------- f (1 row) openGauss=# select json_contains_path('[1,2,3]',null,'$[0]'); json_contains_path -------------------- (1 row) openGauss=# select json_contains_path('[1,2,3]','one','$[0]',null,'$[1]'); json_contains_path -------------------- t (1 row) openGauss=# select json_contains_path('[1,2,3]','one','$[3]',null,'$[1]'); json_contains_path -------------------- (1 row) openGauss=# select json_contains_path('[1,2,3]','all','$[0]',null,'$[1]'); json_contains_path -------------------- (1 row) openGauss=# select json_contains_path('[1,2,3]','all','$[3]',null,'$[1]'); json_contains_path -------------------- f (1 row)
-