2 SELECT '""'::json; -- OK.
8 SELECT $$''$$::json; -- ERROR, single quotes are not allowed
9 ERROR: invalid input syntax for type json
10 LINE 1: SELECT $$''$$::json;
12 DETAIL: Token "'" is invalid.
13 CONTEXT: JSON data, line 1: '...
14 SELECT '"abc"'::json; -- OK
20 SELECT '"abc'::json; -- ERROR, quotes not closed
21 ERROR: invalid input syntax for type json
22 LINE 1: SELECT '"abc'::json;
24 DETAIL: Token ""abc" is invalid.
25 CONTEXT: JSON data, line 1: "abc
27 def"'::json; -- ERROR, unescaped newline in string constant
28 ERROR: invalid input syntax for type json
31 DETAIL: Character with value 0x0a must be escaped.
32 CONTEXT: JSON data, line 1: "abc
33 SELECT '"\n\"\\"'::json; -- OK, legal escapes
39 SELECT '"\v"'::json; -- ERROR, not a valid JSON escape
40 ERROR: invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::json;
43 DETAIL: Escape sequence "\v" is invalid.
44 CONTEXT: JSON data, line 1: "\v...
45 -- see json_encoding test for input with unicode escapes
47 SELECT '1'::json; -- OK
53 SELECT '0'::json; -- OK
59 SELECT '01'::json; -- ERROR, not valid according to JSON spec
60 ERROR: invalid input syntax for type json
61 LINE 1: SELECT '01'::json;
63 DETAIL: Token "01" is invalid.
64 CONTEXT: JSON data, line 1: 01
65 SELECT '0.1'::json; -- OK
71 SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8
77 SELECT '1e100'::json; -- OK
83 SELECT '1.3e100'::json; -- OK
89 SELECT '1f2'::json; -- ERROR
90 ERROR: invalid input syntax for type json
91 LINE 1: SELECT '1f2'::json;
93 DETAIL: Token "1f2" is invalid.
94 CONTEXT: JSON data, line 1: 1f2
95 SELECT '0.x1'::json; -- ERROR
96 ERROR: invalid input syntax for type json
97 LINE 1: SELECT '0.x1'::json;
99 DETAIL: Token "0.x1" is invalid.
100 CONTEXT: JSON data, line 1: 0.x1
101 SELECT '1.3ex100'::json; -- ERROR
102 ERROR: invalid input syntax for type json
103 LINE 1: SELECT '1.3ex100'::json;
105 DETAIL: Token "1.3ex100" is invalid.
106 CONTEXT: JSON data, line 1: 1.3ex100
108 SELECT '[]'::json; -- OK
114 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK
116 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
117 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
120 SELECT '[1,2]'::json; -- OK
126 SELECT '[1,2,]'::json; -- ERROR, trailing comma
127 ERROR: invalid input syntax for type json
128 LINE 1: SELECT '[1,2,]'::json;
130 DETAIL: Expected JSON value, but found "]".
131 CONTEXT: JSON data, line 1: [1,2,]
132 SELECT '[1,2'::json; -- ERROR, no closing bracket
133 ERROR: invalid input syntax for type json
134 LINE 1: SELECT '[1,2'::json;
136 DETAIL: The input string ended unexpectedly.
137 CONTEXT: JSON data, line 1: [1,2
138 SELECT '[1,[2]'::json; -- ERROR, no closing bracket
139 ERROR: invalid input syntax for type json
140 LINE 1: SELECT '[1,[2]'::json;
142 DETAIL: The input string ended unexpectedly.
143 CONTEXT: JSON data, line 1: [1,[2]
145 SELECT '{}'::json; -- OK
151 SELECT '{"abc"}'::json; -- ERROR, no value
152 ERROR: invalid input syntax for type json
153 LINE 1: SELECT '{"abc"}'::json;
155 DETAIL: Expected ":", but found "}".
156 CONTEXT: JSON data, line 1: {"abc"}
157 SELECT '{"abc":1}'::json; -- OK
163 SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
164 ERROR: invalid input syntax for type json
165 LINE 1: SELECT '{1:"abc"}'::json;
167 DETAIL: Expected string or "}", but found "1".
168 CONTEXT: JSON data, line 1: {1...
169 SELECT '{"abc",1}'::json; -- ERROR, wrong separator
170 ERROR: invalid input syntax for type json
171 LINE 1: SELECT '{"abc",1}'::json;
173 DETAIL: Expected ":", but found ",".
174 CONTEXT: JSON data, line 1: {"abc",...
175 SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
176 ERROR: invalid input syntax for type json
177 LINE 1: SELECT '{"abc"=1}'::json;
179 DETAIL: Token "=" is invalid.
180 CONTEXT: JSON data, line 1: {"abc"=...
181 SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
182 ERROR: invalid input syntax for type json
183 LINE 1: SELECT '{"abc"::1}'::json;
185 DETAIL: Expected JSON value, but found ":".
186 CONTEXT: JSON data, line 1: {"abc"::...
187 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
189 ---------------------------------------------------------
190 {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
193 SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
194 ERROR: invalid input syntax for type json
195 LINE 1: SELECT '{"abc":1:2}'::json;
197 DETAIL: Expected "," or "}", but found ":".
198 CONTEXT: JSON data, line 1: {"abc":1:...
199 SELECT '{"abc":1,3}'::json; -- ERROR, no value
200 ERROR: invalid input syntax for type json
201 LINE 1: SELECT '{"abc":1,3}'::json;
203 DETAIL: Expected string, but found "3".
204 CONTEXT: JSON data, line 1: {"abc":1,3...
206 SET max_stack_depth = '100kB';
207 SELECT repeat('[', 10000)::json;
208 ERROR: stack depth limit exceeded
209 HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
210 SELECT repeat('{"a":', 10000)::json;
211 ERROR: stack depth limit exceeded
212 HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
213 RESET max_stack_depth;
214 -- Miscellaneous stuff.
215 SELECT 'true'::json; -- OK
221 SELECT 'false'::json; -- OK
227 SELECT 'null'::json; -- OK
233 SELECT ' true '::json; -- OK, even with extra whitespace
239 SELECT 'true false'::json; -- ERROR, too many values
240 ERROR: invalid input syntax for type json
241 LINE 1: SELECT 'true false'::json;
243 DETAIL: Expected end of input, but found "false".
244 CONTEXT: JSON data, line 1: true false
245 SELECT 'true, false'::json; -- ERROR, too many values
246 ERROR: invalid input syntax for type json
247 LINE 1: SELECT 'true, false'::json;
249 DETAIL: Expected end of input, but found ",".
250 CONTEXT: JSON data, line 1: true,...
251 SELECT 'truf'::json; -- ERROR, not a keyword
252 ERROR: invalid input syntax for type json
253 LINE 1: SELECT 'truf'::json;
255 DETAIL: Token "truf" is invalid.
256 CONTEXT: JSON data, line 1: truf
257 SELECT 'trues'::json; -- ERROR, not a keyword
258 ERROR: invalid input syntax for type json
259 LINE 1: SELECT 'trues'::json;
261 DETAIL: Token "trues" is invalid.
262 CONTEXT: JSON data, line 1: trues
263 SELECT ''::json; -- ERROR, no value
264 ERROR: invalid input syntax for type json
265 LINE 1: SELECT ''::json;
267 DETAIL: The input string ended unexpectedly.
268 CONTEXT: JSON data, line 1:
269 SELECT ' '::json; -- ERROR, no value
270 ERROR: invalid input syntax for type json
271 LINE 1: SELECT ' '::json;
273 DETAIL: The input string ended unexpectedly.
274 CONTEXT: JSON data, line 1:
277 SELECT array_to_json(array(select 1 as a));
283 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
285 ---------------------------------------------
286 [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
289 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
297 SELECT array_to_json(array_agg(q),false)
298 FROM ( SELECT $$a$$ || x AS b, y AS c,
299 ARRAY[ROW(x.*,ARRAY[1,2,3]),
300 ROW(y.*,ARRAY[4,5,6])] AS z
301 FROM generate_series(1,2) x,
302 generate_series(4,5) y) q;
304 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
305 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
308 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
314 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
321 SELECT row_to_json(row(1,'foo'));
323 ---------------------
327 SELECT row_to_json(q)
328 FROM (SELECT $$a$$ || x AS b,
330 ARRAY[ROW(x.*,ARRAY[1,2,3]),
331 ROW(y.*,ARRAY[4,5,6])] AS z
332 FROM generate_series(1,2) x,
333 generate_series(4,5) y) q;
335 --------------------------------------------------------------------
336 {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
337 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
338 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
339 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
342 SELECT row_to_json(q,true)
343 FROM (SELECT $$a$$ || x AS b,
345 ARRAY[ROW(x.*,ARRAY[1,2,3]),
346 ROW(y.*,ARRAY[4,5,6])] AS z
347 FROM generate_series(1,2) x,
348 generate_series(4,5) y) q;
350 -----------------------------------------------------
353 "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
356 "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
359 "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
362 "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
365 CREATE TEMP TABLE rows AS
366 SELECT x, 'txt' || x as y
367 FROM generate_series(1,3) AS x;
368 SELECT row_to_json(q,true)
380 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
382 -----------------------
383 {"f1":[5,6,7,8,9,10]}
387 select to_json(histogram_bounds) histogram_bounds
389 where attname = 'tmplname' and tablename = 'pg_pltemplate';
391 ---------------------------------------------------------------------------------------
392 ["plperl","plperlu","plpgsql","plpython2u","plpython3u","plpythonu","pltcl","pltclu"]
395 -- to_json, timestamps
396 select to_json(timestamp '2014-05-28 12:22:35.614298');
398 ------------------------------
399 "2014-05-28T12:22:35.614298"
403 SET LOCAL TIME ZONE 10.5;
404 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
406 ------------------------------------
407 "2014-05-29T02:52:35.614298+10:30"
410 SET LOCAL TIME ZONE -8;
411 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
413 ------------------------------------
414 "2014-05-28T08:22:35.614298-08:00"
418 select to_json(date '2014-05-28');
424 select to_json(date 'Infinity');
430 select to_json(date '-Infinity');
436 select to_json(timestamp 'Infinity');
442 select to_json(timestamp '-Infinity');
448 select to_json(timestamptz 'Infinity');
454 select to_json(timestamptz '-Infinity');
462 FROM ( SELECT $$a$$ || x AS b, y AS c,
463 ARRAY[ROW(x.*,ARRAY[1,2,3]),
464 ROW(y.*,ARRAY[4,5,6])] AS z
465 FROM generate_series(1,2) x,
466 generate_series(4,5) y) q;
468 -----------------------------------------------------------------------
469 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
470 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
471 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
472 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
475 SELECT json_agg(q ORDER BY x, y)
478 -----------------------
479 [{"x":1,"y":"txt1"}, +
480 {"x":2,"y":"txt2"}, +
484 UPDATE rows SET x = NULL WHERE x = 1;
485 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
488 --------------------------
489 [{"x":null,"y":"txt1"}, +
490 {"x":2,"y":"txt2"}, +
494 -- non-numeric output
495 SELECT row_to_json(q)
496 FROM (SELECT 'NaN'::float8 AS "float8field") q;
498 -----------------------
499 {"float8field":"NaN"}
502 SELECT row_to_json(q)
503 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
505 ----------------------------
506 {"float8field":"Infinity"}
509 SELECT row_to_json(q)
510 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
512 -----------------------------
513 {"float8field":"-Infinity"}
517 SELECT row_to_json(q)
518 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
520 ------------------------------------------------------------------
521 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
524 -- json extraction functions
525 CREATE TEMP TABLE test_json (
529 INSERT INTO test_json VALUES
530 ('scalar','"a scalar"'),
531 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
532 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
533 SELECT test_json -> 'x'
535 WHERE json_type = 'scalar';
541 SELECT test_json -> 'x'
543 WHERE json_type = 'array';
549 SELECT test_json -> 'x'
551 WHERE json_type = 'object';
557 SELECT test_json->'field2'
559 WHERE json_type = 'object';
565 SELECT test_json->>'field2'
567 WHERE json_type = 'object';
573 SELECT test_json -> 2
575 WHERE json_type = 'scalar';
581 SELECT test_json -> 2
583 WHERE json_type = 'array';
589 SELECT test_json -> -1
591 WHERE json_type = 'array';
597 SELECT test_json -> 2
599 WHERE json_type = 'object';
607 WHERE json_type = 'array';
613 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
619 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
625 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
631 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
637 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
643 SELECT json_object_keys(test_json)
645 WHERE json_type = 'scalar';
646 ERROR: cannot call json_object_keys on a scalar
647 SELECT json_object_keys(test_json)
649 WHERE json_type = 'array';
650 ERROR: cannot call json_object_keys on an array
651 SELECT json_object_keys(test_json)
653 WHERE json_type = 'object';
664 -- test extending object_keys resultset - initial resultset size is 256
666 (select json_object_keys(json_object(array_agg(g)))
667 from (select unnest(array['f'||n,n::text])as g
668 from generate_series(1,300) as n) x ) y;
675 select (test_json->'field3') is null as expect_false
677 where json_type = 'object';
683 select (test_json->>'field3') is null as expect_true
685 where json_type = 'object';
691 select (test_json->3) is null as expect_false
693 where json_type = 'array';
699 select (test_json->>3) is null as expect_true
701 where json_type = 'array';
708 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
714 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
720 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
726 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
732 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
738 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
744 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
750 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
756 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
762 select '{"a": "c", "b": null}'::json -> 'b';
768 select '"foo"'::json -> 1;
774 select '"foo"'::json -> 'z';
780 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
786 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
792 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
798 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
804 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
810 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
816 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
822 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
828 select '{"a": "c", "b": null}'::json ->> 'b';
834 select '"foo"'::json ->> 1;
840 select '"foo"'::json ->> 'z';
847 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
853 SELECT json_array_length('[]');
859 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
860 ERROR: cannot get array length of a non-array
861 SELECT json_array_length('4');
862 ERROR: cannot get array length of a scalar
864 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
872 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
882 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
891 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
901 -- extract_path, extract_path_as_text
902 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
908 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
914 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
920 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
926 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
927 json_extract_path_text
928 ------------------------
932 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
933 json_extract_path_text
934 ------------------------
938 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
939 json_extract_path_text
940 ------------------------
944 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
945 json_extract_path_text
946 ------------------------
950 -- extract_path nulls
951 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
957 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
963 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
969 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
975 -- extract_path operators
976 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
982 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
988 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
994 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
1000 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
1006 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
1012 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1018 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1024 -- corner cases for same
1025 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1027 ---------------------------
1028 {"a": {"b":{"c": "foo"}}}
1031 select '[1,2,3]'::json #> '{}';
1037 select '"foo"'::json #> '{}';
1043 select '42'::json #> '{}';
1049 select 'null'::json #> '{}';
1055 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1057 --------------------
1061 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1067 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1073 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1079 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1085 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1091 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1097 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1103 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1109 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1115 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1121 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1127 select '"foo"'::json #> array['z'];
1133 select '42'::json #> array['f2'];
1139 select '42'::json #> array['0'];
1145 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1147 ---------------------------
1148 {"a": {"b":{"c": "foo"}}}
1151 select '[1,2,3]'::json #>> '{}';
1157 select '"foo"'::json #>> '{}';
1163 select '42'::json #>> '{}';
1169 select 'null'::json #>> '{}';
1175 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1177 --------------------
1181 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1187 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1193 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1199 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1205 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1211 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1217 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1223 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1229 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1235 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1241 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1247 select '"foo"'::json #>> array['z'];
1253 select '42'::json #>> array['f2'];
1259 select '42'::json #>> array['0'];
1266 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1268 -----------------------
1273 {"f1":1,"f2":[7,8,9]}
1278 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1280 -----------------------
1285 {"f1":1,"f2":[7,8,9]}
1290 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1291 json_array_elements_text
1292 --------------------------
1297 {"f1":1,"f2":[7,8,9]}
1302 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1304 -----------------------
1309 {"f1":1,"f2":[7,8,9]}
1315 create type jpop as (a text, b int, c timestamp);
1316 CREATE DOMAIN js_int_not_null AS int NOT NULL;
1317 CREATE DOMAIN js_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
1318 CREATE DOMAIN js_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
1319 CREATE TYPE jsrec AS (
1325 ia1d js_int_array_1d,
1326 ia2d js_int_array_2d,
1338 CREATE TYPE jsrec_i_not_null AS (
1341 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1347 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1349 --------+---+--------------------------
1350 blurfl | 3 | Mon Dec 31 15:30:56 2012
1353 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1359 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1361 --------+---+--------------------------
1362 blurfl | 3 | Mon Dec 31 15:30:56 2012
1365 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1367 -----------------+---+---
1371 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1373 -----------------+---+--------------------------
1374 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1377 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1378 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1379 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q;
1381 ---+---+--------------------------
1382 x | 3 | Mon Dec 31 15:30:56 2012
1385 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q;
1386 ERROR: domain js_int_not_null does not allow null values
1387 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q;
1388 ERROR: domain js_int_not_null does not allow null values
1389 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q;
1395 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q;
1401 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q;
1402 ERROR: expected json array
1403 HINT: see the value of key "ia"
1404 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q;
1410 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q;
1416 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q;
1417 ERROR: expected json array
1418 HINT: see the array element [1] of key "ia"
1419 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q;
1420 ERROR: malformed json array
1421 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
1422 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q;
1428 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q;
1434 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q;
1435 ERROR: expected json array
1436 HINT: see the value of key "ia1"
1437 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q;
1443 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q;
1449 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q;
1455 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q;
1456 ERROR: expected json array
1457 HINT: see the value of key "ia1d"
1458 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q;
1459 ERROR: value for domain js_int_array_1d violates check constraint "js_int_array_1d_check"
1460 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q;
1466 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q;
1472 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q;
1478 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q;
1484 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q;
1485 ERROR: malformed json array
1486 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
1487 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q;
1488 ERROR: expected json array
1489 HINT: see the array element [1] of key "ia2"
1490 SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
1491 ERROR: value for domain js_int_array_2d violates check constraint "js_int_array_2d_check"
1492 SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
1494 ----------------------
1495 {{1,2,3},{NULL,5,6}}
1498 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q;
1504 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q;
1510 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
1516 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
1522 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
1524 -------------------------------
1525 {{{1,2},{3,4}},{{5,6},{7,8}}}
1528 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
1529 ERROR: malformed json array
1530 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
1531 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q;
1537 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q;
1538 ERROR: expected json array
1539 HINT: see the value of key "ta"
1540 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q;
1546 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
1547 ERROR: expected json array
1548 HINT: see the array element [1] of key "ta"
1549 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q;
1555 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q;
1561 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q;
1567 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q;
1568 ERROR: value too long for type character(10)
1569 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q;
1575 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q;
1576 ERROR: expected json array
1577 HINT: see the value of key "ca"
1578 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q;
1580 -----------------------------------------------
1581 {"1 ","2 ",NULL,"4 "}
1584 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
1585 ERROR: value too long for type character(10)
1586 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
1587 ERROR: expected json array
1588 HINT: see the array element [1] of key "ca"
1589 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q;
1595 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q;
1601 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q;
1607 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q;
1613 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q;
1619 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
1621 --------------------------------------
1622 [123, "123", null, {"key": "value"}]
1625 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
1627 --------------------------------------
1628 {"a": "bbb", "b": null, "c": 123.45}
1631 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q;
1637 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q;
1643 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q;
1649 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q;
1655 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q;
1661 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
1663 --------------------------------------
1664 [123, "123", null, {"key": "value"}]
1667 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
1669 --------------------------------------
1670 {"a": "bbb", "b": null, "c": 123.45}
1673 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q;
1679 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q;
1680 ERROR: expected json array
1681 HINT: see the value of key "jsa"
1682 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q;
1684 --------------------
1688 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
1690 ----------------------------------------------------------
1691 {"\"aaa\"",NULL,"[1, 2, \"3\", {}]","{ \"k\" : \"v\" }"}
1694 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q;
1695 ERROR: cannot call populate_composite on a scalar
1696 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q;
1697 ERROR: cannot call populate_composite on an array
1698 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
1700 -----------------------------------
1701 (abc,,"Thu Jan 02 00:00:00 2003")
1704 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q;
1706 -------------------------------------
1707 (abc,42,"Thu Jan 02 00:00:00 2003")
1710 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q;
1711 ERROR: expected json array
1712 HINT: see the value of key "reca"
1713 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q;
1714 ERROR: cannot call populate_composite on a scalar
1715 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
1717 --------------------------------------------------------
1718 {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1721 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
1723 -------------------------------------------
1724 {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
1727 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
1729 -------------------------------------------
1730 {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
1733 SELECT rec FROM json_populate_record(
1734 row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
1735 row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec,
1736 '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
1739 ------------------------------------
1740 (abc,3,"Thu Jan 02 00:00:00 2003")
1743 -- populate_recordset
1744 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1746 --------+---+--------------------------
1748 | 3 | Fri Jan 20 10:42:53 2012
1751 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1753 --------+----+--------------------------
1755 def | 3 | Fri Jan 20 10:42:53 2012
1758 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1760 --------+---+--------------------------
1762 | 3 | Fri Jan 20 10:42:53 2012
1765 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1767 --------+----+--------------------------
1769 def | 3 | Fri Jan 20 10:42:53 2012
1772 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1774 ---------------+----+--------------------------
1775 [100,200,300] | 99 |
1776 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1779 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1780 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1781 create type jpop2 as (a int, b json, c int, d int);
1782 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1784 ---+---------+---+---
1788 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1790 --------+---+--------------------------
1792 | 3 | Fri Jan 20 10:42:53 2012
1795 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1797 --------+----+--------------------------
1799 def | 3 | Fri Jan 20 10:42:53 2012
1802 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1804 ---------------+----+--------------------------
1805 [100,200,300] | 99 |
1806 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1809 -- test type info caching in json_populate_record()
1810 CREATE TEMP TABLE jspoptest (js json);
1811 INSERT INTO jspoptest
1813 "jsa": [1, "2", null, 4],
1814 "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
1815 "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
1817 FROM generate_series(1, 3);
1818 SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest;
1819 i | ia | ia1 | ia2 | ia3 | ia1d | ia2d | t | ta | c | ca | ts | js | jsb | jsa | rec | reca
1820 ---+----+-----+-----+-----+------+------+---+----+---+----+----+----+-----+--------------------+-----------------------------------+--------------------------------------------------------
1821 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1822 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1823 | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1827 DROP TYPE jsrec_i_not_null;
1828 DROP DOMAIN js_int_not_null;
1829 DROP DOMAIN js_int_array_1d;
1830 DROP DOMAIN js_int_array_2d;
1831 --json_typeof() function
1832 select value, json_typeof(value)
1833 from (values (json '123.4'),
1841 (json '{"x":"foo", "y":123}'),
1846 ----------------------+-------------
1855 {"x":"foo", "y":123} | object
1860 -- json_build_array, json_build_object, json_object_agg
1861 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1863 -----------------------------------------------------------------------
1864 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1867 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1869 ----------------------------------------------------------------------------
1870 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1873 SELECT json_build_object(
1874 'a', json_build_object('b',false,'c',99),
1875 'd', json_build_object('e',array[9,8,7]::int[],
1876 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1878 -------------------------------------------------------------------------------------------------
1879 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1882 -- empty objects/arrays
1883 SELECT json_build_array();
1889 SELECT json_build_object();
1895 -- make sure keys are quoted
1896 SELECT json_build_object(1,2);
1902 -- keys must be scalar and not null
1903 SELECT json_build_object(null,2);
1904 ERROR: argument 1 cannot be null
1905 HINT: Object keys should be text.
1906 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1907 ERROR: key value must be scalar, not array, composite, or json
1908 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1909 ERROR: key value must be scalar, not array, composite, or json
1910 SELECT json_build_object('{1,2,3}'::int[], 3);
1911 ERROR: key value must be scalar, not array, composite, or json
1912 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1913 INSERT INTO foo VALUES (847001,'t15','GE1043');
1914 INSERT INTO foo VALUES (847002,'t16','GE1043');
1915 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1916 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1919 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1920 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1923 SELECT json_object_agg(name, type) FROM foo;
1925 ----------------------------------------------------------------
1926 { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1929 INSERT INTO foo VALUES (999999, NULL, 'bar');
1930 SELECT json_object_agg(name, type) FROM foo;
1931 ERROR: field name must not be null
1933 -- empty object, one dimension
1934 SELECT json_object('{}');
1940 -- empty object, two dimensions
1941 SELECT json_object('{}', '{}');
1948 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1950 -------------------------------------------------------
1951 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1954 -- same but with two dimensions
1955 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1957 -------------------------------------------------------
1958 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1962 SELECT json_object('{a,b,c}');
1963 ERROR: array must have even number of elements
1965 SELECT json_object('{{a},{b}}');
1966 ERROR: array must have two columns
1967 -- too many columns error
1968 SELECT json_object('{{a,b,c},{b,c,d}}');
1969 ERROR: array must have two columns
1970 -- too many dimensions error
1971 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1972 ERROR: wrong number of array subscripts
1973 --two argument form of json_object
1974 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1976 ------------------------------------------------------
1977 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1980 -- too many dimensions
1981 SELECT 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"}}');
1982 ERROR: wrong number of array subscripts
1983 -- mismatched dimensions
1984 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1985 ERROR: mismatched array dimensions
1986 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1987 ERROR: mismatched array dimensions
1989 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1990 ERROR: null value not allowed for object key
1991 -- empty key is allowed
1992 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1994 -----------------------------------------------------
1995 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1998 -- json_to_record and json_to_recordset
1999 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
2000 as x(a int, b text, d text);
2006 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
2007 as x(a int, b text, c boolean);
2014 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
2015 as x(a int, b json, c boolean);
2017 ---+-------------+---
2022 select *, c is null as c_is_null
2023 from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::json)
2024 as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop);
2025 a | b | c | x | ca | ia | r | c_is_null
2026 ---+-----------------+---+---+-------------------+---------------+------------+-----------
2027 1 | {"c":16, "d":2} | | 8 | {"1 2 ","3 "} | {{1,2},{3,4}} | (aaa,123,) | t
2030 select *, c is null as c_is_null
2031 from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
2032 as t(a int, b json, c text, x int);
2033 a | b | c | x | c_is_null
2034 ---+-----------------+---+---+-----------
2035 1 | {"c":16, "d":2} | | 8 | t
2038 select * from json_to_record('{"ia": null}') as x(ia _int4);
2044 select * from json_to_record('{"ia": 123}') as x(ia _int4);
2045 ERROR: expected json array
2046 HINT: see the value of key "ia"
2047 select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
2053 select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
2059 select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4);
2060 ERROR: expected json array
2061 HINT: see the array element [1] of key "ia"
2062 select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
2063 ERROR: malformed json array
2064 DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
2065 select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
2071 select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
2077 select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
2084 select json_strip_nulls(null);
2090 select json_strip_nulls('1');
2096 select json_strip_nulls('"a string"');
2102 select json_strip_nulls('null');
2108 select json_strip_nulls('[1,2,null,3,4]');
2114 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
2116 ------------------------------------
2117 {"a":1,"c":[2,null,3],"d":{"e":4}}
2120 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
2122 ---------------------
2126 -- an empty object is not null and should not be stripped
2127 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
2134 select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
2136 ---------------------------------------------------------------------------
2137 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
2140 -- json to tsvector with config
2141 select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
2143 ---------------------------------------------------------------------------
2144 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
2147 -- json to tsvector with stop words
2148 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
2150 ----------------------------------------------------------------------------
2151 'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
2154 -- ts_vector corner cases
2155 select to_tsvector('""'::json);
2161 select to_tsvector('{}'::json);
2167 select to_tsvector('[]'::json);
2173 select to_tsvector('null'::json);
2179 -- ts_headline for json
2180 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
2182 ---------------------------------------------------------------------------------------------------------
2183 {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff","c1":"ccc1 ddd1"},"d":["ggg <b>hhh</b>","iii jjj"]}
2186 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
2188 ----------------------------------------------------------------------------------------
2189 {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff"},"d":["ggg <b>hhh</b>","iii jjj"]}
2192 select 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 = >');
2194 ------------------------------------------------------------------------------------------
2195 {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
2198 select 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 = >');
2200 ------------------------------------------------------------------------------------------
2201 {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
2204 -- corner cases for ts_headline with json
2205 select ts_headline('null'::json, tsquery('aaa & bbb'));
2211 select ts_headline('{}'::json, tsquery('aaa & bbb'));
2217 select ts_headline('[]'::json, tsquery('aaa & bbb'));