1-- Strings. 2SELECT '""'::json; -- OK. 3SELECT $$''$$::json; -- ERROR, single quotes are not allowed 4SELECT '"abc"'::json; -- OK 5SELECT '"abc'::json; -- ERROR, quotes not closed 6SELECT '"abc 7def"'::json; -- ERROR, unescaped newline in string constant 8SELECT '"\n\"\\"'::json; -- OK, legal escapes 9SELECT '"\v"'::json; -- ERROR, not a valid JSON escape 10-- see json_encoding test for input with unicode escapes 11 12-- Numbers. 13SELECT '1'::json; -- OK 14SELECT '0'::json; -- OK 15SELECT '01'::json; -- ERROR, not valid according to JSON spec 16SELECT '0.1'::json; -- OK 17SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8 18SELECT '1e100'::json; -- OK 19SELECT '1.3e100'::json; -- OK 20SELECT '1f2'::json; -- ERROR 21SELECT '0.x1'::json; -- ERROR 22SELECT '1.3ex100'::json; -- ERROR 23 24-- Arrays. 25SELECT '[]'::json; -- OK 26SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK 27SELECT '[1,2]'::json; -- OK 28SELECT '[1,2,]'::json; -- ERROR, trailing comma 29SELECT '[1,2'::json; -- ERROR, no closing bracket 30SELECT '[1,[2]'::json; -- ERROR, no closing bracket 31 32-- Objects. 33SELECT '{}'::json; -- OK 34SELECT '{"abc"}'::json; -- ERROR, no value 35SELECT '{"abc":1}'::json; -- OK 36SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings 37SELECT '{"abc",1}'::json; -- ERROR, wrong separator 38SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator 39SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator 40SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK 41SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot 42SELECT '{"abc":1,3}'::json; -- ERROR, no value 43 44-- Recursion. 45SET max_stack_depth = '100kB'; 46SELECT repeat('[', 10000)::json; 47SELECT repeat('{"a":', 10000)::json; 48RESET max_stack_depth; 49 50-- Miscellaneous stuff. 51SELECT 'true'::json; -- OK 52SELECT 'false'::json; -- OK 53SELECT 'null'::json; -- OK 54SELECT ' true '::json; -- OK, even with extra whitespace 55SELECT 'true false'::json; -- ERROR, too many values 56SELECT 'true, false'::json; -- ERROR, too many values 57SELECT 'truf'::json; -- ERROR, not a keyword 58SELECT 'trues'::json; -- ERROR, not a keyword 59SELECT ''::json; -- ERROR, no value 60SELECT ' '::json; -- ERROR, no value 61 62--constructors 63-- array_to_json 64 65SELECT array_to_json(array(select 1 as a)); 66SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q; 67SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q; 68SELECT array_to_json(array_agg(q),false) 69 FROM ( SELECT $$a$$ || x AS b, y AS c, 70 ARRAY[ROW(x.*,ARRAY[1,2,3]), 71 ROW(y.*,ARRAY[4,5,6])] AS z 72 FROM generate_series(1,2) x, 73 generate_series(4,5) y) q; 74SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x; 75SELECT array_to_json('{{1,5},{99,100}}'::int[]); 76 77-- row_to_json 78SELECT row_to_json(row(1,'foo')); 79 80SELECT row_to_json(q) 81FROM (SELECT $$a$$ || x AS b, 82 y AS c, 83 ARRAY[ROW(x.*,ARRAY[1,2,3]), 84 ROW(y.*,ARRAY[4,5,6])] AS z 85 FROM generate_series(1,2) x, 86 generate_series(4,5) y) q; 87 88SELECT row_to_json(q,true) 89FROM (SELECT $$a$$ || x AS b, 90 y AS c, 91 ARRAY[ROW(x.*,ARRAY[1,2,3]), 92 ROW(y.*,ARRAY[4,5,6])] AS z 93 FROM generate_series(1,2) x, 94 generate_series(4,5) y) q; 95 96CREATE TEMP TABLE rows AS 97SELECT x, 'txt' || x as y 98FROM generate_series(1,3) AS x; 99 100SELECT row_to_json(q,true) 101FROM rows q; 102 103SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false); 104 105-- anyarray column 106 107analyze rows; 108 109select attname, to_json(histogram_bounds) histogram_bounds 110from pg_stats 111where tablename = 'rows' and 112 schemaname = pg_my_temp_schema()::regnamespace::text 113order by 1; 114 115-- to_json, timestamps 116 117select to_json(timestamp '2014-05-28 12:22:35.614298'); 118 119BEGIN; 120SET LOCAL TIME ZONE 10.5; 121select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); 122SET LOCAL TIME ZONE -8; 123select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); 124COMMIT; 125 126select to_json(date '2014-05-28'); 127 128select to_json(date 'Infinity'); 129select to_json(date '-Infinity'); 130select to_json(timestamp 'Infinity'); 131select to_json(timestamp '-Infinity'); 132select to_json(timestamptz 'Infinity'); 133select to_json(timestamptz '-Infinity'); 134 135--json_agg 136 137SELECT json_agg(q) 138 FROM ( SELECT $$a$$ || x AS b, y AS c, 139 ARRAY[ROW(x.*,ARRAY[1,2,3]), 140 ROW(y.*,ARRAY[4,5,6])] AS z 141 FROM generate_series(1,2) x, 142 generate_series(4,5) y) q; 143 144SELECT json_agg(q ORDER BY x, y) 145 FROM rows q; 146 147UPDATE rows SET x = NULL WHERE x = 1; 148 149SELECT json_agg(q ORDER BY x NULLS FIRST, y) 150 FROM rows q; 151 152-- non-numeric output 153SELECT row_to_json(q) 154FROM (SELECT 'NaN'::float8 AS "float8field") q; 155 156SELECT row_to_json(q) 157FROM (SELECT 'Infinity'::float8 AS "float8field") q; 158 159SELECT row_to_json(q) 160FROM (SELECT '-Infinity'::float8 AS "float8field") q; 161 162-- json input 163SELECT row_to_json(q) 164FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q; 165 166 167-- json extraction functions 168 169CREATE TEMP TABLE test_json ( 170 json_type text, 171 test_json json 172); 173 174INSERT INTO test_json VALUES 175('scalar','"a scalar"'), 176('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'), 177('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}'); 178 179SELECT test_json -> 'x' 180FROM test_json 181WHERE json_type = 'scalar'; 182 183SELECT test_json -> 'x' 184FROM test_json 185WHERE json_type = 'array'; 186 187SELECT test_json -> 'x' 188FROM test_json 189WHERE json_type = 'object'; 190 191SELECT test_json->'field2' 192FROM test_json 193WHERE json_type = 'object'; 194 195SELECT test_json->>'field2' 196FROM test_json 197WHERE json_type = 'object'; 198 199SELECT test_json -> 2 200FROM test_json 201WHERE json_type = 'scalar'; 202 203SELECT test_json -> 2 204FROM test_json 205WHERE json_type = 'array'; 206 207SELECT test_json -> -1 208FROM test_json 209WHERE json_type = 'array'; 210 211SELECT test_json -> 2 212FROM test_json 213WHERE json_type = 'object'; 214 215SELECT test_json->>2 216FROM test_json 217WHERE json_type = 'array'; 218 219SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array'; 220SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array'; 221 222SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object'; 223SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object'; 224SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object'; 225 226SELECT json_object_keys(test_json) 227FROM test_json 228WHERE json_type = 'scalar'; 229 230SELECT json_object_keys(test_json) 231FROM test_json 232WHERE json_type = 'array'; 233 234SELECT json_object_keys(test_json) 235FROM test_json 236WHERE json_type = 'object'; 237 238-- test extending object_keys resultset - initial resultset size is 256 239 240select count(*) from 241 (select json_object_keys(json_object(array_agg(g))) 242 from (select unnest(array['f'||n,n::text])as g 243 from generate_series(1,300) as n) x ) y; 244 245-- nulls 246 247select (test_json->'field3') is null as expect_false 248from test_json 249where json_type = 'object'; 250 251select (test_json->>'field3') is null as expect_true 252from test_json 253where json_type = 'object'; 254 255select (test_json->3) is null as expect_false 256from test_json 257where json_type = 'array'; 258 259select (test_json->>3) is null as expect_true 260from test_json 261where json_type = 'array'; 262 263-- corner cases 264 265select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text; 266select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int; 267select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1; 268select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1; 269select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z'; 270select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> ''; 271select '[{"b": "c"}, {"b": "cc"}]'::json -> 1; 272select '[{"b": "c"}, {"b": "cc"}]'::json -> 3; 273select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z'; 274select '{"a": "c", "b": null}'::json -> 'b'; 275select '"foo"'::json -> 1; 276select '"foo"'::json -> 'z'; 277 278select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text; 279select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int; 280select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1; 281select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z'; 282select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> ''; 283select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1; 284select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3; 285select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z'; 286select '{"a": "c", "b": null}'::json ->> 'b'; 287select '"foo"'::json ->> 1; 288select '"foo"'::json ->> 'z'; 289 290-- array length 291 292SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]'); 293 294SELECT json_array_length('[]'); 295 296SELECT json_array_length('{"f1":1,"f2":[5,6]}'); 297 298SELECT json_array_length('4'); 299 300-- each 301 302select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}'); 303select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q; 304 305select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}'); 306select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q; 307 308-- extract_path, extract_path_as_text 309 310select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6'); 311select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2'); 312select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text); 313select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text); 314select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6'); 315select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2'); 316select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text); 317select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text); 318 319-- extract_path nulls 320 321select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false; 322select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true; 323select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false; 324select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true; 325 326-- extract_path operators 327 328select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6']; 329select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2']; 330select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0']; 331select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1']; 332 333select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6']; 334select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2']; 335select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0']; 336select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1']; 337 338-- corner cases for same 339select '{"a": {"b":{"c": "foo"}}}'::json #> '{}'; 340select '[1,2,3]'::json #> '{}'; 341select '"foo"'::json #> '{}'; 342select '42'::json #> '{}'; 343select 'null'::json #> '{}'; 344select '{"a": {"b":{"c": "foo"}}}'::json #> array['a']; 345select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null]; 346select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', '']; 347select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b']; 348select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c']; 349select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d']; 350select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c']; 351select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b']; 352select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b']; 353select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b']; 354select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b']; 355select '[{"b": "c"}, {"b": null}]'::json #> array['1','b']; 356select '"foo"'::json #> array['z']; 357select '42'::json #> array['f2']; 358select '42'::json #> array['0']; 359 360select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; 361select '[1,2,3]'::json #>> '{}'; 362select '"foo"'::json #>> '{}'; 363select '42'::json #>> '{}'; 364select 'null'::json #>> '{}'; 365select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a']; 366select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null]; 367select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', '']; 368select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b']; 369select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c']; 370select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d']; 371select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c']; 372select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b']; 373select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b']; 374select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b']; 375select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b']; 376select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b']; 377select '"foo"'::json #>> array['z']; 378select '42'::json #>> array['f2']; 379select '42'::json #>> array['0']; 380 381-- array_elements 382 383select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]'); 384select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q; 385select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]'); 386select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q; 387 388-- populate_record 389create type jpop as (a text, b int, c timestamp); 390 391CREATE DOMAIN js_int_not_null AS int NOT NULL; 392CREATE DOMAIN js_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3); 393CREATE DOMAIN js_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3); 394 395create type j_unordered_pair as (x int, y int); 396create domain j_ordered_pair as j_unordered_pair check((value).x <= (value).y); 397 398CREATE TYPE jsrec AS ( 399 i int, 400 ia _int4, 401 ia1 int[], 402 ia2 int[][], 403 ia3 int[][][], 404 ia1d js_int_array_1d, 405 ia2d js_int_array_2d, 406 t text, 407 ta text[], 408 c char(10), 409 ca char(10)[], 410 ts timestamp, 411 js json, 412 jsb jsonb, 413 jsa json[], 414 rec jpop, 415 reca jpop[] 416); 417 418CREATE TYPE jsrec_i_not_null AS ( 419 i js_int_not_null 420); 421 422select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q; 423select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q; 424 425select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q; 426select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q; 427 428select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q; 429select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q; 430select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q; 431 432select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q; 433 434SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q; 435SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q; 436SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q; 437 438SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q; 439SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q; 440SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q; 441SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q; 442SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q; 443SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q; 444SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q; 445 446SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q; 447SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q; 448SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q; 449SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q; 450 451SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q; 452SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q; 453SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q; 454SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q; 455 456SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q; 457SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q; 458SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q; 459SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q; 460SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q; 461 462SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q; 463SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q; 464 465SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q; 466SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q; 467SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q; 468SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q; 469SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q; 470SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q; 471 472SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q; 473SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q; 474SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q; 475SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q; 476 477SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q; 478SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q; 479SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q; 480SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q; 481 482SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q; 483SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q; 484SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q; 485SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q; 486SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q; 487 488SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q; 489SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q; 490SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q; 491SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q; 492SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q; 493SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q; 494SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q; 495 496SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q; 497SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q; 498SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q; 499SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q; 500SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q; 501SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q; 502SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q; 503 504SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q; 505SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q; 506SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q; 507SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q; 508 509SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q; 510SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q; 511SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q; 512SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q; 513 514SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q; 515SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q; 516SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q; 517SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q; 518SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q; 519 520SELECT rec FROM json_populate_record( 521 row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, 522 row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec, 523 '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}' 524) q; 525 526-- anonymous record type 527SELECT json_populate_record(null::record, '{"x": 0, "y": 1}'); 528SELECT json_populate_record(row(1,2), '{"f1": 0, "f2": 1}'); 529SELECT * FROM 530 json_populate_record(null::record, '{"x": 776}') AS (x int, y int); 531 532-- composite domain 533SELECT json_populate_record(null::j_ordered_pair, '{"x": 0, "y": 1}'); 534SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 0}'); 535SELECT json_populate_record(row(1,2)::j_ordered_pair, '{"x": 1, "y": 0}'); 536 537-- populate_recordset 538 539select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 540select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 541select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 542select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 543select * 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; 544select * 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; 545 546create type jpop2 as (a int, b json, c int, d int); 547select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q; 548 549select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 550select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 551select * 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; 552 553-- anonymous record type 554SELECT json_populate_recordset(null::record, '[{"x": 0, "y": 1}]'); 555SELECT json_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]'); 556SELECT i, json_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]') 557FROM (VALUES (1),(2)) v(i); 558SELECT * FROM 559 json_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int); 560 561-- empty array is a corner case 562SELECT json_populate_recordset(null::record, '[]'); 563SELECT json_populate_recordset(row(1,2), '[]'); 564SELECT * FROM json_populate_recordset(NULL::jpop,'[]') q; 565SELECT * FROM 566 json_populate_recordset(null::record, '[]') AS (x int, y int); 567 568-- composite domain 569SELECT json_populate_recordset(null::j_ordered_pair, '[{"x": 0, "y": 1}]'); 570SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 0}, {"y": 3}]'); 571SELECT json_populate_recordset(row(1,2)::j_ordered_pair, '[{"x": 1, "y": 0}]'); 572 573-- negative cases where the wrong record type is supplied 574select * from json_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 575select * from json_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 576select * from json_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 577select * from json_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text); 578 579-- test type info caching in json_populate_record() 580CREATE TEMP TABLE jspoptest (js json); 581 582INSERT INTO jspoptest 583SELECT '{ 584 "jsa": [1, "2", null, 4], 585 "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}, 586 "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}] 587}'::json 588FROM generate_series(1, 3); 589 590SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest; 591 592DROP TYPE jsrec; 593DROP TYPE jsrec_i_not_null; 594DROP DOMAIN js_int_not_null; 595DROP DOMAIN js_int_array_1d; 596DROP DOMAIN js_int_array_2d; 597DROP DOMAIN j_ordered_pair; 598DROP TYPE j_unordered_pair; 599 600--json_typeof() function 601select value, json_typeof(value) 602 from (values (json '123.4'), 603 (json '-1'), 604 (json '"foo"'), 605 (json 'true'), 606 (json 'false'), 607 (json 'null'), 608 (json '[1, 2, 3]'), 609 (json '[]'), 610 (json '{"x":"foo", "y":123}'), 611 (json '{}'), 612 (NULL::json)) 613 as data(value); 614 615-- json_build_array, json_build_object, json_object_agg 616 617SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); 618SELECT json_build_array('a', NULL); -- ok 619SELECT json_build_array(VARIADIC NULL::text[]); -- ok 620SELECT json_build_array(VARIADIC '{}'::text[]); -- ok 621SELECT json_build_array(VARIADIC '{a,b,c}'::text[]); -- ok 622SELECT json_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok 623SELECT json_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok 624SELECT json_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok 625SELECT json_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok 626 627SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); 628 629SELECT json_build_object( 630 'a', json_build_object('b',false,'c',99), 631 'd', json_build_object('e',array[9,8,7]::int[], 632 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r))); 633SELECT json_build_object('{a,b,c}'::text[]); -- error 634SELECT json_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array 635SELECT json_build_object('a', 'b', 'c'); -- error 636SELECT json_build_object(NULL, 'a'); -- error, key cannot be NULL 637SELECT json_build_object('a', NULL); -- ok 638SELECT json_build_object(VARIADIC NULL::text[]); -- ok 639SELECT json_build_object(VARIADIC '{}'::text[]); -- ok 640SELECT json_build_object(VARIADIC '{a,b,c}'::text[]); -- error 641SELECT json_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok 642SELECT json_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL 643SELECT json_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok 644SELECT json_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok 645SELECT json_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok 646 647-- empty objects/arrays 648SELECT json_build_array(); 649 650SELECT json_build_object(); 651 652-- make sure keys are quoted 653SELECT json_build_object(1,2); 654 655-- keys must be scalar and not null 656SELECT json_build_object(null,2); 657 658SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r; 659 660SELECT json_build_object(json '{"a":1,"b":2}', 3); 661 662SELECT json_build_object('{1,2,3}'::int[], 3); 663 664CREATE TEMP TABLE foo (serial_num int, name text, type text); 665INSERT INTO foo VALUES (847001,'t15','GE1043'); 666INSERT INTO foo VALUES (847002,'t16','GE1043'); 667INSERT INTO foo VALUES (847003,'sub-alpha','GESS90'); 668 669SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type))) 670FROM foo; 671 672SELECT json_object_agg(name, type) FROM foo; 673 674INSERT INTO foo VALUES (999999, NULL, 'bar'); 675SELECT json_object_agg(name, type) FROM foo; 676 677-- json_object 678 679-- empty object, one dimension 680SELECT json_object('{}'); 681 682-- empty object, two dimensions 683SELECT json_object('{}', '{}'); 684 685-- one dimension 686SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}'); 687 688-- same but with two dimensions 689SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); 690 691-- odd number error 692SELECT json_object('{a,b,c}'); 693 694-- one column error 695SELECT json_object('{{a},{b}}'); 696 697-- too many columns error 698SELECT json_object('{{a,b,c},{b,c,d}}'); 699 700-- too many dimensions error 701SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}'); 702 703--two argument form of json_object 704 705select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}'); 706 707-- too many dimensions 708SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); 709 710-- mismatched dimensions 711 712select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}'); 713 714select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}'); 715 716-- null key error 717 718select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}'); 719 720-- empty key is allowed 721 722select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}'); 723 724 725-- json_to_record and json_to_recordset 726 727select * from json_to_record('{"a":1,"b":"foo","c":"bar"}') 728 as x(a int, b text, d text); 729 730select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]') 731 as x(a int, b text, c boolean); 732 733select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]') 734 as x(a int, b json, c boolean); 735 736select *, c is null as c_is_null 737from 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) 738 as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop); 739 740select *, c is null as c_is_null 741from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json) 742 as t(a int, b json, c text, x int); 743 744select * from json_to_record('{"ia": null}') as x(ia _int4); 745select * from json_to_record('{"ia": 123}') as x(ia _int4); 746select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4); 747select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4); 748select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4); 749select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4); 750 751select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]); 752select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]); 753select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]); 754 755select * from json_to_record('{"out": {"key": 1}}') as x(out json); 756select * from json_to_record('{"out": [{"key": 1}]}') as x(out json); 757select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out json); 758select * from json_to_record('{"out": {"key": 1}}') as x(out jsonb); 759select * from json_to_record('{"out": [{"key": 1}]}') as x(out jsonb); 760select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb); 761 762-- json_strip_nulls 763 764select json_strip_nulls(null); 765 766select json_strip_nulls('1'); 767 768select json_strip_nulls('"a string"'); 769 770select json_strip_nulls('null'); 771 772select json_strip_nulls('[1,2,null,3,4]'); 773 774select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}'); 775 776select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]'); 777 778-- an empty object is not null and should not be stripped 779select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }'); 780 781-- json to tsvector 782select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json); 783 784-- json to tsvector with config 785select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json); 786 787-- json to tsvector with stop words 788select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json); 789 790-- json to tsvector with numeric values 791select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::json); 792 793-- json_to_tsvector 794select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"'); 795select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"'); 796select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"'); 797select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"'); 798select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"'); 799select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]'); 800 801select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"all"'); 802select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"key"'); 803select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"string"'); 804select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"numeric"'); 805select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '"boolean"'); 806select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["string", "numeric"]'); 807 808-- to_tsvector corner cases 809select to_tsvector('""'::json); 810select to_tsvector('{}'::json); 811select to_tsvector('[]'::json); 812select to_tsvector('null'::json); 813 814-- json_to_tsvector corner cases 815select json_to_tsvector('""'::json, '"all"'); 816select json_to_tsvector('{}'::json, '"all"'); 817select json_to_tsvector('[]'::json, '"all"'); 818select json_to_tsvector('null'::json, '"all"'); 819 820select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '""'); 821select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '{}'); 822select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '[]'); 823select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, 'null'); 824select json_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::json, '["all", null]'); 825 826-- ts_headline for json 827select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh')); 828select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh')); 829select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >'); 830select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >'); 831 832-- corner cases for ts_headline with json 833select ts_headline('null'::json, tsquery('aaa & bbb')); 834select ts_headline('{}'::json, tsquery('aaa & bbb')); 835select ts_headline('[]'::json, tsquery('aaa & bbb')); 836