选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2
历史版本PostgreSQL 9.3 已于 2018 年 11 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

9.15. JSON 函数和操作符 #

表 9.40 展示可用于 JSON(见第 8.17 节)数据的操作符。

表 9.40. JSON 操作符

操作符 右操作数类型 描述 示例
-> int 获得 JSON 数组元素 '[1,2,3]'::json->2
-> text 获得 JSON 对象域 '{"a":1,"b":2}'::json->'b'
->> int 获得 JSON 数组元素 as text '[1,2,3]'::json->>2
->> text 获得 JSON 对象域 as text '{"a":1,"b":2}'::json->>'b'
#> array of text 获得指定路径上的 JSON 对象 '{"a":[1,2,3],"b":[4,5,6]}'::json#>'{a,2}'
#>> array of text 获得指定路径上的 JSON 对象 as text '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

表 9.41 展示可用于创建和处理 JSON(见第 8.17 节)数据的函数。

表 9.41. JSON 支持函数

函数 返回类型 描述 示例 示例结果
array_to_json(anyarray [, pretty_bool]) json 把数组作为 JSON 返回。PostgreSQL 多维数组会变成 JSON 数组的数组。如果 pretty_bool 为真,将在第 1 维元素之间添加换行符。 array_to_json('{{1,5},{99,100}}'::int[]) [[1,5],[99,100]]
row_to_json(record [, pretty_bool]) json 把行作为 JSON 返回。如果 pretty_bool 为真,将在第 1 层元素之间添加换行符。 row_to_json(row(1,'foo')) {"f1":1,"f2":"foo"}
to_json(anyelement) json 把值作为 JSON 返回。如果数据类型不是内建的,并且存在从该类型到 json 的转换,则将使用转换函数执行转换。否则,对于数字、布尔或空值之外的任何值,都将使用其文本表示,并进行转义和加引号使其成为合法的 JSON。 to_json('Fred said "Hi."'::text) "Fred said \"Hi.\""
json_array_length(json) int 返回最外层 JSON 数组中的元素数量。 json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]') 5
json_each(json) SETOF key text, value json 把最外层 JSON 对象展开为键/值对集合。 select * from json_each('{"a":"foo", "b":"bar"}')
 key | value
-----+-------
 a   | "foo"
 b   | "bar"
 
json_each_text(from_json json) SETOF key text, value text 把最外层 JSON 对象展开为键/值对集合。 The returned value will be of type 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[]) json Returns JSON object pointed to by path_elems. 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[]) text Returns JSON object pointed to by path_elems. json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6') foo
json_object_keys(json) SETOF text Returns set of keys in the JSON object. Only the outer object will be displayed. json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')
 json_object_keys
------------------
 f1
 f2
json_populate_record(base anyelement, from_json json, [, use_json_as_text bool=false] anyelement Expands the object in from_json to a row whose columns match the record type defined by base. Conversion will be best effort; columns in base with no corresponding key in from_json will be left null. If a column is specified more than once, the last value is used. select * from json_populate_record(null::x, '{"a":1,"b":2}')
 a | b
---+---
 1 | 2
json_populate_recordset(base anyelement, from_json json, [, use_json_as_text bool=false] SETOF anyelement Expands the outermost set of objects in from_json to a set whose columns match the record type defined by base. Conversion will be best effort; columns in base with no corresponding key in from_json will be left null. If a column is specified more than once, the last value is used. select * from json_populate_recordset(null::x, '[{"a":1,"b":2},{"a":3,"b":4}]')
 a | b
---+---
 1 | 2
 3 | 4
 
json_array_elements(json) SETOF json 把 JSON 数组展开为 JSON 元素集合。 json_array_elements('[1,true, [2,false]]')
   value
-----------
 1
 true
 [2,false]

注意

json 函数和操作符可能施加比该类型输入函数更严格的有效性要求。特别地,它们会仔细得多地检查对 Unicode 代理对指定基本多文种平面之外字符的任何使用是否正确。

注意

当数据库编码为 UTF8 时,这些函数和操作符中的许多会把 JSON 文本中的 Unicode 转义转换为相应的 UTF8 字符。在其他编码中,转义序列必须用于 ASCII 字符,Unicode 转义序列中的任何其他码点都会导致错误。一般而言,如果可能,最好避免在 JSON 中混用 Unicode 转义与非 UTF8 数据库编码。

注意

hstore 扩展有从 hstorejson 的转换,因此转换后的 hstore 值表示为 JSON 对象,而不是字符串值。

另请参阅第 9.20 节中关于聚集函数 json_agg 的说明,它能高效地把记录值聚集为 JSON。

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。