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(timestamp 'Infinity');
427 select to_json(timestamptz 'Infinity');
435 FROM ( SELECT $$a$$ || x AS b, y AS c,
436 ARRAY[ROW(x.*,ARRAY[1,2,3]),
437 ROW(y.*,ARRAY[4,5,6])] AS z
438 FROM generate_series(1,2) x,
439 generate_series(4,5) y) q;
441 -----------------------------------------------------------------------
442 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
443 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
444 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
445 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
448 SELECT json_agg(q ORDER BY x, y)
451 -----------------------
452 [{"x":1,"y":"txt1"}, +
453 {"x":2,"y":"txt2"}, +
457 UPDATE rows SET x = NULL WHERE x = 1;
458 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
461 --------------------------
462 [{"x":null,"y":"txt1"}, +
463 {"x":2,"y":"txt2"}, +
467 -- non-numeric output
468 SELECT row_to_json(q)
469 FROM (SELECT 'NaN'::float8 AS "float8field") q;
471 -----------------------
472 {"float8field":"NaN"}
475 SELECT row_to_json(q)
476 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
478 ----------------------------
479 {"float8field":"Infinity"}
482 SELECT row_to_json(q)
483 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
485 -----------------------------
486 {"float8field":"-Infinity"}
490 SELECT row_to_json(q)
491 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
493 ------------------------------------------------------------------
494 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
497 -- json extraction functions
498 CREATE TEMP TABLE test_json (
502 INSERT INTO test_json VALUES
503 ('scalar','"a scalar"'),
504 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
505 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
506 SELECT test_json -> 'x'
508 WHERE json_type = 'scalar';
514 SELECT test_json -> 'x'
516 WHERE json_type = 'array';
522 SELECT test_json -> 'x'
524 WHERE json_type = 'object';
530 SELECT test_json->'field2'
532 WHERE json_type = 'object';
538 SELECT test_json->>'field2'
540 WHERE json_type = 'object';
546 SELECT test_json -> 2
548 WHERE json_type = 'scalar';
554 SELECT test_json -> 2
556 WHERE json_type = 'array';
562 SELECT test_json -> -1
564 WHERE json_type = 'array';
570 SELECT test_json -> 2
572 WHERE json_type = 'object';
580 WHERE json_type = 'array';
586 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
592 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
598 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
604 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
610 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
616 SELECT json_object_keys(test_json)
618 WHERE json_type = 'scalar';
619 ERROR: cannot call json_object_keys on a scalar
620 SELECT json_object_keys(test_json)
622 WHERE json_type = 'array';
623 ERROR: cannot call json_object_keys on an array
624 SELECT json_object_keys(test_json)
626 WHERE json_type = 'object';
637 -- test extending object_keys resultset - initial resultset size is 256
639 (select json_object_keys(json_object(array_agg(g)))
640 from (select unnest(array['f'||n,n::text])as g
641 from generate_series(1,300) as n) x ) y;
648 select (test_json->'field3') is null as expect_false
650 where json_type = 'object';
656 select (test_json->>'field3') is null as expect_true
658 where json_type = 'object';
664 select (test_json->3) is null as expect_false
666 where json_type = 'array';
672 select (test_json->>3) is null as expect_true
674 where json_type = 'array';
681 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
687 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
693 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
699 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
705 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
711 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
717 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
723 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
729 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
735 select '{"a": "c", "b": null}'::json -> 'b';
741 select '"foo"'::json -> 1;
747 select '"foo"'::json -> 'z';
753 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
759 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
765 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
771 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
777 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
783 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
789 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
795 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
801 select '{"a": "c", "b": null}'::json ->> 'b';
807 select '"foo"'::json ->> 1;
813 select '"foo"'::json ->> 'z';
820 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
826 SELECT json_array_length('[]');
832 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
833 ERROR: cannot get array length of a non-array
834 SELECT json_array_length('4');
835 ERROR: cannot get array length of a scalar
837 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
845 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
855 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
864 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
874 -- extract_path, extract_path_as_text
875 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
881 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
887 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
893 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
899 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
900 json_extract_path_text
901 ------------------------
905 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
906 json_extract_path_text
907 ------------------------
911 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
912 json_extract_path_text
913 ------------------------
917 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
918 json_extract_path_text
919 ------------------------
923 -- extract_path nulls
924 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
930 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
936 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
942 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
948 -- extract_path operators
949 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
955 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
961 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
967 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
973 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
979 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
985 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
991 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
997 -- corner cases for same
998 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1000 ---------------------------
1001 {"a": {"b":{"c": "foo"}}}
1004 select '[1,2,3]'::json #> '{}';
1010 select '"foo"'::json #> '{}';
1016 select '42'::json #> '{}';
1022 select 'null'::json #> '{}';
1028 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1030 --------------------
1034 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1040 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1046 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1052 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1058 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1064 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1070 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1076 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1082 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1088 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1094 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1100 select '"foo"'::json #> array['z'];
1106 select '42'::json #> array['f2'];
1112 select '42'::json #> array['0'];
1118 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1120 ---------------------------
1121 {"a": {"b":{"c": "foo"}}}
1124 select '[1,2,3]'::json #>> '{}';
1130 select '"foo"'::json #>> '{}';
1136 select '42'::json #>> '{}';
1142 select 'null'::json #>> '{}';
1148 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1150 --------------------
1154 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1160 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1166 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1172 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1178 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1184 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1190 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1196 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1202 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1208 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1214 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1220 select '"foo"'::json #>> array['z'];
1226 select '42'::json #>> array['f2'];
1232 select '42'::json #>> array['0'];
1239 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1241 -----------------------
1246 {"f1":1,"f2":[7,8,9]}
1251 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1253 -----------------------
1258 {"f1":1,"f2":[7,8,9]}
1263 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1264 json_array_elements_text
1265 --------------------------
1270 {"f1":1,"f2":[7,8,9]}
1275 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1277 -----------------------
1282 {"f1":1,"f2":[7,8,9]}
1288 create type jpop as (a text, b int, c timestamp);
1289 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1295 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1297 --------+---+--------------------------
1298 blurfl | 3 | Mon Dec 31 15:30:56 2012
1301 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1307 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1309 --------+---+--------------------------
1310 blurfl | 3 | Mon Dec 31 15:30:56 2012
1313 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1315 -----------------+---+---
1319 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1321 -----------------+---+--------------------------
1322 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1325 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1326 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1327 -- populate_recordset
1328 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1330 --------+---+--------------------------
1332 | 3 | Fri Jan 20 10:42:53 2012
1335 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;
1337 --------+----+--------------------------
1339 def | 3 | Fri Jan 20 10:42:53 2012
1342 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1344 --------+---+--------------------------
1346 | 3 | Fri Jan 20 10:42:53 2012
1349 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;
1351 --------+----+--------------------------
1353 def | 3 | Fri Jan 20 10:42:53 2012
1356 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;
1358 ---------------+----+--------------------------
1359 [100,200,300] | 99 |
1360 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1363 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;
1364 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1365 create type jpop2 as (a int, b json, c int, d int);
1366 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1368 ---+---------+---+---
1372 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1374 --------+---+--------------------------
1376 | 3 | Fri Jan 20 10:42:53 2012
1379 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;
1381 --------+----+--------------------------
1383 def | 3 | Fri Jan 20 10:42:53 2012
1386 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;
1388 ---------------+----+--------------------------
1389 [100,200,300] | 99 |
1390 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1393 --json_typeof() function
1394 select value, json_typeof(value)
1395 from (values (json '123.4'),
1403 (json '{"x":"foo", "y":123}'),
1408 ----------------------+-------------
1417 {"x":"foo", "y":123} | object
1422 -- json_build_array, json_build_object, json_object_agg
1423 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1425 -----------------------------------------------------------------------
1426 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1429 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1431 ----------------------------------------------------------------------------
1432 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1435 SELECT json_build_object(
1436 'a', json_build_object('b',false,'c',99),
1437 'd', json_build_object('e',array[9,8,7]::int[],
1438 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1440 -------------------------------------------------------------------------------------------------
1441 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1444 -- empty objects/arrays
1445 SELECT json_build_array();
1451 SELECT json_build_object();
1457 -- make sure keys are quoted
1458 SELECT json_build_object(1,2);
1464 -- keys must be scalar and not null
1465 SELECT json_build_object(null,2);
1466 ERROR: argument 1 cannot be null
1467 HINT: Object keys should be text.
1468 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1469 ERROR: key value must be scalar, not array, composite, or json
1470 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1471 ERROR: key value must be scalar, not array, composite, or json
1472 SELECT json_build_object('{1,2,3}'::int[], 3);
1473 ERROR: key value must be scalar, not array, composite, or json
1474 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1475 INSERT INTO foo VALUES (847001,'t15','GE1043');
1476 INSERT INTO foo VALUES (847002,'t16','GE1043');
1477 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1478 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1481 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1482 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1485 SELECT json_object_agg(name, type) FROM foo;
1487 ----------------------------------------------------------------
1488 { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1491 INSERT INTO foo VALUES (999999, NULL, 'bar');
1492 SELECT json_object_agg(name, type) FROM foo;
1493 ERROR: field name must not be null
1496 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1498 -------------------------------------------------------
1499 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1502 -- same but with two dimensions
1503 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1505 -------------------------------------------------------
1506 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1510 SELECT json_object('{a,b,c}');
1511 ERROR: array must have even number of elements
1513 SELECT json_object('{{a},{b}}');
1514 ERROR: array must have two columns
1515 -- too many columns error
1516 SELECT json_object('{{a,b,c},{b,c,d}}');
1517 ERROR: array must have two columns
1518 -- too many dimensions error
1519 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1520 ERROR: wrong number of array subscripts
1521 --two argument form of json_object
1522 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1524 ------------------------------------------------------
1525 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1528 -- too many dimensions
1529 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"}}');
1530 ERROR: wrong number of array subscripts
1531 -- mismatched dimensions
1532 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1533 ERROR: mismatched array dimensions
1534 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1535 ERROR: mismatched array dimensions
1537 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1538 ERROR: null value not allowed for object key
1539 -- empty key is allowed
1540 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1542 -----------------------------------------------------
1543 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1546 -- json_to_record and json_to_recordset
1547 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1548 as x(a int, b text, d text);
1554 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1555 as x(a int, b text, c boolean);
1562 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1563 as x(a int, b json, c boolean);
1565 ---+-------------+---
1571 select json_strip_nulls(null);
1577 select json_strip_nulls('1');
1583 select json_strip_nulls('"a string"');
1589 select json_strip_nulls('null');
1595 select json_strip_nulls('[1,2,null,3,4]');
1601 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1603 ------------------------------------
1604 {"a":1,"c":[2,null,3],"d":{"e":4}}
1607 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1609 ---------------------
1613 -- an empty object is not null and should not be stripped
1614 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');