函数 | 返回值 | 描述 | 用法示例 | 示例结果 |
json_array_length(json)
jsonb_array_length(jsonb)
| int
| 返回最外层json 数组中的元素个数。 | json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')
| 5
|
json_each(json)
jsonb_each(jsonb)
| set of key text, value json
set of key text, value jsonb
| 将最外层json 对象展开为一组键值对。 | SELECT * FROM json_each('{"a":"foo", "b":"bar"}')
| key | value
-----+-------
a | "foo"
b | "bar"
|
json_each_text(json)
jsonb_each_text(jsonb)
| set of key text , value text
| 将最外层json 对象展开为一组键值对。返回值是text类型。 | SELECT * FROM json_each_text('{"a":"foo", "b":"bar"}')
| key | value
-----+-------
a | foo
b | bar
|
json_extract_path(from_json json, VARIADIC path_elems text[])
jsonb_extract_path(from_json jsonb, VARIADIC path_elems text[])
| json
jsonb
| 返回由path_elems 指向的json 值(效果同#> 操作符)。 | json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')
| {"f5":99,"f6":"foo"}
|
json_extract_path_text(from_json json, VARIADIC path_elems text[])
jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[])
| text
| 以text 类型返回由path_elems 指向的JSON值(效果同#>> 操作符)。 | json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')
| foo
|
json_object_keys(json)
jsonb_object_keys(jsonb)
| set of text
| 返回最外层json 对象中的键集合。 | json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')
| json_object_keys
------------------
f1
f2
|
json_populate_record(base any element, from_json json)
jsonb_populate_record(base any element, from_json jsonb)
| any element
| 将from_json 中的对象展开为一行,它的列匹配是由base 定义的记录类型。 | SELECT * FROM json_populate_record(null::myrowtype, '{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "a b c"}}')
| a | b | c
---+-----------+-------------
1 | {2,"a b"} | (4,"a b c")
|
json_populate_recordset(base any element, from_json json)
jsonb_populate_recordset(base any element, from_json jsonb)
| set of any element
| 将from_json 中最外的对象数组展开为一个集合,该集合的列匹配由base 定义的记录类型。 | SELECT * FROM json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]')
| a | b
---+---
1 | 2
3 | 4
|
json_array_elements(json)
jsonb_array_elements(jsonb)
| set of json
set of jsonb
| 将一个json 数组展开为一个json 值的集合。 | SELECT * FROM json_array_elements('[1,true, [2,false]]')
| value
-----------
1
true
[2,false]
|
json_array_elements_text(json)
jsonb_array_elements_text(jsonb)
| set of text
| 将一个json 数组展开为一个text 值集合。 | SELECT * FROM json_array_elements_text('["foo", "bar"]')
| value
-----------
foo
bar
|
json_typeof(json)
jsonb_typeof(jsonb)
| text
| 将最外层的json 值的类型作为一个文本字符串返回。可能的类型:object 、array 、string 、number 、boolean 及null 。 | json_typeof('-123.4')
| number
|
json_to_record(json)
jsonb_to_record(jsonb)
| record
| 由一个json 对象构建任意一条记录。正如所有返回record 的函数一样,用户必须用一个AS 子句显式地定义记录的结构。 | SELECT * FROM json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}')
as x(a int, b text, c text)
| a | b | c |
---+---------+-----+
1 | [1,2,3] | bar |
|
json_to_recordset(json)
jsonb_to_recordset(jsonb)
| set of record
| 由一个json 对象数组构建任意一条记录集合。正如所有返回record 的函数一样,用户必须用一个AS 子句显式地定义记录的结构。 | SELECT * FROM json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text);
| a | b
---+-----
1 | foo
2 |
|
json_strip_nulls(from_json json)
jsonb_strip_nulls(from_json jsonb)
| json
jsonb
| 返回from_json ,其中所有存在空值的对象域都被省略,其他空值除外。 | json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')
| [{"f1":1},2,null,3]
|
jsonb_set(jsonb, path text[], new_value jsonb[,create_missing boolean])
| jsonb
| 返回jsonb ,其中由path 指定的项用new_value 替换。若path 指定的项不存在并且create_missing 为true (默认为true )则加上new_value 。正如面向路径的操作符一样,出现在path 中的负整数表示从json 数组的末尾开始数。 | jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')
| [{"f1":[2,3,4],"f2":null},2,null,3]
[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]
|
jsonb_insert(target jsonb, path text[], new_value jsonb [, insert_after boolean])
| jsonb
| 返回被插入了new_value 的jsonb 。如果path 指定的jsonb 在一个jsonb 数组中,new_value 将被插入到目标之前(当insert_after 默认为false )或之后(当insert_after 为true )。如果path 指定的target 在一个jsonb 对象内,则只有target 不存在时才插入new_value 。对于面向路径的操作符来说,出现在path 中的负整数表示从JSON数组的末尾开始数。 | jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"')
jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true)
| {"a": [0, "new_value", 1, 2]}
{"a": [0, 1, "new_value", 2]}
|
jsonb_pretty(from_json jsonb)
| text
| 将from_json 返回为一段缩进后的json 文本。 | jsonb_pretty('[{"f1":1,"f2":null},2,null,3]' )
| [
{
"f1": 1,
"f2": null
},
2,
null,
3
]
|
jsonb_path_exists(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| boolean
| 检查json 路径是否为指定的json 值返回任意项。 | jsonb_path_exists('{"a":[1,2,3,4,5]}', '$. a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| true
|
jsonb_path_match(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| boolean
| 返回指定的json 路径检查结果。 只考虑结果的首项。 如果结果非布尔值,则返回null 。 | jsonb_path_match('{"a":[1,2,3,4,5]}', 'exists($.a[*] ? (@ >= $min && @ <= $max))', '{"min":2,"max":4}')
| true
|
jsonb_path_query(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| set of jsonb
| 获取指定的json 值的json 路径返回的全部json 项。 | SELECT * FROM jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}');
| jsonb_path_query
------------------
2
3
4
|
jsonb_path_query_array(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| jsonb
| 获取指定json 路径返回的全部json 项,并将结果封装为数组。 | jsonb_path_query_array('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| [2, 3, 4]
|
jsonb_path_query_first(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| jsonb
| 获取指定的json 值的第一个json 路径返回的json 项。 如果无结果,则返回NULL 。 | jsonb_path_query_first('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| 2
|