新增json函数

  • 在public中创建同名自定义函数或存储过程可能影响原函数功能,建议用户创建同名自定义函数或存储过程时指定模式名。
  • json_array([val[, val] …])

描述:输入可变长参数,输出一个 JSON 数组。

返回类型:array-json

示例:

``` 1. opengauss=# select json_array(1,'a','b',true,null);

  1. json_array

  2. [1, "a", "b", true, null]

  3. (1 row) ```
  4. json_object([key, val[, key, val] …])

描述:输入参数为交替出现的key、value。从一个可变参数列表构造出一个 JSON 对象,使用前需设置GUC参数 dolphin.b_compatibility_mode = 1。

返回类型:json

备注:

  • 由于 JSON 对象中的所有键为字符串,因此JSON_OBJECT()会将不是字符串类型的key转为字符串类型。为了保证程序的稳定性,我们一般使用字符串类型的key。
  • key不能为 NULL 且入参个数应为偶数个。

示例:

``` 1. opengauss=# SET dolphin.b_compatibility_mode = 1; 2. opengauss=# SELECT JSON_OBJECT( 3. opengauss(# 'name', 4. opengauss(# 'Tim', 5. opengauss(# 'age', 6. opengauss(# 20, 7. opengauss(# 'friend', 8. opengauss(# JSON_OBJECT('name', 'Jim', 'age', 20), 9. opengauss(# 'hobby', 10. opengauss(# JSON_BUILD_ARRAY('games', 'sports') 11. opengauss(# ) AS object;

  1. object

  2. {"age" : 20, "name" : "Tim", "hobby" : ["games", "sports"], "friend" : {"age" : 20, "name" : "Jim"}}

  3. (1 row)

  4. opengauss=# SET dolphin.b_compatibility_mode = 0;

  5. opengauss=# select json_object('{a,b,"a b c"}', '{a,1,1}');
  6. json_object

  7. {"a" : "a", "b" : "1", "a b c" : "1"}
  8. (1 row) ```
  9. json_quote(string)

描述:输入字符串,输出 JSON 文档,并用双引号修饰。

返回类型:json

备注:

  • 在opengauss中,通过入参前加E来实现转译功能,转译前后与Mysql一致。
  • 在opengauss中,json_quote函数支持数值型。

示例:

``` 1. opengauss=# select json_quote('gauss'); 2. json_quote 3. ------------ 4. "gauss" 5. (1 row)

  1. opengauss反斜杠非转译模式

  2. opengauss=# select json_quote('\t\u0032');
  3. json_quote

  4. "\\t\\u0032"
  5. (1 row)

  6. opengauss反斜杠转译模式

  7. opengauss=# select json_quote(E'\t\u0032');
  8. json_quote

  9. "\t\u0032"
  10. (1 row)

  11. opengauss支持数值型

  12. opengauss=# select json_quote(1);
  13. json_quote

  14. "1"
  15. (1 row) ```
  16. json_contains(target, candidate[, path])

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

返回类型:bool

备注:

  • 当path在target中不存在时,函数返回NULL。

示例:

``` 1. openGauss=# select json_contains('[1,2,3,4,5]','[3,5]'); 2. json_contains 3. --------------- 4. t 5. (1 row)

  1. openGauss=# select json_contains('[1,2,3,4,5]','6');
  2. json_contains

  3. f
  4. (1 row)

  5. openGauss=# select json_contains('{"a":[null,true,false]}','{"a":false}');

  6. json_contains

  7. t
  8. (1 row)

  9. openGauss=# select json_contains('{"a":[1,2,3]}','3');

  10. json_contains

  11. f
  12. (1 row)

  13. openGauss=# select json_contains('{"a":[1,2,3]}','3','$.a');

  14. json_contains

  15. t
  16. (1 row)

  17. openGauss=# select json_contains('{"a":[1,2,3]}','3','$.b');

  18. json_contains

  19. (1 row) ```

  20. 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。

示例:

