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 jsrec AS ( 392 i int, 393 ia _int4, 394 ia1 int[], 395 ia2 int[][], 396 ia3 int[][][], 397 ia1d js_int_array_1d, 398 ia2d js_int_array_2d, 399 t text, 400 ta text[], 401 c char(10), 402 ca char(10)[], 403 ts timestamp, 404 js json, 405 jsb jsonb, 406 jsa json[], 407 rec jpop, 408 reca jpop[] 409); 410 411CREATE TYPE jsrec_i_not_null AS ( 412 i js_int_not_null 413); 414 415select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q; 416select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q; 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":[100,200,false],"x":43.2}') q; 422select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q; 423select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q; 424 425select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q; 426 427SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q; 428SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q; 429SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q; 430 431SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q; 432SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q; 433SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q; 434SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q; 435SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q; 436SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q; 437SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q; 438 439SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q; 440SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q; 441SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q; 442SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q; 443 444SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q; 445SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q; 446SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q; 447SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q; 448 449SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q; 450SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q; 451SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q; 452SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q; 453SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q; 454 455SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q; 456SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q; 457 458SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q; 459SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q; 460SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q; 461SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q; 462SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q; 463SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q; 464 465SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q; 466SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q; 467SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q; 468SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q; 469 470SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q; 471SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q; 472SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q; 473SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q; 474 475SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q; 476SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q; 477SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q; 478SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q; 479SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q; 480 481SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q; 482SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q; 483SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q; 484SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q; 485SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q; 486SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q; 487SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q; 488 489SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q; 490SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q; 491SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q; 492SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q; 493SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q; 494SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q; 495SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q; 496 497SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q; 498SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q; 499SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q; 500SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q; 501 502SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q; 503SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q; 504SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q; 505SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q; 506 507SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q; 508SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q; 509SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q; 510SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q; 511SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q; 512 513SELECT rec FROM json_populate_record( 514 row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, 515 row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec, 516 '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}' 517) q; 518 519-- populate_recordset 520 521select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 522select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 523select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 524select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 525select * 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; 526select * 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; 527 528create type jpop2 as (a int, b json, c int, d int); 529select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q; 530 531select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 532select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; 533select * 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; 534 535-- negative cases where the wrong record type is supplied 536select * from json_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 537select * from json_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 538select * from json_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text); 539select * from json_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text); 540 541-- test type info caching in json_populate_record() 542CREATE TEMP TABLE jspoptest (js json); 543 544INSERT INTO jspoptest 545SELECT '{ 546 "jsa": [1, "2", null, 4], 547 "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}, 548 "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}] 549}'::json 550FROM generate_series(1, 3); 551 552SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest; 553 554DROP TYPE jsrec; 555DROP TYPE jsrec_i_not_null; 556DROP DOMAIN js_int_not_null; 557DROP DOMAIN js_int_array_1d; 558DROP DOMAIN js_int_array_2d; 559 560--json_typeof() function 561select value, json_typeof(value) 562 from (values (json '123.4'), 563 (json '-1'), 564 (json '"foo"'), 565 (json 'true'), 566 (json 'false'), 567 (json 'null'), 568 (json '[1, 2, 3]'), 569 (json '[]'), 570 (json '{"x":"foo", "y":123}'), 571 (json '{}'), 572 (NULL::json)) 573 as data(value); 574 575-- json_build_array, json_build_object, json_object_agg 576 577SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); 578SELECT json_build_array('a', NULL); -- ok 579SELECT json_build_array(VARIADIC NULL::text[]); -- ok 580SELECT json_build_array(VARIADIC '{}'::text[]); -- ok 581SELECT json_build_array(VARIADIC '{a,b,c}'::text[]); -- ok 582SELECT json_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok 583SELECT json_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok 584SELECT json_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok 585SELECT json_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok 586 587SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}'); 588 589SELECT json_build_object( 590 'a', json_build_object('b',false,'c',99), 591 'd', json_build_object('e',array[9,8,7]::int[], 592 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r))); 593SELECT json_build_object('{a,b,c}'::text[]); -- error 594SELECT json_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array 595SELECT json_build_object('a', 'b', 'c'); -- error 596SELECT json_build_object(NULL, 'a'); -- error, key cannot be NULL 597SELECT json_build_object('a', NULL); -- ok 598SELECT json_build_object(VARIADIC NULL::text[]); -- ok 599SELECT json_build_object(VARIADIC '{}'::text[]); -- ok 600SELECT json_build_object(VARIADIC '{a,b,c}'::text[]); -- error 601SELECT json_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok 602SELECT json_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL 603SELECT json_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok 604SELECT json_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok 605SELECT json_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok 606 607-- empty objects/arrays 608SELECT json_build_array(); 609 610SELECT json_build_object(); 611 612-- make sure keys are quoted 613SELECT json_build_object(1,2); 614 615-- keys must be scalar and not null 616SELECT json_build_object(null,2); 617 618SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r; 619 620SELECT json_build_object(json '{"a":1,"b":2}', 3); 621 622SELECT json_build_object('{1,2,3}'::int[], 3); 623 624CREATE TEMP TABLE foo (serial_num int, name text, type text); 625INSERT INTO foo VALUES (847001,'t15','GE1043'); 626INSERT INTO foo VALUES (847002,'t16','GE1043'); 627INSERT INTO foo VALUES (847003,'sub-alpha','GESS90'); 628 629SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type))) 630FROM foo; 631 632SELECT json_object_agg(name, type) FROM foo; 633 634INSERT INTO foo VALUES (999999, NULL, 'bar'); 635SELECT json_object_agg(name, type) FROM foo; 636 637-- json_object 638 639-- empty object, one dimension 640SELECT json_object('{}'); 641 642-- empty object, two dimensions 643SELECT json_object('{}', '{}'); 644 645-- one dimension 646SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}'); 647 648-- same but with two dimensions 649SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}'); 650 651-- odd number error 652SELECT json_object('{a,b,c}'); 653 654-- one column error 655SELECT json_object('{{a},{b}}'); 656 657-- too many columns error 658SELECT json_object('{{a,b,c},{b,c,d}}'); 659 660-- too many dimensions error 661SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}'); 662 663--two argument form of json_object 664 665select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}'); 666 667-- too many dimensions 668SELECT 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"}}'); 669 670-- mismatched dimensions 671 672select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}'); 673 674select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}'); 675 676-- null key error 677 678select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}'); 679 680-- empty key is allowed 681 682select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}'); 683 684 685-- json_to_record and json_to_recordset 686 687select * from json_to_record('{"a":1,"b":"foo","c":"bar"}') 688 as x(a int, b text, d text); 689 690select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]') 691 as x(a int, b text, c boolean); 692 693select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]') 694 as x(a int, b json, c boolean); 695 696select *, c is null as c_is_null 697from 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) 698 as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop); 699 700select *, c is null as c_is_null 701from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json) 702 as t(a int, b json, c text, x int); 703 704select * from json_to_record('{"ia": null}') as x(ia _int4); 705select * from json_to_record('{"ia": 123}') as x(ia _int4); 706select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4); 707select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4); 708select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4); 709select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4); 710 711select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]); 712select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]); 713select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]); 714 715select * from json_to_record('{"out": {"key": 1}}') as x(out json); 716select * from json_to_record('{"out": [{"key": 1}]}') as x(out json); 717select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out json); 718select * from json_to_record('{"out": {"key": 1}}') as x(out jsonb); 719select * from json_to_record('{"out": [{"key": 1}]}') as x(out jsonb); 720select * from json_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb); 721 722-- json_strip_nulls 723 724select json_strip_nulls(null); 725 726select json_strip_nulls('1'); 727 728select json_strip_nulls('"a string"'); 729 730select json_strip_nulls('null'); 731 732select json_strip_nulls('[1,2,null,3,4]'); 733 734select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}'); 735 736select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]'); 737 738-- an empty object is not null and should not be stripped 739select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }'); 740 741-- json to tsvector 742select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json); 743 744-- json to tsvector with config 745select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json); 746 747-- json to tsvector with stop words 748select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json); 749 750-- ts_vector corner cases 751select to_tsvector('""'::json); 752select to_tsvector('{}'::json); 753select to_tsvector('[]'::json); 754select to_tsvector('null'::json); 755 756-- ts_headline for json 757select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh')); 758select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh')); 759select 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 = >'); 760select 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 = >'); 761 762-- corner cases for ts_headline with json 763select ts_headline('null'::json, tsquery('aaa & bbb')); 764select ts_headline('{}'::json, tsquery('aaa & bbb')); 765select ts_headline('[]'::json, tsquery('aaa & bbb')); 766