openGauss插件使用指南:函数和操作符—JSON-JSONB函数和操作符(上)
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) -
鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。
更多推荐


所有评论(0)