``` 1. openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.a', '$.e'); 2. json_contains_path 3. -------------------- 4. t 5. (1 row)

  1. openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a', '$.b','$."c".d');
  2. json_contains_path

  3. t
  4. (1 row)

  5. openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": [3,4,5]}}', 'one', '$.c.d[3]');

  6. json_contains_path

  7. f
  8. (1 row)

  9. openGauss=# select json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a.d');

  10. json_contains_path

  11. f
  12. (1 row)

  13. openGauss=# select json_contains_path('[1,2,3]',null,'$[0]');

  14. json_contains_path

  15. (1 row)

  16. openGauss=# select json_contains_path('[1,2,3]','one','$[0]',null,'$[1]');

  17. json_contains_path

  18. t
  19. (1 row)

  20. openGauss=# select json_contains_path('[1,2,3]','one','$[3]',null,'$[1]');

  21. json_contains_path

  22. (1 row)

  23. openGauss=# select json_contains_path('[1,2,3]','all','$[0]',null,'$[1]');

  24. json_contains_path

  25. (1 row)

  26. openGauss=# select json_contains_path('[1,2,3]','all','$[3]',null,'$[1]');

  27. json_contains_path

  28. f
  29. (1 row) ```
  30. json_extract(json_doc, path[, path] …)

描述:在 JSON 文档提取路径表达式指定的数据并返回。

返回类型:json

示例:

1. opengauss=# select json_extract('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}', '$.f4.f6'); 2. json_extract 3. -------------- 4. "stringy" 5. (1 row) - json_unquote(json_val)

描述:去除文本中的引号,对转义符有所处理,或者舍弃 JSON 值中的引号。

返回类型:text

示例:

1. opengauss=# select json_unquote('"dajifa\\tIMIDF"'); 2. json_unquote 3. --------------- 4. dajifa IMIDF 5. (1 row) - json_unquote(json_extract(column, path))

描述:去除文本中的引号,对转义符有所处理,或者舍弃 JSON 值中的引号。

返回类型:text

示例:

1. opengauss=# select json_unquote(json_extract('{"a": "lihua"}', '$.a')); 2. json_unquote 3. -------------- 4. lihua 5. (1 row) - json_keys(json_doc[, path])

描述:将 JSON 对象的顶级值中的键作为 JSON 数组返回,如果给定了路径参数,则返回路径所指示 JSON 对象的顶级键。

返回类型:json

示例:

``` 1. opengauss=# SELECT JSON_KEYS('{"a":123,"b":{"c":"qwe"}}');

  1. json_keys

  2. ["a","b"]

  3. (1 row) ```
  4. json_search(json_doc,one_or_all,search_str [,escape_char [,path] …])

描述:可传入一个或多个路径参数,根据转义符和one_or_all模式,返回目标字符串在路径限定下对应目标文件中的所在位置。

返回类型:text

备注:

  • 转义符如果为boolean型相当于 NULL,默认"\"为转义符,可以直接输入个位数整型作为转义符。
  • 目标json文件和目标字符串不能为空,路径不能含空,如果path不存在则返回空。
  • one_or_all只能输入one或all。
  • search_str可以直接输入整型、浮点型、boolean型进行匹配,但是只能匹配目标文件中的字符串。
  • search_str可以使用模糊匹配,path中可以使用通配符进行匹配。

示例:

``` 1. opengauss=# select json_search('"abc"','one','abc',true); 2. json_search 3. ------------- 4. "$" 5. (1 row)

  1. opengauss=# select json_search('"a%c"','all','a1%c',1);
  2. json_search

  3. "$"
  4. (1 row)

  5. opengauss=# select json_search('"abc"','one','abc','&','$',null);

  6. json_search

  7. (1 row)

  8. opengauss=# select json_search('"1.2"','one',1.2);

  9. json_search

  10. "$"
  11. (1 row)

  12. opengauss=# select json_search('{"a":[{"b":["abc","abc"]},"ac"],"c":["abbc","abcc"]}','all','a%c',null,'$.[]');

  13. json_search

  14. ["$.a[0].b[0]", "$.a[0].b[1]", "$.a[1]", "$.c[0]", "$.c[1]"]
  15. (1 row) ```
  16. json_array_append(json, path, value[, path2, value2] …)

