• json_remove(json, path[, path] …)

    描述:从一个 JSON 文档中删除由路径指定的 JSON 对象并返回修改后的 JSON 文档。

    返回类型:json

    备注:

    • 可以通过参数提供多个路径表达式以供删除。多个路径参数会从左到右依次被执行。当执行下一个参数的时候,JSON 文档可能已经发生了变化。
    • 如果 JSON 中不存在指定的路径,此函数返回原文档。
    • 如果 JSON 文档或者路径为 NULL,此函数将返回 NULL。

    示例:

    opengauss=# SELECT JSON_REMOVE('[0, 1, 2, [3, 4]]', '$[0]', '$[2]');
     json_remove 
    -------------
     [1, 2]
    (1 row)
    opengauss=# SELECT JSON_REMOVE('{"x": 1, "y": 2}', '$.x');
     json_remove 
    -------------
     {"y": 2}
    (1 row)
    opengauss=# SELECT JSON_REMOVE('{"x": {"z":2,"a":3}, "y": 2}', NULL);
     json_remove 
    -------------
    
    (1 row)
    opengauss=# SELECT JSON_REMOVE(NULL, '$.x.z');
     json_remove 
    -------------
    
    (1 row)
    
  • json_replace(json_doc, path, val[, path, val] …)

    描述:在一个 JSON 文档中替换已存在的数据并返回新的 JSON 文档。第一个参数为 JSON 文档,其后为交替出现的路径和替换值。

    返回类型:json

    示例:

    openGauss=# select json_replace('{"a": 1, "b": 2, "c": 3}', '$.b', 9);
      json_replace
    -------------------
     {"a": 1, "b": 9, "c": 3}
    (1 row)
    
  • json_set(json_doc, path, val[, path, val] …))

    描述:输入 JSON 文档,路径和键值,替换 JSON 文档中已有路径对应的键值,对于新增路径,插入对应键值。

    返回类型:json

    示例:

    opengauss=# select json_set('{"student":{"id":1,"gender":"man"}}','$.age',23,'$.student.id',3);
                       json_set                   
    ----------------------------------------------
     {"age":23,"student":{"id":3,"gender":"man"}}
    (1 row)
    
  • json_depth(json)

    描述:返回 JSON 文档的最大深度。

    返回类型:integer

    备注:

    • 空数组、空对象或标量值的深度为1。
    • 仅包含深度为1的数组或对象深度为2。
    • JSON 节点的最大深度等于其所有子节点最大深度的最大值。

    示例:

    openGauss=# SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true');
     json_depth | json_depth | json_depth 
    ------------+------------+------------
              1 |          1 |          1
    (1 row)
    openGauss=# SELECT JSON_DEPTH('[10, 20]'), JSON_DEPTH('[[], {}]');
     json_depth | json_depth 
    ------------+------------
              2 |          2
    (1 row)
    openGauss=# SELECT JSON_DEPTH('[10, {"a": 20}]');
     json_depth 
    ------------
              3
    (1 row)
    
  • json_length(json_doc[, path])

    描述:输出JSON长度,如果有路径,则输出该路径对应文档的长度。

    返回类型:integer

    备注:

    • 路径不能含有通配符*,并且只能有一个路径。

    示例:

    opengauss=# select json_length('null');
     json_length 
    -------------
               1
    (1 row)
    
    opengauss=# select json_length('{}');
     json_length 
    -------------
               0
    (1 row)
    
    opengauss=# select json_length('{"a":1,"b":2,"c":3,"d":4}');
     json_length 
    -------------
               4
    (1 row)
    
    opengauss=# select json_length('{"a":"abc","b":"abc"}','$.a');
     json_length 
    -------------
               1
    (1 row)
    
  • json_type(json_val)

    描述:输入为JSON文档,返回数据类型。

    返回类型:text

    示例:

    opengauss=# select json_type('"aa"');
     json_type 
    -----------
     STRING
    (1 row)
    
    opengauss=# select json_type('null');
     json_type 
    -----------
     NULL
    (1 row)
    
    opengauss=# select json_type('[1,2]');
     json_type 
    -----------
     ARRAY
    (1 row)
    
    opengauss=# select json_type('{"w":1}');
     json_type 
    -----------
     OBJECT
    (1 row)
    
    opengauss=# select json_type('11');
     json_type 
    -----------
     INTEGER
    (1 row)
    
  • json_valid(val)

    描述:判断输入文本是否是合法的 JSON 。

    返回类型:bool

    备注:

    • 若输入的val参数是 JSON 类型,该函数返回 true
    • 若所输入字符串需要转义,在单引号前加E,字符串转义语法是(E'...')。

    示例:

    openGauss=# select json_valid('{"a":[1,2,3]}');
     json_valid 
    ------------
     t
    (1 row)
    
    openGauss=# select json_valid('{"a":[1,2,3]}');
     json_valid 
    ------------
     t
    (1 row)
    
    openGauss=# select json_valid('{"a":[[1,2,3]}');
     json_valid 
    ------------
     f
    (1 row)
    
    openGauss=# select json_valid('0.3135621312');
     json_valid 
    ------------
     t
    (1 row)
    
    openGauss=# select json_valid('03135621312');
     json_valid 
    ------------
     f
    (1 row)
    
    openGauss=# select json_valid('{"a":true}'::json);
     json_valid 
    ------------
     t
    (1 row)
    
  • json_pretty(json)

    描述:格式化输出一个 JSON 文档,以便更易于阅读。

    返回类型:json

    示例:

    opengauss=# select JSON_PRETTY('{"a": 43}');
     json_pretty 
    -------------
       {        +
       "a": 43  +
     }
    (1 row)
    
    opengauss=# select JSON_PRETTY('{}');
     json_pretty 
    -------------
       {}
    (1 row)
    
    opengauss=# select JSON_PRETTY('{"a":[{"age": 43, "name": "lihua"}, [[[[43,33, []]]]], "hello"]}');
          json_pretty      
    -----------------------
       {                  +
       "a": [             +
         {                +
           "age": 43,     +
           "name": "lihua"+
         },               +
         [                +
           [              +
             [            +
               [          +
                 43,      +
                 33,      +
                 []       +
               ]          +
             ]            +
           ]              +
         ],               +
         "hello"          +
       ]                  +
     }
    (1 row)
    
  • json_storage_size(json)

    描述:JSON_STORAGE_SIZE() 函数返回存储一个 JSON 文档的二进制表示所占用的字节数。

    返回类型:integer

    备注:

    • json是必需的。一个 JSON 文档。它可以是一个 JSON 字符串,或者一个 JSON 列。根据实际存储方式的差异,调用opengauss内部函数计算json在opengauss中的具体存储大小,其结果与mysql不同。

    示例:

    opengauss=# SELECT JSON_STORAGE_SIZE('0');
     json_storage_size 
    -------------------
                     2
    (1 row)
    
    opengauss=# SELECT JSON_STORAGE_SIZE('"Hello World"');
     json_storage_size 
    -------------------
                    14
    (1 row)
    
    opengauss=# SELECT JSON_STORAGE_SIZE('[1, "abc", null, true, "10:27:06.000000", {"id": 1}]');
     json_storage_size 
    -------------------
                    53
    (1 row)
    
    opengauss=# SELECT JSON_STORAGE_SIZE('{"x": 1, "y": 2}');
     json_storage_size 
    -------------------
                    17
    (1 row)
    
  • json_arrayagg(col_or_expr)

    描述:json_arrayagg函数返回一个 JSON_ARRAY型数组,它将指定列中的值聚合。

    备注:

    • 如果结果集没有任何行,此函数将返回 NULL。

    示例:

    opengauss=# CREATE TEMP TABLE foo1 (serial_num int, name text, type text);
    opengauss=# INSERT INTO foo1 VALUES (847001,'t15','GE1043');
    opengauss=# INSERT INTO foo1 VALUES (847002,'t16','GE1043');
    opengauss=# INSERT INTO foo1 VALUES (847003,'sub-alpha','GESS90');
    opengauss=# SELECT json_arrayagg(serial_num)
    FROM foo1;
          json_arrayagg       
    --------------------------
     [847001, 847002, 847003]
    (1 row)
    
    opengauss=# SELECT json_arrayagg(type)
    FROM foo1;
             json_arrayagg          
    --------------------------------
     ["GE1043", "GE1043", "GESS90"]
    (1 row)
    
  • json_objectagg(key, value)

    描述:将由第一个参数作为键和第二个参数作为值的键值对聚合为一个 JSON 对象。

    返回类型:object-json

    备注:

    • 如果结果集没有任何行,此函数将返回 NULL。

    示例:

    openGauss=# select * from City;
        district     |     name      | population 
    -----------------+---------------+------------
     Capital Region  | Canberra      |     322723
     New South Wales | Sydney        |    3276207
     New South Wales | Newcastle     |     270324
     New South Wales | Central Coast |     227657
     New South Wales | Wollongong    |     219761
     Queensland      | Brisbane      |    1291117
     Queensland      | Gold Coast    |     311932
     Queensland      | Townsville    |     109914
     Queensland      | Cairns        |      92273
     South Australia | Adelaide      |     978100
     Tasmania        | Hobart        |     126118
     Victoria        | Melbourne     |    2865329
     Victoria        | Geelong       |     125382
     West Australia  | Perth         |    1096829
    (14 rows)
    
    openGauss=# SELECT 
    openGauss-#   District AS State,
    openGauss-#   JSON_OBJECTAGG(Name, Population) AS "City/Population"
    openGauss-# FROM City
    openGauss-# GROUP BY State;
          state      |                                     City/Population                                     
    -----------------+-----------------------------------------------------------------------------------------
     West Australia  | {"Perth": 1096829}
     Queensland      | {"Cairns": 92273, "Brisbane": 1291117, "Gold Coast": 311932, "Townsville": 109914}
     New South Wales | {"Sydney": 3276207, "Newcastle": 270324, "Wollongong": 219761, "Central Coast": 227657}
     Tasmania        | {"Hobart": 126118}
     Victoria        | {"Geelong": 125382, "Melbourne": 2865329}
     South Australia | {"Adelaide": 978100}
     Capital Region  | {"Canberra": 322723}
    (7 rows)
    
  • column->path

    描述:相当于json_extract的别名,在JSON文档提取路径表达式指定的数据并返回,操作符->要在查表操作中进行。

    返回类型: json

    示例:

    opengauss=# create table test(data json);
    CREATE TABLE
    opengauss=# insert into test values('{"a":"lihua"}');
    INSERT 0 1
    opengauss=# select data->'$.a' from test;
     ?column? 
    ----------
     "lihua"
    (1 row)
    
  • column-»path

    描述:功能类似于json_unquote(json_extract(json,path))json_unquote(column->path),取消对JSON文档中提取的数据引号的引用,操作符->>要在查表操作中进行。

    返回类型: text

    示例:

    opengauss=# create table test(data json);
    CREATE TABLE
    opengauss=# insert into test values('{"a":"lihua"}');
    INSERT 0 1
    opengauss=# select data->>'$.a' from test;
     ?column? 
    ----------
      lihua
    (1 row)
Logo

鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。

更多推荐