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]}
386 -- to_json, timestamps
387 select to_json(timestamp '2014-05-28 12:22:35.614298');
389 ------------------------------
390 "2014-05-28T12:22:35.614298"
394 SET LOCAL TIME ZONE 10.5;
395 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
397 ------------------------------------
398 "2014-05-29T02:52:35.614298+10:30"
401 SET LOCAL TIME ZONE -8;
402 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
404 ------------------------------------
405 "2014-05-28T08:22:35.614298-08:00"
409 select to_json(date '2014-05-28');
415 select to_json(date 'Infinity');
421 select to_json(date '-Infinity');
427 select to_json(timestamp 'Infinity');
433 select to_json(timestamp '-Infinity');
439 select to_json(timestamptz 'Infinity');
445 select to_json(timestamptz '-Infinity');
453 FROM ( SELECT $$a$$ || x AS b, y AS c,
454 ARRAY[ROW(x.*,ARRAY[1,2,3]),
455 ROW(y.*,ARRAY[4,5,6])] AS z
456 FROM generate_series(1,2) x,
457 generate_series(4,5) y) q;
459 -----------------------------------------------------------------------
460 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
461 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
462 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
463 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
466 SELECT json_agg(q ORDER BY x, y)
469 -----------------------
470 [{"x":1,"y":"txt1"}, +
471 {"x":2,"y":"txt2"}, +
475 UPDATE rows SET x = NULL WHERE x = 1;
476 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
479 --------------------------
480 [{"x":null,"y":"txt1"}, +
481 {"x":2,"y":"txt2"}, +
485 -- non-numeric output
486 SELECT row_to_json(q)
487 FROM (SELECT 'NaN'::float8 AS "float8field") q;
489 -----------------------
490 {"float8field":"NaN"}
493 SELECT row_to_json(q)
494 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
496 ----------------------------
497 {"float8field":"Infinity"}
500 SELECT row_to_json(q)
501 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
503 -----------------------------
504 {"float8field":"-Infinity"}
508 SELECT row_to_json(q)
509 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
511 ------------------------------------------------------------------
512 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
515 -- json extraction functions
516 CREATE TEMP TABLE test_json (
520 INSERT INTO test_json VALUES
521 ('scalar','"a scalar"'),
522 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
523 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
524 SELECT test_json -> 'x'
526 WHERE json_type = 'scalar';
532 SELECT test_json -> 'x'
534 WHERE json_type = 'array';
540 SELECT test_json -> 'x'
542 WHERE json_type = 'object';
548 SELECT test_json->'field2'
550 WHERE json_type = 'object';
556 SELECT test_json->>'field2'
558 WHERE json_type = 'object';
564 SELECT test_json -> 2
566 WHERE json_type = 'scalar';
572 SELECT test_json -> 2
574 WHERE json_type = 'array';
580 SELECT test_json -> -1
582 WHERE json_type = 'array';
588 SELECT test_json -> 2
590 WHERE json_type = 'object';
598 WHERE json_type = 'array';
604 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
610 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
616 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
622 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
628 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
634 SELECT json_object_keys(test_json)
636 WHERE json_type = 'scalar';
637 ERROR: cannot call json_object_keys on a scalar
638 SELECT json_object_keys(test_json)
640 WHERE json_type = 'array';
641 ERROR: cannot call json_object_keys on an array
642 SELECT json_object_keys(test_json)
644 WHERE json_type = 'object';
655 -- test extending object_keys resultset - initial resultset size is 256
657 (select json_object_keys(json_object(array_agg(g)))
658 from (select unnest(array['f'||n,n::text])as g
659 from generate_series(1,300) as n) x ) y;
666 select (test_json->'field3') is null as expect_false
668 where json_type = 'object';
674 select (test_json->>'field3') is null as expect_true
676 where json_type = 'object';
682 select (test_json->3) is null as expect_false
684 where json_type = 'array';
690 select (test_json->>3) is null as expect_true
692 where json_type = 'array';
699 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
705 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
711 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
717 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
723 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
729 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
735 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
741 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
747 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
753 select '{"a": "c", "b": null}'::json -> 'b';
759 select '"foo"'::json -> 1;
765 select '"foo"'::json -> 'z';
771 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
777 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
783 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
789 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
795 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
801 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
807 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
813 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
819 select '{"a": "c", "b": null}'::json ->> 'b';
825 select '"foo"'::json ->> 1;
831 select '"foo"'::json ->> 'z';
838 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
844 SELECT json_array_length('[]');
850 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
851 ERROR: cannot get array length of a non-array
852 SELECT json_array_length('4');
853 ERROR: cannot get array length of a scalar
855 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
863 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
873 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
882 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
892 -- extract_path, extract_path_as_text
893 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
899 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
905 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
911 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
917 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
918 json_extract_path_text
919 ------------------------
923 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
924 json_extract_path_text
925 ------------------------
929 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
930 json_extract_path_text
931 ------------------------
935 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
936 json_extract_path_text
937 ------------------------
941 -- extract_path nulls
942 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
948 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
954 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
960 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
966 -- extract_path operators
967 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
973 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
979 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
985 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
991 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
997 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
1003 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1009 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1015 -- corner cases for same
1016 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1018 ---------------------------
1019 {"a": {"b":{"c": "foo"}}}
1022 select '[1,2,3]'::json #> '{}';
1028 select '"foo"'::json #> '{}';
1034 select '42'::json #> '{}';
1040 select 'null'::json #> '{}';
1046 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1048 --------------------
1052 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1058 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1064 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1070 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1076 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1082 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1088 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1094 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1100 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1106 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1112 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1118 select '"foo"'::json #> array['z'];
1124 select '42'::json #> array['f2'];
1130 select '42'::json #> array['0'];
1136 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1138 ---------------------------
1139 {"a": {"b":{"c": "foo"}}}
1142 select '[1,2,3]'::json #>> '{}';
1148 select '"foo"'::json #>> '{}';
1154 select '42'::json #>> '{}';
1160 select 'null'::json #>> '{}';
1166 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1168 --------------------
1172 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1178 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1184 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1190 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1196 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1202 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1208 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1214 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1220 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1226 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1232 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1238 select '"foo"'::json #>> array['z'];
1244 select '42'::json #>> array['f2'];
1250 select '42'::json #>> array['0'];
1257 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1259 -----------------------
1264 {"f1":1,"f2":[7,8,9]}
1269 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1271 -----------------------
1276 {"f1":1,"f2":[7,8,9]}
1281 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1282 json_array_elements_text
1283 --------------------------
1288 {"f1":1,"f2":[7,8,9]}
1293 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1295 -----------------------
1300 {"f1":1,"f2":[7,8,9]}
1306 create type jpop as (a text, b int, c timestamp);
1307 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1313 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1315 --------+---+--------------------------
1316 blurfl | 3 | Mon Dec 31 15:30:56 2012
1319 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1325 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1327 --------+---+--------------------------
1328 blurfl | 3 | Mon Dec 31 15:30:56 2012
1331 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1333 -----------------+---+---
1337 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1339 -----------------+---+--------------------------
1340 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1343 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1344 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1345 -- populate_recordset
1346 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1348 --------+---+--------------------------
1350 | 3 | Fri Jan 20 10:42:53 2012
1353 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;
1355 --------+----+--------------------------
1357 def | 3 | Fri Jan 20 10:42:53 2012
1360 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1362 --------+---+--------------------------
1364 | 3 | Fri Jan 20 10:42:53 2012
1367 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;
1369 --------+----+--------------------------
1371 def | 3 | Fri Jan 20 10:42:53 2012
1374 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;
1376 ---------------+----+--------------------------
1377 [100,200,300] | 99 |
1378 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1381 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;
1382 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1383 create type jpop2 as (a int, b json, c int, d int);
1384 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1386 ---+---------+---+---
1390 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1392 --------+---+--------------------------
1394 | 3 | Fri Jan 20 10:42:53 2012
1397 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;
1399 --------+----+--------------------------
1401 def | 3 | Fri Jan 20 10:42:53 2012
1404 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;
1406 ---------------+----+--------------------------
1407 [100,200,300] | 99 |
1408 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1411 --json_typeof() function
1412 select value, json_typeof(value)
1413 from (values (json '123.4'),
1421 (json '{"x":"foo", "y":123}'),
1426 ----------------------+-------------
1435 {"x":"foo", "y":123} | object
1440 -- json_build_array, json_build_object, json_object_agg
1441 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1443 -----------------------------------------------------------------------
1444 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1447 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1449 ----------------------------------------------------------------------------
1450 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1453 SELECT json_build_object(
1454 'a', json_build_object('b',false,'c',99),
1455 'd', json_build_object('e',array[9,8,7]::int[],
1456 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1458 -------------------------------------------------------------------------------------------------
1459 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1462 -- empty objects/arrays
1463 SELECT json_build_array();
1469 SELECT json_build_object();
1475 -- make sure keys are quoted
1476 SELECT json_build_object(1,2);
1482 -- keys must be scalar and not null
1483 SELECT json_build_object(null,2);
1484 ERROR: argument 1 cannot be null
1485 HINT: Object keys should be text.
1486 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1487 ERROR: key value must be scalar, not array, composite, or json
1488 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1489 ERROR: key value must be scalar, not array, composite, or json
1490 SELECT json_build_object('{1,2,3}'::int[], 3);
1491 ERROR: key value must be scalar, not array, composite, or json
1492 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1493 INSERT INTO foo VALUES (847001,'t15','GE1043');
1494 INSERT INTO foo VALUES (847002,'t16','GE1043');
1495 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1496 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1499 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1500 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1503 SELECT json_object_agg(name, type) FROM foo;
1505 ----------------------------------------------------------------
1506 { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1509 INSERT INTO foo VALUES (999999, NULL, 'bar');
1510 SELECT json_object_agg(name, type) FROM foo;
1511 ERROR: field name must not be null
1514 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1516 -------------------------------------------------------
1517 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1520 -- same but with two dimensions
1521 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1523 -------------------------------------------------------
1524 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1528 SELECT json_object('{a,b,c}');
1529 ERROR: array must have even number of elements
1531 SELECT json_object('{{a},{b}}');
1532 ERROR: array must have two columns
1533 -- too many columns error
1534 SELECT json_object('{{a,b,c},{b,c,d}}');
1535 ERROR: array must have two columns
1536 -- too many dimensions error
1537 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1538 ERROR: wrong number of array subscripts
1539 --two argument form of json_object
1540 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1542 ------------------------------------------------------
1543 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1546 -- too many dimensions
1547 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"}}');
1548 ERROR: wrong number of array subscripts
1549 -- mismatched dimensions
1550 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1551 ERROR: mismatched array dimensions
1552 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1553 ERROR: mismatched array dimensions
1555 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1556 ERROR: null value not allowed for object key
1557 -- empty key is allowed
1558 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1560 -----------------------------------------------------
1561 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1564 -- json_to_record and json_to_recordset
1565 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1566 as x(a int, b text, d text);
1572 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1573 as x(a int, b text, c boolean);
1580 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1581 as x(a int, b json, c boolean);
1583 ---+-------------+---
1589 select json_strip_nulls(null);
1595 select json_strip_nulls('1');
1601 select json_strip_nulls('"a string"');
1607 select json_strip_nulls('null');
1613 select json_strip_nulls('[1,2,null,3,4]');
1619 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1621 ------------------------------------
1622 {"a":1,"c":[2,null,3],"d":{"e":4}}
1625 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1627 ---------------------
1631 -- an empty object is not null and should not be stripped
1632 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');