描述:用来修改 JSON 文档,它向指定的数组节点中追加一个元素,并返回修改后的 JSON 文档。

返回类型:json

``` 1. opengauss=# select JSON_ARRAY_APPEND('{"name": "Tim", "hobby": "car"}', '$.name', 'food');

  1. json_array_append

  2. {"name": ["Tim", "food"], "hobby": "car"}

  3. (1 row) ```
  4. json_append(json_doc, path, val[, path, val] …) 描述:功能同json_array_append函数。

返回类型:json

备注: json_append() 函数可能在将来的版本中被删除,推荐使用json_array_append函数。 示例:

``` 1. opengauss=# select JSON_ARRAY_APPEND('{"name": "Tim", "hobby": "car"}', '$.name', 'food');

  1. json_array_append

  2. {"name": ["Tim", "food"], "hobby": "car"}

  3. (1 row) ```
  4. json_array_insert(json, path, value[, path2, value2] …)

描述:函数用来修改 JSON 文档,它向 JSON 文档中的指定的数组中的指定位置插入一个值并返回新的 JSON 文档。

返回类型:json

备注:

  • 如果路径表达式指示的数组元素超过了数组的长度,那么新元素将插入到数组的尾部。
  • 如果 JSON 文档或者路径为 NULL,此函数将返回 NULL。

示例:

``` 1. opengauss=# select json_array_insert('[1, [2, 3], {"a": [4, 5]}]', '$[0]', 0); 2. json_array_insert 3. ------------------------------- 4. [0, 1, [2, 3], {"a": [4, 5]}] 5. (1 row)

  1. opengauss=# select json_array_insert('[1, [2, 3], {"a": [4, 5]}]', '$[9]', 4);
  2. json_array_insert

  3. [1, [2, 3], {"a": [4, 5]}, 4]
  4. (1 row) ```
  5. json_insert(json_doc, path, val[, path, val] …)

描述:向一个 JSON 文档中插入数据并返回新的 JSON 文档。

返回类型:json

备注:当 JSON 文档或path为空时,返回空。

示例:

1. opengauss=# select json_insert('{"x": 1}','$.y',true); 2. json_insert 3. --------------------- 4. {"x": 1, "y": true} 5. (1 row) - json_merge(json_doc, json_doc[, json_doc] …)

描述:功能同json_merge_preserve函数。

返回类型:json

备注: JSON_MERGE() 函数可能在将来的版本中被删除,推荐使用json_merge_preserve函数。

示例:

1. openGauss=# select json_merge('"opengauss"', '[[1,2],3,"test"]'); 2. json_merge 3. ---------------------------------- 4. ["opengauss", [1, 2], 3, "test"] 5. (1 row) - json_merge_preserve(json_doc, json_doc[, json_doc] …)

描述:合并两个及以上的 JSON,相同键值合并为一个数组。

返回类型:json

备注:

  • 如果任何参数为NULL则返回NULL。
  • 合并规范:
    • 若相邻的两个 JSON 参数一个为scalar或对象,一个为数组。将scalar或对象,作为数组元素,按照参数的先后顺序,加入数组参数中,合并为单个数组。
    • 若相邻的两个 JSON 参数都是scalar或对象。将scalar或对象按照参数的先后顺序,合并为单个数组。
    • 若相邻的两个 JSON 参数都是数组。将两个数组的各个元素,按照参数的先后顺序,合并为单个数组。
    • 若相邻的两个 JSON 参数都是对象。将两个对象的各个成员,按照key的顺序,合并为单个对象。
    • 合并 JSON 后的对象成员返回值,全部符合key的顺序。

示例:

