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