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] …])

    描述:输入参数为交替出现的keyvalue。从一个可变参数列表构造出一个 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_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)
    
  • json_contains(target, candidate[, path])

    描述:path可选参数是target参数的path,返回target参数是否包含candidate参数。

    返回类型:bool

    备注:

    • pathtarget中不存在时,函数返回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参数可以为oneall

      one则只要一个path存在即返回true,否则返回falseall则全部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)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值