首页 > 数据库 > 数据库开发技术 > PostgreSQL 15 JSON 函数增强
JSON()
JSON_SCALAR()
JSON_SERIALIZE()
JSON_ARRAY()
JSON_ARRAYAGG()
JSON_OBJECT()
JSON_OBJECTAGG()
select json('"json"');
json
----------
"json"
select json('[1,2,3]');
json
---------
[1,2,3]select json('{"a": 123, "b": [true, "foo"], "a": "bar"}' with unique keys);ERROR: duplicate JSON object key valueselect json_array( 1, '2022', 'json' );
json_array
-------------------------------
[1, "2022", "json"]select json_arrayagg(datname order by datname) from pg_database;
json_arrayagg
-----------------------------------------------------------
["hrdb", "postgres", "template0", "template1"]select json_object( 'id': 1, 'aid': json_array(1,2,3));
json_object
------------------------------
{"id" : 1, "aid" : [1, 2, 3]}select json_objectagg( datname: oid) from pg_database;
json_objectagg
---------------------------------------------------------------------------------------------------
{ "hrdb" : "16668", "postgres" : "14617", "template0" : "14616", "template1" : "1" }
IS [NOT] JSON [VALUE]
IS [NOT] JSON ARRAY
IS [NOT] JSON OBJECT
IS [NOT] JSON SCALAR
IS [NOT] JSON WITH | WITHOUT UNIQUE KEYS
SELECT
js,
js IS JSON "is json",
js IS NOT JSON "is not json",
js IS JSON SCALAR "is scalar",
js IS JSON OBJECT "is object",
js IS JSON ARRAY "is array"FROM
(VALUES ('123'), ('"abc"'), ('{"a": "b"}'), ('[1,2]'), ('abc')) foo(js);
js | is json | is not json | is scalar | is object | is array------------+---------+-------------+-----------+-----------|-------------
123 | t | f | t | f | f "abc" | t | f | t | f | f
{"a": "b"} | t | f | f | t | f [1,2] | t | f | f | f | t
abc | f | t | f | f | f(5 rows)
JSON_EXISTS()
JSON_QUERY()
JSON_VALUE()
select json_exists( '{"id":123}'::jsonb, '$.id' ), json_exists( '{"id":123}'::jsonb, '$.aid' );
json_exists | json_exists
-------------+-------------
t | f
select json_query('[{"x": "aaa"},{"x": "bbb"},{"x": "ccc"}]'::jsonb, '$[1].x' );
json_query
------------
"bbb"
SELECT JSON_VALUE('"123.45"', '$' RETURNING float);
json_value------------
123.45
select * from json_table(
'[{"a":10,"b":20},{"a":30,"b":40}]'::jsonb,
'$[*]'
COLUMNS (
column_a int4 path '$.a',
column_b int4 path '$.b'
));
column_a | column_b
----------+----------
10 | 20
30 | 40
原文作者:SQL编程思想;如有侵权,请联系管理员删除。