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 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1322 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1324 --------+---+--------------------------
1325 blurfl | 3 | Mon Dec 31 15:30:56 2012
1328 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1334 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1336 --------+---+--------------------------
1337 blurfl | 3 | Mon Dec 31 15:30:56 2012
1340 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1342 -----------------+---+---
1346 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1348 -----------------+---+--------------------------
1349 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1352 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1353 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1354 -- populate_recordset
1355 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1357 --------+---+--------------------------
1359 | 3 | Fri Jan 20 10:42:53 2012
1362 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;
1364 --------+----+--------------------------
1366 def | 3 | Fri Jan 20 10:42:53 2012
1369 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1371 --------+---+--------------------------
1373 | 3 | Fri Jan 20 10:42:53 2012
1376 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;
1378 --------+----+--------------------------
1380 def | 3 | Fri Jan 20 10:42:53 2012
1383 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;
1385 ---------------+----+--------------------------
1386 [100,200,300] | 99 |
1387 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1390 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;
1391 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1392 create type jpop2 as (a int, b json, c int, d int);
1393 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1395 ---+---------+---+---
1399 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1401 --------+---+--------------------------
1403 | 3 | Fri Jan 20 10:42:53 2012
1406 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;
1408 --------+----+--------------------------
1410 def | 3 | Fri Jan 20 10:42:53 2012
1413 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;
1415 ---------------+----+--------------------------
1416 [100,200,300] | 99 |
1417 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1420 --json_typeof() function
1421 select value, json_typeof(value)
1422 from (values (json '123.4'),
1430 (json '{"x":"foo", "y":123}'),
1435 ----------------------+-------------
1444 {"x":"foo", "y":123} | object
1449 -- json_build_array, json_build_object, json_object_agg
1450 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1452 -----------------------------------------------------------------------
1453 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1456 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1458 ----------------------------------------------------------------------------
1459 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1462 SELECT json_build_object(
1463 'a', json_build_object('b',false,'c',99),
1464 'd', json_build_object('e',array[9,8,7]::int[],
1465 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1467 -------------------------------------------------------------------------------------------------
1468 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1471 -- empty objects/arrays
1472 SELECT json_build_array();
1478 SELECT json_build_object();
1484 -- make sure keys are quoted
1485 SELECT json_build_object(1,2);
1491 -- keys must be scalar and not null
1492 SELECT json_build_object(null,2);
1493 ERROR: argument 1 cannot be null
1494 HINT: Object keys should be text.
1495 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1496 ERROR: key value must be scalar, not array, composite, or json
1497 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1498 ERROR: key value must be scalar, not array, composite, or json
1499 SELECT json_build_object('{1,2,3}'::int[], 3);
1500 ERROR: key value must be scalar, not array, composite, or json
1501 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1502 INSERT INTO foo VALUES (847001,'t15','GE1043');
1503 INSERT INTO foo VALUES (847002,'t16','GE1043');
1504 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1505 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1508 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1509 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1512 SELECT json_object_agg(name, type) FROM foo;
1514 ----------------------------------------------------------------
1515 { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1518 INSERT INTO foo VALUES (999999, NULL, 'bar');
1519 SELECT json_object_agg(name, type) FROM foo;
1520 ERROR: field name must not be null
1522 -- empty object, one dimension
1523 SELECT json_object('{}');
1529 -- empty object, two dimensions
1530 SELECT json_object('{}', '{}');
1537 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1539 -------------------------------------------------------
1540 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1543 -- same but with two dimensions
1544 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1546 -------------------------------------------------------
1547 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1551 SELECT json_object('{a,b,c}');
1552 ERROR: array must have even number of elements
1554 SELECT json_object('{{a},{b}}');
1555 ERROR: array must have two columns
1556 -- too many columns error
1557 SELECT json_object('{{a,b,c},{b,c,d}}');
1558 ERROR: array must have two columns
1559 -- too many dimensions error
1560 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1561 ERROR: wrong number of array subscripts
1562 --two argument form of json_object
1563 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1565 ------------------------------------------------------
1566 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1569 -- too many dimensions
1570 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"}}');
1571 ERROR: wrong number of array subscripts
1572 -- mismatched dimensions
1573 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1574 ERROR: mismatched array dimensions
1575 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1576 ERROR: mismatched array dimensions
1578 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1579 ERROR: null value not allowed for object key
1580 -- empty key is allowed
1581 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1583 -----------------------------------------------------
1584 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1587 -- json_to_record and json_to_recordset
1588 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1589 as x(a int, b text, d text);
1595 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1596 as x(a int, b text, c boolean);
1603 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1604 as x(a int, b json, c boolean);
1606 ---+-------------+---
1611 select *, c is null as c_is_null
1612 from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
1613 as t(a int, b json, c text, x int);
1614 a | b | c | x | c_is_null
1615 ---+-----------------+---+---+-----------
1616 1 | {"c":16, "d":2} | | 8 | t
1619 select *, c is null as c_is_null
1620 from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
1621 as t(a int, b json, c text, x int);
1622 a | b | c | x | c_is_null
1623 ---+-----------------+---+---+-----------
1624 1 | {"c":16, "d":2} | | 8 | t
1628 select json_strip_nulls(null);
1634 select json_strip_nulls('1');
1640 select json_strip_nulls('"a string"');
1646 select json_strip_nulls('null');
1652 select json_strip_nulls('[1,2,null,3,4]');
1658 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1660 ------------------------------------
1661 {"a":1,"c":[2,null,3],"d":{"e":4}}
1664 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1666 ---------------------
1670 -- an empty object is not null and should not be stripped
1671 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
1678 select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
1680 ---------------------------------------------------------------------------
1681 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
1684 -- json to tsvector with config
1685 select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
1687 ---------------------------------------------------------------------------
1688 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
1691 -- json to tsvector with stop words
1692 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
1694 ----------------------------------------------------------------------------
1695 'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
1698 -- ts_vector corner cases
1699 select to_tsvector('""'::json);
1705 select to_tsvector('{}'::json);
1711 select to_tsvector('[]'::json);
1717 select to_tsvector('null'::json);
1723 -- ts_headline for json
1724 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
1726 ---------------------------------------------------------------------------------------------------------
1727 {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff","c1":"ccc1 ddd1"},"d":["ggg <b>hhh</b>","iii jjj"]}
1730 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
1732 ----------------------------------------------------------------------------------------
1733 {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff"},"d":["ggg <b>hhh</b>","iii jjj"]}
1736 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 = >');
1738 ------------------------------------------------------------------------------------------
1739 {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
1742 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 = >');
1744 ------------------------------------------------------------------------------------------
1745 {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
1748 -- corner cases for ts_headline with json
1749 select ts_headline('null'::json, tsquery('aaa & bbb'));
1755 select ts_headline('{}'::json, tsquery('aaa & bbb'));
1761 select ts_headline('[]'::json, tsquery('aaa & bbb'));