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 SELECT '"\u"'::json; -- ERROR, incomplete escape
46 ERROR: invalid input syntax for type json
47 LINE 1: SELECT '"\u"'::json;
49 DETAIL: "\u" must be followed by four hexadecimal digits.
50 CONTEXT: JSON data, line 1: "\u"
51 SELECT '"\u00"'::json; -- ERROR, incomplete escape
52 ERROR: invalid input syntax for type json
53 LINE 1: SELECT '"\u00"'::json;
55 DETAIL: "\u" must be followed by four hexadecimal digits.
56 CONTEXT: JSON data, line 1: "\u00"
57 SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
58 ERROR: invalid input syntax for type json
59 LINE 1: SELECT '"\u000g"'::json;
61 DETAIL: "\u" must be followed by four hexadecimal digits.
62 CONTEXT: JSON data, line 1: "\u000g...
63 SELECT '"\u0000"'::json; -- OK, legal escape
69 SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
76 SELECT '1'::json; -- OK
82 SELECT '0'::json; -- OK
88 SELECT '01'::json; -- ERROR, not valid according to JSON spec
89 ERROR: invalid input syntax for type json
90 LINE 1: SELECT '01'::json;
92 DETAIL: Token "01" is invalid.
93 CONTEXT: JSON data, line 1: 01
94 SELECT '0.1'::json; -- OK
100 SELECT '9223372036854775808'::json; -- OK, even though it's too large for int8
102 ---------------------
106 SELECT '1e100'::json; -- OK
112 SELECT '1.3e100'::json; -- OK
118 SELECT '1f2'::json; -- ERROR
119 ERROR: invalid input syntax for type json
120 LINE 1: SELECT '1f2'::json;
122 DETAIL: Token "1f2" is invalid.
123 CONTEXT: JSON data, line 1: 1f2
124 SELECT '0.x1'::json; -- ERROR
125 ERROR: invalid input syntax for type json
126 LINE 1: SELECT '0.x1'::json;
128 DETAIL: Token "0.x1" is invalid.
129 CONTEXT: JSON data, line 1: 0.x1
130 SELECT '1.3ex100'::json; -- ERROR
131 ERROR: invalid input syntax for type json
132 LINE 1: SELECT '1.3ex100'::json;
134 DETAIL: Token "1.3ex100" is invalid.
135 CONTEXT: JSON data, line 1: 1.3ex100
137 SELECT '[]'::json; -- OK
143 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json; -- OK
145 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
146 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
149 SELECT '[1,2]'::json; -- OK
155 SELECT '[1,2,]'::json; -- ERROR, trailing comma
156 ERROR: invalid input syntax for type json
157 LINE 1: SELECT '[1,2,]'::json;
159 DETAIL: Expected JSON value, but found "]".
160 CONTEXT: JSON data, line 1: [1,2,]
161 SELECT '[1,2'::json; -- ERROR, no closing bracket
162 ERROR: invalid input syntax for type json
163 LINE 1: SELECT '[1,2'::json;
165 DETAIL: The input string ended unexpectedly.
166 CONTEXT: JSON data, line 1: [1,2
167 SELECT '[1,[2]'::json; -- ERROR, no closing bracket
168 ERROR: invalid input syntax for type json
169 LINE 1: SELECT '[1,[2]'::json;
171 DETAIL: The input string ended unexpectedly.
172 CONTEXT: JSON data, line 1: [1,[2]
174 SELECT '{}'::json; -- OK
180 SELECT '{"abc"}'::json; -- ERROR, no value
181 ERROR: invalid input syntax for type json
182 LINE 1: SELECT '{"abc"}'::json;
184 DETAIL: Expected ":", but found "}".
185 CONTEXT: JSON data, line 1: {"abc"}
186 SELECT '{"abc":1}'::json; -- OK
192 SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
193 ERROR: invalid input syntax for type json
194 LINE 1: SELECT '{1:"abc"}'::json;
196 DETAIL: Expected string or "}", but found "1".
197 CONTEXT: JSON data, line 1: {1...
198 SELECT '{"abc",1}'::json; -- ERROR, wrong separator
199 ERROR: invalid input syntax for type json
200 LINE 1: SELECT '{"abc",1}'::json;
202 DETAIL: Expected ":", but found ",".
203 CONTEXT: JSON data, line 1: {"abc",...
204 SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
205 ERROR: invalid input syntax for type json
206 LINE 1: SELECT '{"abc"=1}'::json;
208 DETAIL: Token "=" is invalid.
209 CONTEXT: JSON data, line 1: {"abc"=...
210 SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
211 ERROR: invalid input syntax for type json
212 LINE 1: SELECT '{"abc"::1}'::json;
214 DETAIL: Expected JSON value, but found ":".
215 CONTEXT: JSON data, line 1: {"abc"::...
216 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
218 ---------------------------------------------------------
219 {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
222 SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
223 ERROR: invalid input syntax for type json
224 LINE 1: SELECT '{"abc":1:2}'::json;
226 DETAIL: Expected "," or "}", but found ":".
227 CONTEXT: JSON data, line 1: {"abc":1:...
228 SELECT '{"abc":1,3}'::json; -- ERROR, no value
229 ERROR: invalid input syntax for type json
230 LINE 1: SELECT '{"abc":1,3}'::json;
232 DETAIL: Expected string, but found "3".
233 CONTEXT: JSON data, line 1: {"abc":1,3...
234 -- Miscellaneous stuff.
235 SELECT 'true'::json; -- OK
241 SELECT 'false'::json; -- OK
247 SELECT 'null'::json; -- OK
253 SELECT ' true '::json; -- OK, even with extra whitespace
259 SELECT 'true false'::json; -- ERROR, too many values
260 ERROR: invalid input syntax for type json
261 LINE 1: SELECT 'true false'::json;
263 DETAIL: Expected end of input, but found "false".
264 CONTEXT: JSON data, line 1: true false
265 SELECT 'true, false'::json; -- ERROR, too many values
266 ERROR: invalid input syntax for type json
267 LINE 1: SELECT 'true, false'::json;
269 DETAIL: Expected end of input, but found ",".
270 CONTEXT: JSON data, line 1: true,...
271 SELECT 'truf'::json; -- ERROR, not a keyword
272 ERROR: invalid input syntax for type json
273 LINE 1: SELECT 'truf'::json;
275 DETAIL: Token "truf" is invalid.
276 CONTEXT: JSON data, line 1: truf
277 SELECT 'trues'::json; -- ERROR, not a keyword
278 ERROR: invalid input syntax for type json
279 LINE 1: SELECT 'trues'::json;
281 DETAIL: Token "trues" is invalid.
282 CONTEXT: JSON data, line 1: trues
283 SELECT ''::json; -- ERROR, no value
284 ERROR: invalid input syntax for type json
285 LINE 1: SELECT ''::json;
287 DETAIL: The input string ended unexpectedly.
288 CONTEXT: JSON data, line 1:
289 SELECT ' '::json; -- ERROR, no value
290 ERROR: invalid input syntax for type json
291 LINE 1: SELECT ' '::json;
293 DETAIL: The input string ended unexpectedly.
294 CONTEXT: JSON data, line 1:
297 SELECT array_to_json(array(select 1 as a));
303 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
305 ---------------------------------------------
306 [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
309 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
317 SELECT array_to_json(array_agg(q),false)
318 FROM ( SELECT $$a$$ || x AS b, y AS c,
319 ARRAY[ROW(x.*,ARRAY[1,2,3]),
320 ROW(y.*,ARRAY[4,5,6])] AS z
321 FROM generate_series(1,2) x,
322 generate_series(4,5) y) q;
324 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
325 [{"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]}]}]
328 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
334 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
341 SELECT row_to_json(row(1,'foo'));
343 ---------------------
347 SELECT row_to_json(q)
348 FROM (SELECT $$a$$ || x AS b,
350 ARRAY[ROW(x.*,ARRAY[1,2,3]),
351 ROW(y.*,ARRAY[4,5,6])] AS z
352 FROM generate_series(1,2) x,
353 generate_series(4,5) y) q;
355 --------------------------------------------------------------------
356 {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
357 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
358 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
359 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
362 SELECT row_to_json(q,true)
363 FROM (SELECT $$a$$ || x AS b,
365 ARRAY[ROW(x.*,ARRAY[1,2,3]),
366 ROW(y.*,ARRAY[4,5,6])] AS z
367 FROM generate_series(1,2) x,
368 generate_series(4,5) y) q;
370 -----------------------------------------------------
373 "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
376 "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
379 "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
382 "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
385 CREATE TEMP TABLE rows AS
386 SELECT x, 'txt' || x as y
387 FROM generate_series(1,3) AS x;
388 SELECT row_to_json(q,true)
400 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
402 -----------------------
403 {"f1":[5,6,7,8,9,10]}
406 -- to_json, timestamps
407 select to_json(timestamp '2014-05-28 12:22:35.614298');
409 ------------------------------
410 "2014-05-28T12:22:35.614298"
414 SET LOCAL TIME ZONE 10.5;
415 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
417 ------------------------------------
418 "2014-05-29T02:52:35.614298+10:30"
421 SET LOCAL TIME ZONE -8;
422 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
424 ------------------------------------
425 "2014-05-28T08:22:35.614298-08:00"
429 select to_json(date '2014-05-28');
435 select to_json(date 'Infinity');
441 select to_json(timestamp 'Infinity');
447 select to_json(timestamptz 'Infinity');
455 FROM ( SELECT $$a$$ || x AS b, y AS c,
456 ARRAY[ROW(x.*,ARRAY[1,2,3]),
457 ROW(y.*,ARRAY[4,5,6])] AS z
458 FROM generate_series(1,2) x,
459 generate_series(4,5) y) q;
461 -----------------------------------------------------------------------
462 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
463 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
464 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
465 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
471 -----------------------
472 [{"x":1,"y":"txt1"}, +
473 {"x":2,"y":"txt2"}, +
477 -- non-numeric output
478 SELECT row_to_json(q)
479 FROM (SELECT 'NaN'::float8 AS "float8field") q;
481 -----------------------
482 {"float8field":"NaN"}
485 SELECT row_to_json(q)
486 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
488 ----------------------------
489 {"float8field":"Infinity"}
492 SELECT row_to_json(q)
493 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
495 -----------------------------
496 {"float8field":"-Infinity"}
500 SELECT row_to_json(q)
501 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
503 ------------------------------------------------------------------
504 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
507 -- json extraction functions
508 CREATE TEMP TABLE test_json (
512 INSERT INTO test_json VALUES
513 ('scalar','"a scalar"'),
514 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
515 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
516 SELECT test_json -> 'x'
518 WHERE json_type = 'scalar';
524 SELECT test_json -> 'x'
526 WHERE json_type = 'array';
532 SELECT test_json -> 'x'
534 WHERE json_type = 'object';
540 SELECT test_json->'field2'
542 WHERE json_type = 'object';
548 SELECT test_json->>'field2'
550 WHERE json_type = 'object';
556 SELECT test_json -> 2
558 WHERE json_type = 'scalar';
564 SELECT test_json -> 2
566 WHERE json_type = 'array';
572 SELECT test_json -> 2
574 WHERE json_type = 'object';
582 WHERE json_type = 'array';
588 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
594 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
600 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
606 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
612 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
618 SELECT json_object_keys(test_json)
620 WHERE json_type = 'scalar';
621 ERROR: cannot call json_object_keys on a scalar
622 SELECT json_object_keys(test_json)
624 WHERE json_type = 'array';
625 ERROR: cannot call json_object_keys on an array
626 SELECT json_object_keys(test_json)
628 WHERE json_type = 'object';
639 -- test extending object_keys resultset - initial resultset size is 256
641 (select json_object_keys(json_object(array_agg(g)))
642 from (select unnest(array['f'||n,n::text])as g
643 from generate_series(1,300) as n) x ) y;
650 select (test_json->'field3') is null as expect_false
652 where json_type = 'object';
658 select (test_json->>'field3') is null as expect_true
660 where json_type = 'object';
666 select (test_json->3) is null as expect_false
668 where json_type = 'array';
674 select (test_json->>3) is null as expect_true
676 where json_type = 'array';
683 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
689 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
695 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
701 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
707 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
713 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
719 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
725 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
731 select '{"a": "c", "b": null}'::json -> 'b';
737 select '"foo"'::json -> 1;
743 select '"foo"'::json -> 'z';
749 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
755 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
761 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
767 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
773 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
779 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
785 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
791 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
797 select '{"a": "c", "b": null}'::json ->> 'b';
803 select '"foo"'::json ->> 1;
809 select '"foo"'::json ->> 'z';
816 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
822 SELECT json_array_length('[]');
828 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
829 ERROR: cannot get array length of a non-array
830 SELECT json_array_length('4');
831 ERROR: cannot get array length of a scalar
833 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
841 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
851 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
860 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
870 -- extract_path, extract_path_as_text
871 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
877 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
883 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
889 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
895 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
896 json_extract_path_text
897 ------------------------
901 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
902 json_extract_path_text
903 ------------------------
907 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
908 json_extract_path_text
909 ------------------------
913 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
914 json_extract_path_text
915 ------------------------
919 -- extract_path nulls
920 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
926 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
932 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
938 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
944 -- extract_path operators
945 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
951 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
957 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
963 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
969 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
975 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
981 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
987 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
993 -- corner cases for same
994 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
996 ---------------------------
997 {"a": {"b":{"c": "foo"}}}
1000 select '[1,2,3]'::json #> '{}';
1006 select '"foo"'::json #> '{}';
1012 select '42'::json #> '{}';
1018 select 'null'::json #> '{}';
1024 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1026 --------------------
1030 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1036 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1042 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1048 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1054 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1060 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1066 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1072 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1078 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1084 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1090 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1096 select '"foo"'::json #> array['z'];
1102 select '42'::json #> array['f2'];
1108 select '42'::json #> array['0'];
1114 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1116 ---------------------------
1117 {"a": {"b":{"c": "foo"}}}
1120 select '[1,2,3]'::json #>> '{}';
1126 select '"foo"'::json #>> '{}';
1132 select '42'::json #>> '{}';
1138 select 'null'::json #>> '{}';
1144 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1146 --------------------
1150 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1156 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1162 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1168 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1174 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1180 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1186 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1192 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1198 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1204 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1210 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1216 select '"foo"'::json #>> array['z'];
1222 select '42'::json #>> array['f2'];
1228 select '42'::json #>> array['0'];
1235 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1237 -----------------------
1242 {"f1":1,"f2":[7,8,9]}
1247 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1249 -----------------------
1254 {"f1":1,"f2":[7,8,9]}
1259 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1260 json_array_elements_text
1261 --------------------------
1266 {"f1":1,"f2":[7,8,9]}
1271 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1273 -----------------------
1278 {"f1":1,"f2":[7,8,9]}
1284 create type jpop as (a text, b int, c timestamp);
1285 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1291 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1293 --------+---+--------------------------
1294 blurfl | 3 | Mon Dec 31 15:30:56 2012
1297 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1303 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1305 --------+---+--------------------------
1306 blurfl | 3 | Mon Dec 31 15:30:56 2012
1309 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1311 -----------------+---+---
1315 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1317 -----------------+---+--------------------------
1318 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1321 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1322 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1323 -- populate_recordset
1324 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1326 --------+---+--------------------------
1328 | 3 | Fri Jan 20 10:42:53 2012
1331 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;
1333 --------+----+--------------------------
1335 def | 3 | Fri Jan 20 10:42:53 2012
1338 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1340 --------+---+--------------------------
1342 | 3 | Fri Jan 20 10:42:53 2012
1345 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;
1347 --------+----+--------------------------
1349 def | 3 | Fri Jan 20 10:42:53 2012
1352 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;
1354 ---------------+----+--------------------------
1355 [100,200,300] | 99 |
1356 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1359 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;
1360 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1361 create type jpop2 as (a int, b json, c int, d int);
1362 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1364 ---+---------+---+---
1368 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1370 --------+---+--------------------------
1372 | 3 | Fri Jan 20 10:42:53 2012
1375 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;
1377 --------+----+--------------------------
1379 def | 3 | Fri Jan 20 10:42:53 2012
1382 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;
1384 ---------------+----+--------------------------
1385 [100,200,300] | 99 |
1386 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1389 -- handling of unicode surrogate pairs
1390 select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
1392 ----------------------------
1393 "\ud83d\ude04\ud83d\udc36"
1396 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
1397 ERROR: invalid input syntax for type json
1398 DETAIL: Unicode high surrogate must not follow a high surrogate.
1399 CONTEXT: JSON data, line 1: { "a":...
1400 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
1401 ERROR: invalid input syntax for type json
1402 DETAIL: Unicode low surrogate must follow a high surrogate.
1403 CONTEXT: JSON data, line 1: { "a":...
1404 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
1405 ERROR: invalid input syntax for type json
1406 DETAIL: Unicode low surrogate must follow a high surrogate.
1407 CONTEXT: JSON data, line 1: { "a":...
1408 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
1409 ERROR: invalid input syntax for type json
1410 DETAIL: Unicode low surrogate must follow a high surrogate.
1411 CONTEXT: JSON data, line 1: { "a":...
1412 --handling of simple unicode escapes
1413 select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
1415 ---------------------------------------
1416 { "a": "the Copyright \u00a9 sign" }
1419 select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
1421 -------------------------------------
1422 { "a": "dollar \u0024 character" }
1425 select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
1427 --------------------------------------
1428 { "a": "dollar \\u0024 character" }
1431 select json '{ "a": "null \u0000 escape" }' as not_unescaped;
1433 --------------------------------
1434 { "a": "null \u0000 escape" }
1437 select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
1439 ---------------------------------
1440 { "a": "null \\u0000 escape" }
1443 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
1445 ----------------------
1446 the Copyright © sign
1449 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
1451 --------------------
1455 select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
1457 -------------------------
1458 dollar \u0024 character
1461 select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
1462 ERROR: unsupported Unicode escape sequence
1463 DETAIL: \u0000 cannot be converted to text.
1464 CONTEXT: JSON data, line 1: { "a":...
1465 select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
1467 --------------------
1471 --json_typeof() function
1472 select value, json_typeof(value)
1473 from (values (json '123.4'),
1481 (json '{"x":"foo", "y":123}'),
1486 ----------------------+-------------
1495 {"x":"foo", "y":123} | object
1500 -- json_build_array, json_build_object, json_object_agg
1501 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1503 -----------------------------------------------------------------------
1504 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1507 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1509 ----------------------------------------------------------------------------
1510 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1513 SELECT json_build_object(
1514 'a', json_build_object('b',false,'c',99),
1515 'd', json_build_object('e',array[9,8,7]::int[],
1516 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1518 -------------------------------------------------------------------------------------------------
1519 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1522 -- empty objects/arrays
1523 SELECT json_build_array();
1529 SELECT json_build_object();
1535 -- make sure keys are quoted
1536 SELECT json_build_object(1,2);
1542 -- keys must be scalar and not null
1543 SELECT json_build_object(null,2);
1544 ERROR: argument 1 cannot be null
1545 HINT: Object keys should be text.
1546 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1547 ERROR: key value must be scalar, not array, composite, or json
1548 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1549 ERROR: key value must be scalar, not array, composite, or json
1550 SELECT json_build_object('{1,2,3}'::int[], 3);
1551 ERROR: key value must be scalar, not array, composite, or json
1552 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1553 INSERT INTO foo VALUES (847001,'t15','GE1043');
1554 INSERT INTO foo VALUES (847002,'t16','GE1043');
1555 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1556 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1559 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1560 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1565 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1567 -------------------------------------------------------
1568 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1571 -- same but with two dimensions
1572 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1574 -------------------------------------------------------
1575 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1579 SELECT json_object('{a,b,c}');
1580 ERROR: array must have even number of elements
1582 SELECT json_object('{{a},{b}}');
1583 ERROR: array must have two columns
1584 -- too many columns error
1585 SELECT json_object('{{a,b,c},{b,c,d}}');
1586 ERROR: array must have two columns
1587 -- too many dimensions error
1588 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1589 ERROR: wrong number of array subscripts
1590 --two argument form of json_object
1591 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1593 ------------------------------------------------------
1594 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1597 -- too many dimensions
1598 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"}}');
1599 ERROR: wrong number of array subscripts
1600 -- mismatched dimensions
1601 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1602 ERROR: mismatched array dimensions
1603 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1604 ERROR: mismatched array dimensions
1606 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1607 ERROR: null value not allowed for object key
1608 -- empty key is allowed
1609 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1611 -----------------------------------------------------
1612 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1615 -- json_to_record and json_to_recordset
1616 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1617 as x(a int, b text, d text);
1623 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1624 as x(a int, b text, c boolean);
1631 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1632 as x(a int, b json, c boolean);
1634 ---+-------------+---
1640 select json_strip_nulls(null);
1646 select json_strip_nulls('1');
1652 select json_strip_nulls('"a string"');
1658 select json_strip_nulls('null');
1664 select json_strip_nulls('[1,2,null,3,4]');
1670 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1672 ------------------------------------
1673 {"a":1,"c":[2,null,3],"d":{"e":4}}
1676 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1678 ---------------------
1682 -- an empty object is not null and should not be stripped
1683 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');