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]}]}]
468 SELECT json_agg(q ORDER BY x, y)
471 -----------------------
472 [{"x":1,"y":"txt1"}, +
473 {"x":2,"y":"txt2"}, +
477 UPDATE rows SET x = NULL WHERE x = 1;
478 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
481 --------------------------
482 [{"x":null,"y":"txt1"}, +
483 {"x":2,"y":"txt2"}, +
487 -- non-numeric output
488 SELECT row_to_json(q)
489 FROM (SELECT 'NaN'::float8 AS "float8field") q;
491 -----------------------
492 {"float8field":"NaN"}
495 SELECT row_to_json(q)
496 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
498 ----------------------------
499 {"float8field":"Infinity"}
502 SELECT row_to_json(q)
503 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
505 -----------------------------
506 {"float8field":"-Infinity"}
510 SELECT row_to_json(q)
511 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
513 ------------------------------------------------------------------
514 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
517 -- json extraction functions
518 CREATE TEMP TABLE test_json (
522 INSERT INTO test_json VALUES
523 ('scalar','"a scalar"'),
524 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
525 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
526 SELECT test_json -> 'x'
528 WHERE json_type = 'scalar';
534 SELECT test_json -> 'x'
536 WHERE json_type = 'array';
542 SELECT test_json -> 'x'
544 WHERE json_type = 'object';
550 SELECT test_json->'field2'
552 WHERE json_type = 'object';
558 SELECT test_json->>'field2'
560 WHERE json_type = 'object';
566 SELECT test_json -> 2
568 WHERE json_type = 'scalar';
574 SELECT test_json -> 2
576 WHERE json_type = 'array';
582 SELECT test_json -> -1
584 WHERE json_type = 'array';
590 SELECT test_json -> 2
592 WHERE json_type = 'object';
600 WHERE json_type = 'array';
606 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
612 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
618 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
624 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
630 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
636 SELECT json_object_keys(test_json)
638 WHERE json_type = 'scalar';
639 ERROR: cannot call json_object_keys on a scalar
640 SELECT json_object_keys(test_json)
642 WHERE json_type = 'array';
643 ERROR: cannot call json_object_keys on an array
644 SELECT json_object_keys(test_json)
646 WHERE json_type = 'object';
657 -- test extending object_keys resultset - initial resultset size is 256
659 (select json_object_keys(json_object(array_agg(g)))
660 from (select unnest(array['f'||n,n::text])as g
661 from generate_series(1,300) as n) x ) y;
668 select (test_json->'field3') is null as expect_false
670 where json_type = 'object';
676 select (test_json->>'field3') is null as expect_true
678 where json_type = 'object';
684 select (test_json->3) is null as expect_false
686 where json_type = 'array';
692 select (test_json->>3) is null as expect_true
694 where json_type = 'array';
701 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
707 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
713 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
719 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
725 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
731 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
737 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
743 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
749 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
755 select '{"a": "c", "b": null}'::json -> 'b';
761 select '"foo"'::json -> 1;
767 select '"foo"'::json -> 'z';
773 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
779 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
785 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
791 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
797 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
803 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
809 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
815 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
821 select '{"a": "c", "b": null}'::json ->> 'b';
827 select '"foo"'::json ->> 1;
833 select '"foo"'::json ->> 'z';
840 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
846 SELECT json_array_length('[]');
852 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
853 ERROR: cannot get array length of a non-array
854 SELECT json_array_length('4');
855 ERROR: cannot get array length of a scalar
857 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
865 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
875 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
884 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
894 -- extract_path, extract_path_as_text
895 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
901 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
907 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
913 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
919 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
920 json_extract_path_text
921 ------------------------
925 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
926 json_extract_path_text
927 ------------------------
931 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
932 json_extract_path_text
933 ------------------------
937 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
938 json_extract_path_text
939 ------------------------
943 -- extract_path nulls
944 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
950 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
956 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
962 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
968 -- extract_path operators
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 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
999 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
1005 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1011 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1017 -- corner cases for same
1018 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1020 ---------------------------
1021 {"a": {"b":{"c": "foo"}}}
1024 select '[1,2,3]'::json #> '{}';
1030 select '"foo"'::json #> '{}';
1036 select '42'::json #> '{}';
1042 select 'null'::json #> '{}';
1048 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1050 --------------------
1054 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1060 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1066 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1072 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1078 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1084 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1090 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1096 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1102 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1108 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1114 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1120 select '"foo"'::json #> array['z'];
1126 select '42'::json #> array['f2'];
1132 select '42'::json #> array['0'];
1138 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1140 ---------------------------
1141 {"a": {"b":{"c": "foo"}}}
1144 select '[1,2,3]'::json #>> '{}';
1150 select '"foo"'::json #>> '{}';
1156 select '42'::json #>> '{}';
1162 select 'null'::json #>> '{}';
1168 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1170 --------------------
1174 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1180 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1186 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1192 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1198 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1204 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1210 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1216 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1222 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1228 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1234 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1240 select '"foo"'::json #>> array['z'];
1246 select '42'::json #>> array['f2'];
1252 select '42'::json #>> array['0'];
1259 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1261 -----------------------
1266 {"f1":1,"f2":[7,8,9]}
1271 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1273 -----------------------
1278 {"f1":1,"f2":[7,8,9]}
1283 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1284 json_array_elements_text
1285 --------------------------
1290 {"f1":1,"f2":[7,8,9]}
1295 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1297 -----------------------
1302 {"f1":1,"f2":[7,8,9]}
1308 create type jpop as (a text, b int, c timestamp);
1309 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1315 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1317 --------+---+--------------------------
1318 blurfl | 3 | Mon Dec 31 15:30:56 2012
1321 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1327 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1329 --------+---+--------------------------
1330 blurfl | 3 | Mon Dec 31 15:30:56 2012
1333 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1335 -----------------+---+---
1339 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1341 -----------------+---+--------------------------
1342 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1345 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1346 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
1347 -- populate_recordset
1348 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1350 --------+---+--------------------------
1352 | 3 | Fri Jan 20 10:42:53 2012
1355 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;
1357 --------+----+--------------------------
1359 def | 3 | Fri Jan 20 10:42:53 2012
1362 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1364 --------+---+--------------------------
1366 | 3 | Fri Jan 20 10:42:53 2012
1369 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;
1371 --------+----+--------------------------
1373 def | 3 | Fri Jan 20 10:42:53 2012
1376 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;
1378 ---------------+----+--------------------------
1379 [100,200,300] | 99 |
1380 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1383 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;
1384 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
1385 create type jpop2 as (a int, b json, c int, d int);
1386 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1388 ---+---------+---+---
1392 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1394 --------+---+--------------------------
1396 | 3 | Fri Jan 20 10:42:53 2012
1399 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;
1401 --------+----+--------------------------
1403 def | 3 | Fri Jan 20 10:42:53 2012
1406 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;
1408 ---------------+----+--------------------------
1409 [100,200,300] | 99 |
1410 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
1413 -- handling of unicode surrogate pairs
1414 select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
1416 ----------------------------
1417 "\ud83d\ude04\ud83d\udc36"
1420 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
1421 ERROR: invalid input syntax for type json
1422 DETAIL: Unicode high surrogate must not follow a high surrogate.
1423 CONTEXT: JSON data, line 1: { "a":...
1424 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
1425 ERROR: invalid input syntax for type json
1426 DETAIL: Unicode low surrogate must follow a high surrogate.
1427 CONTEXT: JSON data, line 1: { "a":...
1428 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
1429 ERROR: invalid input syntax for type json
1430 DETAIL: Unicode low surrogate must follow a high surrogate.
1431 CONTEXT: JSON data, line 1: { "a":...
1432 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
1433 ERROR: invalid input syntax for type json
1434 DETAIL: Unicode low surrogate must follow a high surrogate.
1435 CONTEXT: JSON data, line 1: { "a":...
1436 --handling of simple unicode escapes
1437 select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
1439 ---------------------------------------
1440 { "a": "the Copyright \u00a9 sign" }
1443 select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
1445 -------------------------------------
1446 { "a": "dollar \u0024 character" }
1449 select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
1451 --------------------------------------
1452 { "a": "dollar \\u0024 character" }
1455 select json '{ "a": "null \u0000 escape" }' as not_unescaped;
1457 --------------------------------
1458 { "a": "null \u0000 escape" }
1461 select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
1463 ---------------------------------
1464 { "a": "null \\u0000 escape" }
1467 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
1469 ----------------------
1470 the Copyright © sign
1473 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
1475 --------------------
1479 select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
1481 -------------------------
1482 dollar \u0024 character
1485 select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
1486 ERROR: unsupported Unicode escape sequence
1487 DETAIL: \u0000 cannot be converted to text.
1488 CONTEXT: JSON data, line 1: { "a":...
1489 select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
1491 --------------------
1495 --json_typeof() function
1496 select value, json_typeof(value)
1497 from (values (json '123.4'),
1505 (json '{"x":"foo", "y":123}'),
1510 ----------------------+-------------
1519 {"x":"foo", "y":123} | object
1524 -- json_build_array, json_build_object, json_object_agg
1525 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1527 -----------------------------------------------------------------------
1528 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1531 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1533 ----------------------------------------------------------------------------
1534 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1537 SELECT json_build_object(
1538 'a', json_build_object('b',false,'c',99),
1539 'd', json_build_object('e',array[9,8,7]::int[],
1540 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1542 -------------------------------------------------------------------------------------------------
1543 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1546 -- empty objects/arrays
1547 SELECT json_build_array();
1553 SELECT json_build_object();
1559 -- make sure keys are quoted
1560 SELECT json_build_object(1,2);
1566 -- keys must be scalar and not null
1567 SELECT json_build_object(null,2);
1568 ERROR: argument 1 cannot be null
1569 HINT: Object keys should be text.
1570 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1571 ERROR: key value must be scalar, not array, composite, or json
1572 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1573 ERROR: key value must be scalar, not array, composite, or json
1574 SELECT json_build_object('{1,2,3}'::int[], 3);
1575 ERROR: key value must be scalar, not array, composite, or json
1576 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1577 INSERT INTO foo VALUES (847001,'t15','GE1043');
1578 INSERT INTO foo VALUES (847002,'t16','GE1043');
1579 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1580 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1583 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1584 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1587 SELECT json_object_agg(name, type) FROM foo;
1589 ----------------------------------------------------------------
1590 { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1593 INSERT INTO foo VALUES (999999, NULL, 'bar');
1594 SELECT json_object_agg(name, type) FROM foo;
1595 ERROR: field name must not be null
1598 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1600 -------------------------------------------------------
1601 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1604 -- same but with two dimensions
1605 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1607 -------------------------------------------------------
1608 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1612 SELECT json_object('{a,b,c}');
1613 ERROR: array must have even number of elements
1615 SELECT json_object('{{a},{b}}');
1616 ERROR: array must have two columns
1617 -- too many columns error
1618 SELECT json_object('{{a,b,c},{b,c,d}}');
1619 ERROR: array must have two columns
1620 -- too many dimensions error
1621 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1622 ERROR: wrong number of array subscripts
1623 --two argument form of json_object
1624 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1626 ------------------------------------------------------
1627 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1630 -- too many dimensions
1631 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"}}');
1632 ERROR: wrong number of array subscripts
1633 -- mismatched dimensions
1634 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1635 ERROR: mismatched array dimensions
1636 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1637 ERROR: mismatched array dimensions
1639 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1640 ERROR: null value not allowed for object key
1641 -- empty key is allowed
1642 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1644 -----------------------------------------------------
1645 {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1648 -- json_to_record and json_to_recordset
1649 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1650 as x(a int, b text, d text);
1656 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1657 as x(a int, b text, c boolean);
1664 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1665 as x(a int, b json, c boolean);
1667 ---+-------------+---
1673 select json_strip_nulls(null);
1679 select json_strip_nulls('1');
1685 select json_strip_nulls('"a string"');
1691 select json_strip_nulls('null');
1697 select json_strip_nulls('[1,2,null,3,4]');
1703 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1705 ------------------------------------
1706 {"a":1,"c":[2,null,3],"d":{"e":4}}
1709 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1711 ---------------------
1715 -- an empty object is not null and should not be stripped
1716 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');