``` 1. openGauss=# select json_merge_preserve('{"a":"abc"}', '[1,true,null]'); 2. json_merge_preserve 3. ------------------------------- 4. [{"a": "abc"}, 1, true, null] 5. (1 row)

  1. openGauss=# select json_merge_preserve('1', '"b"', 'true');
  2. json_merge_preserve

  3. [1, "b", true]
  4. (1 row)

  5. openGauss=# select json_merge_preserve('[1,{"a":"abc"}]', '["b",false]');

  6. json_merge_preserve

  7. [1, {"a": "abc"}, "b", false]
  8. (1 row)

  9. openGauss=# select json_merge_preserve('{"b":"abc"}', '{"a":"jks"}');

  10. json_merge_preserve

  11. {"a": "jks", "b": "abc"}
  12. (1 row)

  13. openGauss=# select json_merge_preserve(NULL, '1');

  14. json_merge_preserve

  15. (1 row) ```

  16. json_merge_patch(json_doc, json_doc[, json_doc] …)

描述:合并两个及以上的 JSON ,相同键值保留后者 JSON 对象键值成员。

返回类型:json

备注:

  • 若任一参数为NULL,则之前的参数和该参数的合并结果为 NULL。
  • NULL 参数后面一个参数若非 NULL,则:
    • 后面参数为数组、scalar,合并结果为后面参数本身。
    • 后面参数为对象,合并结果为 NULL。
  • 合并规范:
    • 若相邻的两个 JSON 参数都是对象,则合并结果为单个对象。
    • 若一个 JSON 对象的某一成员键在另一个 JSON 对象中没有重复,则在合并结果中保留该成员。
    • 若前一个 JSON 对象的某一成员键在后一个 JSON 对象中重复,则在合并结果中,保留后者 JSON 对象中重复键成员。特别地,当后一个相同键对应对象成员的value为 NULL 时,在结果中删除该键成员。
    • 若相邻的两个 JSON 参数存在一个参数不是对象,则合并的结果直接为第二个 JSON 参数。
    • 若任一参数为NULL,则位于该参数之前的参数和该参数的合并结果为 NULL
    • NULL 参数后面一个参数若非 NULL ,则:
    • 后者参数为数组或scalar,合并结果为后面参数本身。
    • 后者参数为对象,合并结果为 NULL 。

示例:

``` 1. openGauss=# select json_merge_patch('{"a":1}', '{"b":2}'); 2. json_merge_patch 3. ------------------ 4. {"a": 1, "b": 2} 5. (1 row)

  1. openGauss=# select json_merge_patch('{"a":1}', '{"a":2}');
  2. json_merge_patch

  3. {"a": 2}
  4. (1 row)

  5. openGauss=# select json_merge_patch('{"a":{"b":"abc"}}', '{"a":{"b":null}}');

  6. json_merge_patch

  7. {"a": {}}
  8. (1 row)

  9. openGauss=# select json_merge_patch('{"a":1}', 'true');

  10. json_merge_patch

  11. true
  12. (1 row)

  13. openGauss=# select json_merge_patch('{"a":1}', NULL);

  14. json_merge_patch

  15. (1 row)

  16. openGauss=# select json_merge_patch(NULL, '{"a":1}');

  17. json_merge_patch

  18. (1 row)

  19. openGauss=# select json_merge_patch(NULL, '[1,2,3]');

  20. json_merge_patch

  21. [1, 2, 3]
  22. (1 row) ```
  23. json_remove(json, path[, path] …)

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

返回类型:json

备注:

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

示例:

``` 1. opengauss=# SELECT JSON_REMOVE('[0, 1, 2, [3, 4]]', '$[0]', '$[2]'); 2. json_remove 3. ------------- 4. [1, 2] 5. (1 row) 6. opengauss=# SELECT JSON_REMOVE('{"x": 1, "y": 2}', '$.x'); 7. json_remove 8. ------------- 9. {"y": 2} 10. (1 row) 11. opengauss=# SELECT JSON_REMOVE('{"x": {"z":2,"a":3}, "y": 2}', NULL); 12. json_remove 13. -------------

  1. (1 row)
  2. opengauss=# SELECT JSON_REMOVE(NULL, '$.x.z');
  3. json_remove

  4. (1 row) ```

  5. json_replace(json_doc, path, val[, path, val] …)

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

