-- Strings. SELECT'""'::json; -- OK. SELECT $$''$$::json; -- ERROR, single quotes are not allowed SELECT'"abc"'::json; -- OK SELECT'"abc'::json; -- ERROR, quotes not closed SELECT'"abc
def"'::json; -- ERROR, unescaped newline in string constant SELECT'"\n\"\\"'::json; -- OK, legal escapes SELECT'"\v"'::json; -- ERROR, not a valid JSON escape
-- Check fast path for longer strings (at least 16 bytes long) SELECT ('"'||repeat('.', 12)||'abc"')::json; -- OK SELECT ('"'||repeat('.', 12)||'abc\n"')::json; -- OK, legal escapes
-- Test various lengths of strings to validate SIMD processing to escape -- special chars in the JSON. SELECT row_to_json(j)::jsonb FROM ( SELECTleft(E'abcdefghijklmnopqrstuv"\twxyz012345678', a) AS a FROM generate_series(0,37) a
) j;
-- see json_encoding test for input with unicode escapes
-- Numbers. SELECT'1'::json; -- OK SELECT'0'::json; -- OK SELECT'01'::json; -- ERROR, not valid according to JSON spec SELECT'0.1'::json; -- OK SELECT'9223372036854775808'::json; -- OK, even though it's too large for int8 SELECT'1e100'::json; -- OK SELECT'1.3e100'::json; -- OK SELECT'1f2'::json; -- ERROR SELECT'0.x1'::json; -- ERROR SELECT'1.3ex100'::json; -- ERROR
-- Arrays. SELECT'[]'::json; -- OK SELECT'[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK SELECT'[1,2]'::json; -- OK SELECT'[1,2,]'::json; -- ERROR, trailing comma SELECT'[1,2'::json; -- ERROR, no closing bracket SELECT'[1,[2]'::json; -- ERROR, no closing bracket
-- Objects. SELECT'{}'::json; -- OK SELECT'{"abc"}'::json; -- ERROR, no value SELECT'{"abc":1}'::json; -- OK SELECT'{1:"abc"}'::json; -- ERROR, keys must be strings SELECT'{"abc",1}'::json; -- ERROR, wrong separator SELECT'{"abc"=1}'::json; -- ERROR, totally wrong separator SELECT'{"abc"::1}'::json; -- ERROR, another wrong separator SELECT'{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK SELECT'{"abc":1:2}'::json; -- ERROR, colon in wrong spot SELECT'{"abc":1,3}'::json; -- ERROR, no value
-- Miscellaneous stuff. SELECT'true'::json; -- OK SELECT'false'::json; -- OK SELECT'null'::json; -- OK SELECT' true '::json; -- OK, even with extra whitespace SELECT'true false'::json; -- ERROR, too many values SELECT'true, false'::json; -- ERROR, too many values SELECT'truf'::json; -- ERROR, not a keyword SELECT'trues'::json; -- ERROR, not a keyword SELECT''::json; -- ERROR, no value SELECT' '::json; -- ERROR, no value
-- Multi-line JSON input to check ERROR reporting SELECT'{ "one": 1, "two":"two", "three": true}'::json; -- OK SELECT'{ "one": 1, "two":,"two", -- ERROR extraneous comma before field "two" "three": true}'::json; SELECT'{ "one": 1, "two":"two", "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json; -- ERROR missing value for last field
-- test non-error-throwing input select pg_input_is_valid('{"a":true}', 'json'); select pg_input_is_valid('{"a":true', 'json'); select * from pg_input_error_info('{"a":true', 'json');
--constructors -- array_to_json
SELECT array_to_json(array(select1as a)); SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2as c from generate_series(1,3) x) q; SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2as c from generate_series(1,3) x) q; SELECT array_to_json(array_agg(q),false) FROM ( SELECT $$a$$ || x AS b, y AS c,
ARRAY[ROW(x.*,ARRAY[1,2,3]),
ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x,
generate_series(4,5) y) q; SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x; SELECT array_to_json('{{1,5},{99,100}}'::int[]);
-- row_to_json SELECT row_to_json(row(1,'foo'));
SELECT row_to_json(q) FROM (SELECT $$a$$ || x AS b,
y AS c,
ARRAY[ROW(x.*,ARRAY[1,2,3]),
ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x,
generate_series(4,5) y) q;
SELECT row_to_json(q,true) FROM (SELECT $$a$$ || x AS b,
y AS c,
ARRAY[ROW(x.*,ARRAY[1,2,3]),
ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x,
generate_series(4,5) y) q;
CREATE TEMP TABLE rows AS SELECT x, 'txt' || x as y FROM generate_series(1,3) AS x;
SELECT row_to_json(q,true) FROM rows q;
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
-- anyarray column
analyze rows;
select attname, to_json(histogram_bounds) histogram_bounds from pg_stats where tablename = 'rows'and
schemaname = pg_my_temp_schema()::regnamespace::text orderby1;
BEGIN; SET LOCAL TIME ZONE 10.5; select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); SET LOCAL TIME ZONE -8; select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); COMMIT;
SELECT json_agg(q) FROM ( SELECT $$a$$ || x AS b, y AS c,
ARRAY[ROW(x.*,ARRAY[1,2,3]),
ROW(y.*,ARRAY[4,5,6])] AS z FROM generate_series(1,2) x,
generate_series(4,5) y) q;
SELECT json_agg(q ORDERBY x, y) FROM rows q;
UPDATE rows SET x = NULLWHERE x = 1;
SELECT json_agg(q ORDERBY x NULLS FIRST, y) FROM rows q;
-- non-numeric output SELECT row_to_json(q) FROM (SELECT'NaN'::float8AS"float8field") q;
SELECT row_to_json(q) FROM (SELECT'Infinity'::float8AS"float8field") q;
SELECT row_to_json(q) FROM (SELECT'-Infinity'::float8AS"float8field") q;
-- json input SELECT row_to_json(q) FROM (SELECT'{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS"jsonfield") q;
SELECT test_json -> 'x' FROM test_json WHERE json_type = 'scalar';
SELECT test_json -> 'x' FROM test_json WHERE json_type = 'array';
SELECT test_json -> 'x' FROM test_json WHERE json_type = 'object';
SELECT test_json->'field2' FROM test_json WHERE json_type = 'object';
SELECT test_json->>'field2' FROM test_json WHERE json_type = 'object';
SELECT test_json -> 2 FROM test_json WHERE json_type = 'scalar';
SELECT test_json -> 2 FROM test_json WHERE json_type = 'array';
SELECT test_json -> -1 FROM test_json WHERE json_type = 'array';
SELECT test_json -> 2 FROM test_json WHERE json_type = 'object';
SELECT test_json->>2 FROM test_json WHERE json_type = 'array';
SELECT test_json ->> 6FROM test_json WHERE json_type = 'array'; SELECT test_json ->> 7FROM test_json WHERE json_type = 'array';
SELECT test_json ->> 'field4'FROM test_json WHERE json_type = 'object'; SELECT test_json ->> 'field5'FROM test_json WHERE json_type = 'object'; SELECT test_json ->> 'field6'FROM test_json WHERE json_type = 'object';
SELECT json_object_keys(test_json) FROM test_json WHERE json_type = 'scalar';
SELECT json_object_keys(test_json) FROM test_json WHERE json_type = 'array';
SELECT json_object_keys(test_json) FROM test_json WHERE json_type = 'object';
-- test extending object_keys resultset - initial resultset size is 256
select count(*) from
(select json_object_keys(json_object(array_agg(g))) from (select unnest(array['f'||n,n::text])as g from generate_series(1,300) as n) x ) y;
-- nulls
select (test_json->'field3') isnullas expect_false from test_json where json_type = 'object';
select (test_json->>'field3') isnullas expect_true from test_json where json_type = 'object';
select (test_json->3) isnullas expect_false from test_json where json_type = 'array';
select (test_json->>3) isnullas expect_true from test_json where json_type = 'array';
create type j_unordered_pair as (x int, y int); create domain j_ordered_pair as j_unordered_pair check((value).x <= (value).y);
CREATE TYPE jsrec AS (
i int,
ia _int4,
ia1 int[],
ia2 int[][],
ia3 int[][][],
ia1d js_int_array_1d,
ia2d js_int_array_2d,
t text,
ta text[],
c char(10),
ca char(10)[],
ts timestamp,
js json,
jsb jsonb,
jsa json[],
rec jpop,
reca jpop[]
);
CREATE TYPE jsrec_i_not_null AS (
i js_int_not_null
);
select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q; select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q; select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q; select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q; select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q;
SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q; SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q; SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q;
SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q; SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q;
SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q; SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q; SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q; SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q;
SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q; SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q; SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q; SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q;
SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q; SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q; SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q; SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q; SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q;
SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q; SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q; SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q; SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q; SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q; SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q; SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q;
SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q; SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q; SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q; SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q; SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q; SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q; SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
create type jpop2 as (a int, b json, c int, d int); select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
-- anonymous record type SELECT json_populate_recordset(null::record, '[{"x": 0, "y": 1}]'); SELECT json_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]'); SELECT i, json_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]') FROM (VALUES (1),(2)) v(i); SELECT * FROM
json_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
-- empty array is a corner case SELECT json_populate_recordset(null::record, '[]'); SELECT json_populate_recordset(row(1,2), '[]'); SELECT * FROM json_populate_recordset(NULL::jpop,'[]') q; SELECT * FROM
json_populate_recordset(null::record, '[]') AS (x int, y int);
-- negative cases where the wrong record type is supplied select * from json_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); select * from json_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); select * from json_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); select * from json_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
-- test type info caching in json_populate_record() CREATE TEMP TABLE jspoptest (js json);
SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest;
DROP TYPE jsrec; DROP TYPE jsrec_i_not_null; DROP DOMAIN js_int_not_null; DROP DOMAIN js_int_array_1d; DROP DOMAIN js_int_array_2d; DROP DOMAIN j_ordered_pair; DROP TYPE j_unordered_pair;
select * from json_to_record('{"a":1,"b":"foo","c":"bar"}') as x(a int, b text, d text);
select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]') as x(a int, b text, c boolean);
select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]') as x(a int, b json, c boolean);
select *, c isnullas c_is_null from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::json) as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop);
select *, c isnullas c_is_null from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json) as t(a int, b json, c text, x int);
select * from json_to_record('{"ia": null}') as x(ia _int4); select * from json_to_record('{"ia": 123}') as x(ia _int4); select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4); select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4); select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4); select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]); select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]); select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
select * from json_to_record('{"out": {"key": 1}}') as x(out json); select * from json_to_record('{"out": [{"key": 1}]}') as x(out json); select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out json); select * from json_to_record('{"out": {"key": 1}}') as x(out jsonb); select * from json_to_record('{"out": [{"key": 1}]}') as x(out jsonb); select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.17Bemerkung:
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Angebot
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.