返回类型:json

示例:

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

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

返回类型:json

示例:

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

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

返回类型:integer

备注:

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

示例:

1. openGauss=# SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true'); 2. json_depth | json_depth | json_depth 3. ------------+------------+------------ 4. 1 | 1 | 1 5. (1 row) 6. openGauss=# SELECT JSON_DEPTH('[10, 20]'), JSON_DEPTH('[[], {}]'); 7. json_depth | json_depth 8. ------------+------------ 9. 2 | 2 10. (1 row) 11. openGauss=# SELECT JSON_DEPTH('[10, {"a": 20}]'); 12. json_depth 13. ------------ 14. 3 15. (1 row) - json_length(json_doc[, path])

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

返回类型:integer

备注:

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

示例:

``` 1. opengauss=# select json_length('null'); 2. json_length 3. ------------- 4. 1 5. (1 row)

  1. opengauss=# select json_length('{}');
  2. json_length

  3. 0
  4. (1 row)

  5. opengauss=# select json_length('{"a":1,"b":2,"c":3,"d":4}');

  6. json_length

  7. 4
  8. (1 row)

  9. opengauss=# select json_length('{"a":"abc","b":"abc"}','$.a');

  10. json_length

  11. 1
  12. (1 row) ```
  13. json_type(json_val)

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

返回类型:text

示例:

``` 1. opengauss=# select json_type('"aa"'); 2. json_type 3. ----------- 4. STRING 5. (1 row)

  1. opengauss=# select json_type('null');
  2. json_type

  3. NULL
  4. (1 row)

  5. opengauss=# select json_type('[1,2]');

  6. json_type

  7. ARRAY
  8. (1 row)

  9. opengauss=# select json_type('{"w":1}');

  10. json_type

  11. OBJECT
  12. (1 row)

  13. opengauss=# select json_type('11');

  14. json_type

  15. INTEGER
  16. (1 row) ```
  17. json_valid(val)

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

返回类型:bool

备注:

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

示例:

``` 1. openGauss=# select json_valid('{"a":[1,2,3]}'); 2. json_valid 3. ------------ 4. t 5. (1 row)

  1. openGauss=# select json_valid('{"a":[1,2,3]}');
  2. json_valid

  3. t
  4. (1 row)

  5. openGauss=# select json_valid('{"a":[[1,2,3]}');

  6. json_valid

  7. f
  8. (1 row)

  9. openGauss=# select json_valid('0.3135621312');

  10. json_valid

  11. t
  12. (1 row)

  13. openGauss=# select json_valid('03135621312');

  14. json_valid

  15. f
  16. (1 row)

  17. openGauss=# select json_valid('{"a":true}'::json);

  18. json_valid

  19. t
  20. (1 row) ```
  21. json_pretty(json)

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

返回类型:json

示例:

``` 1. opengauss=# select JSON_PRETTY('{"a": 43}'); 2. json_pretty 3. ------------- 4. { + 5. "a": 43 + 6. } 7. (1 row)

  1. opengauss=# select JSON_PRETTY('{}');
  2. json_pretty

  3. {}
  4. (1 row)

  5. opengauss=# select JSON_PRETTY('{"a":[{"age": 43, "name": "lihua"}, [[[[43,33, []]]]], "hello"]}');

  6. json_pretty

  7. { +
  8. "a": [ +
  9. { +
  10. "age": 43, +
  11. "name": "lihua"+
  12. }, +
  13. [ +
  14. [ +
  15. [ +
  16. [ +
  17. 43, +
  18. 33, +
  19. [] +
  20. ] +
  21. ] +
  22. ] +
  23. ], +
  24. "hello" +
  25. ] +
  26. }
  27. (1 row) ```
  28. json_storage_size(json)

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

返回类型:integer

备注:

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

示例:

``` 1. opengauss=# SELECT JSON_STORAGE_SIZE('0'); 2. json_storage_size 3. ------------------- 4. 2 5. (1 row)

  1. opengauss=# SELECT JSON_STORAGE_SIZE('"Hello World"');
  2. json_storage_size

  3. 14
  4. (1 row)

  5. opengauss=# SELECT JSON_STORAGE_SIZE('[1, "abc", null, true, "10:27:06.000000", {"id": 1}]');

  6. json_storage_size

  7. 53
  8. (1 row)

  9. opengauss=# SELECT JSON_STORAGE_SIZE('{"x": 1, "y": 2}');

  10. json_storage_size

  11. 17
  12. (1 row) ```
  13. json_arrayagg(col_or_expr)

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

备注:

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

示例:

``` 1. opengauss=# CREATE TEMP TABLE foo1 (serial_num int, name text, type text); 2. opengauss=# INSERT INTO foo1 VALUES (847001,'t15','GE1043'); 3. opengauss=# INSERT INTO foo1 VALUES (847002,'t16','GE1043'); 4. opengauss=# INSERT INTO foo1 VALUES (847003,'sub-alpha','GESS90'); 5. opengauss=# SELECT json_arrayagg(serial_num) 6. FROM foo1; 7. json_arrayagg 8. -------------------------- 9. [847001, 847002, 847003] 10. (1 row)

  1. opengauss=# SELECT json_arrayagg(type)
  2. FROM foo1;
  3. json_arrayagg

  4. ["GE1043", "GE1043", "GESS90"]
  5. (1 row) ```
  6. json_objectagg(key, value)

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

返回类型:object-json

备注:

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

示例:

``` 1. openGauss=# select * from City; 2. district | name | population 3. -----------------+---------------+------------ 4. Capital Region | Canberra | 322723 5. New South Wales | Sydney | 3276207 6. New South Wales | Newcastle | 270324 7. New South Wales | Central Coast | 227657 8. New South Wales | Wollongong | 219761 9. Queensland | Brisbane | 1291117 10. Queensland | Gold Coast | 311932 11. Queensland | Townsville | 109914 12. Queensland | Cairns | 92273 13. South Australia | Adelaide | 978100 14. Tasmania | Hobart | 126118 15. Victoria | Melbourne | 2865329 16. Victoria | Geelong | 125382 17. West Australia | Perth | 1096829 18. (14 rows)

  1. openGauss=# SELECT
  2. openGauss-# District AS State,
  3. openGauss-# JSON_OBJECTAGG(Name, Population) AS "City/Population"
  4. openGauss-# FROM City
  5. openGauss-# GROUP BY State;
  6. state | City/Population
  7. -----------------+-----------------------------------------------------------------------------------------
  8. West Australia | {"Perth": 1096829}
  9. Queensland | {"Cairns": 92273, "Brisbane": 1291117, "Gold Coast": 311932, "Townsville": 109914}
  10. New South Wales | {"Sydney": 3276207, "Newcastle": 270324, "Wollongong": 219761, "Central Coast": 227657}
  11. Tasmania | {"Hobart": 126118}
  12. Victoria | {"Geelong": 125382, "Melbourne": 2865329}
  13. South Australia | {"Adelaide": 978100}
  14. Capital Region | {"Canberra": 322723}
  15. (7 rows) ```
  16. column->path

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

返回类型: json

示例:

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

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

返回类型: text

示例:

1. opengauss=# create table test(data json); 2. CREATE TABLE 3. opengauss=# insert into test values('{"a":"lihua"}'); 4. INSERT 0 1 5. opengauss=# select data->>'$.a' from test; 6. ?column? 7. ---------- 8. lihua 9. (1 row)