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"
431 FROM ( SELECT $$a$$ || x AS b, y AS c,
432 ARRAY[ROW(x.*,ARRAY[1,2,3]),
433 ROW(y.*,ARRAY[4,5,6])] AS z
434 FROM generate_series(1,2) x,
435 generate_series(4,5) y) q;
437 -----------------------------------------------------------------------
438 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
439 {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
440 {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
441 {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
447 -----------------------
448 [{"x":1,"y":"txt1"}, +
449 {"x":2,"y":"txt2"}, +
453 -- non-numeric output
454 SELECT row_to_json(q)
455 FROM (SELECT 'NaN'::float8 AS "float8field") q;
457 -----------------------
458 {"float8field":"NaN"}
461 SELECT row_to_json(q)
462 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
464 ----------------------------
465 {"float8field":"Infinity"}
468 SELECT row_to_json(q)
469 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
471 -----------------------------
472 {"float8field":"-Infinity"}
476 SELECT row_to_json(q)
477 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
479 ------------------------------------------------------------------
480 {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
483 -- json extraction functions
484 CREATE TEMP TABLE test_json (
488 INSERT INTO test_json VALUES
489 ('scalar','"a scalar"'),
490 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
491 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
492 SELECT test_json -> 'x'
494 WHERE json_type = 'scalar';
495 ERROR: cannot extract element from a scalar
496 SELECT test_json -> 'x'
498 WHERE json_type = 'array';
499 ERROR: cannot extract field from a non-object
500 SELECT test_json -> 'x'
502 WHERE json_type = 'object';
508 SELECT test_json->'field2'
510 WHERE json_type = 'object';
516 SELECT test_json->>'field2'
518 WHERE json_type = 'object';
524 SELECT test_json -> 2
526 WHERE json_type = 'scalar';
527 ERROR: cannot extract element from a scalar
528 SELECT test_json -> 2
530 WHERE json_type = 'array';
536 SELECT test_json -> 2
538 WHERE json_type = 'object';
539 ERROR: cannot extract array element from a non-array
542 WHERE json_type = 'array';
548 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
554 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
560 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
566 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
572 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
578 SELECT json_object_keys(test_json)
580 WHERE json_type = 'scalar';
581 ERROR: cannot call json_object_keys on a scalar
582 SELECT json_object_keys(test_json)
584 WHERE json_type = 'array';
585 ERROR: cannot call json_object_keys on an array
586 SELECT json_object_keys(test_json)
588 WHERE json_type = 'object';
599 -- test extending object_keys resultset - initial resultset size is 256
601 (select json_object_keys(json_object(array_agg(g)))
602 from (select unnest(array['f'||n,n::text])as g
603 from generate_series(1,300) as n) x ) y;
610 select (test_json->'field3') is null as expect_false
612 where json_type = 'object';
618 select (test_json->>'field3') is null as expect_true
620 where json_type = 'object';
626 select (test_json->3) is null as expect_false
628 where json_type = 'array';
634 select (test_json->>3) is null as expect_true
636 where json_type = 'array';
643 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
649 SELECT json_array_length('[]');
655 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
656 ERROR: cannot get array length of a non-array
657 SELECT json_array_length('4');
658 ERROR: cannot get array length of a scalar
660 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
668 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
678 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
687 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
697 -- extract_path, extract_path_as_text
698 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
704 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
710 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
716 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
722 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
723 json_extract_path_text
724 ------------------------
728 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
729 json_extract_path_text
730 ------------------------
734 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
735 json_extract_path_text
736 ------------------------
740 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
741 json_extract_path_text
742 ------------------------
746 -- extract_path nulls
747 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
753 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
759 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
765 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
771 -- extract_path operators
772 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
778 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
784 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
790 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
796 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
802 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
808 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
814 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
820 -- same using array literals
821 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f4,f6}';
827 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2}';
833 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2,0}';
839 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2,1}';
845 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f4,f6}';
851 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2}';
857 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2,0}';
863 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2,1}';
870 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
872 -----------------------
877 {"f1":1,"f2":[7,8,9]}
882 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
884 -----------------------
889 {"f1":1,"f2":[7,8,9]}
894 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
895 json_array_elements_text
896 --------------------------
901 {"f1":1,"f2":[7,8,9]}
906 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
908 -----------------------
913 {"f1":1,"f2":[7,8,9]}
919 create type jpop as (a text, b int, c timestamp);
920 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
926 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
928 --------+---+--------------------------
929 blurfl | 3 | Mon Dec 31 15:30:56 2012
932 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}', true) q;
938 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}', true) q;
940 --------+---+--------------------------
941 blurfl | 3 | Mon Dec 31 15:30:56 2012
944 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}', true) q;
946 -----------------+---+---
950 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}', true) q;
952 -----------------+---+--------------------------
953 [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
956 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}', true) q;
957 ERROR: invalid input syntax for type timestamp: "[100,200,false]"
958 -- populate_recordset
959 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',false) q;
961 --------+---+--------------------------
963 | 3 | Fri Jan 20 10:42:53 2012
966 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',false) q;
968 --------+----+--------------------------
970 def | 3 | Fri Jan 20 10:42:53 2012
973 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',true) q;
975 --------+---+--------------------------
977 | 3 | Fri Jan 20 10:42:53 2012
980 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',true) q;
982 --------+----+--------------------------
984 def | 3 | Fri Jan 20 10:42:53 2012
987 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"}]',true) q;
989 ---------------+----+--------------------------
991 {"z":true} | 3 | Fri Jan 20 10:42:53 2012
994 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"}]',true) q;
995 ERROR: invalid input syntax for type timestamp: "[100,200,300]"
996 -- using the default use_json_as_text argument
997 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
999 --------+---+--------------------------
1001 | 3 | Fri Jan 20 10:42:53 2012
1004 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;
1006 --------+----+--------------------------
1008 def | 3 | Fri Jan 20 10:42:53 2012
1011 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;
1012 ERROR: cannot call json_populate_recordset on a nested object
1013 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;
1014 ERROR: cannot call json_populate_recordset on a nested object
1015 -- handling of unicode surrogate pairs
1016 select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
1018 ----------------------------
1019 "\ud83d\ude04\ud83d\udc36"
1022 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
1023 ERROR: invalid input syntax for type json
1024 DETAIL: Unicode high surrogate must not follow a high surrogate.
1025 CONTEXT: JSON data, line 1: { "a":...
1026 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
1027 ERROR: invalid input syntax for type json
1028 DETAIL: Unicode low surrogate must follow a high surrogate.
1029 CONTEXT: JSON data, line 1: { "a":...
1030 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
1031 ERROR: invalid input syntax for type json
1032 DETAIL: Unicode low surrogate must follow a high surrogate.
1033 CONTEXT: JSON data, line 1: { "a":...
1034 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
1035 ERROR: invalid input syntax for type json
1036 DETAIL: Unicode low surrogate must follow a high surrogate.
1037 CONTEXT: JSON data, line 1: { "a":...
1038 --handling of simple unicode escapes
1039 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
1041 ----------------------
1042 the Copyright © sign
1045 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
1047 --------------------
1051 select json '{ "a": "null \u0000 escape" }' ->> 'a' as not_unescaped;
1053 --------------------
1057 --json_typeof() function
1058 select value, json_typeof(value)
1059 from (values (json '123.4'),
1067 (json '{"x":"foo", "y":123}'),
1072 ----------------------+-------------
1081 {"x":"foo", "y":123} | object
1086 -- json_build_array, json_build_object, json_object_agg
1087 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1089 -----------------------------------------------------------------------
1090 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1093 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1095 ----------------------------------------------------------------------------
1096 {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1099 SELECT json_build_object(
1100 'a', json_build_object('b',false,'c',99),
1101 'd', json_build_object('e',array[9,8,7]::int[],
1102 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1104 -------------------------------------------------------------------------------------------------
1105 {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1108 -- empty objects/arrays
1109 SELECT json_build_array();
1115 SELECT json_build_object();
1121 -- make sure keys are quoted
1122 SELECT json_build_object(1,2);
1128 -- keys must be scalar and not null
1129 SELECT json_build_object(null,2);
1130 ERROR: arg 1: key cannot be null
1131 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1132 ERROR: key value must be scalar, not array, composite or json
1133 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1134 ERROR: key value must be scalar, not array, composite or json
1135 SELECT json_build_object('{1,2,3}'::int[], 3);
1136 ERROR: key value must be scalar, not array, composite or json
1137 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1138 INSERT INTO foo VALUES (847001,'t15','GE1043');
1139 INSERT INTO foo VALUES (847002,'t16','GE1043');
1140 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1141 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1144 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1145 {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1150 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1152 -------------------------------------------------------
1153 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1156 -- same but with two dimensions
1157 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1159 -------------------------------------------------------
1160 {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1164 SELECT json_object('{a,b,c}');
1165 ERROR: array must have even number of elements
1167 SELECT json_object('{{a},{b}}');
1168 ERROR: array must have two columns
1169 -- too many columns error
1170 SELECT json_object('{{a,b,c},{b,c,d}}');
1171 ERROR: array must have two columns
1172 -- too many dimensions error
1173 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1174 ERROR: wrong number of array subscripts
1175 --two argument form of json_object
1176 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1178 ------------------------------------------------------
1179 {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1182 -- too many dimensions
1183 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"}}');
1184 ERROR: wrong number of array subscripts
1185 -- mismatched dimensions
1186 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1187 ERROR: mismatched array dimensions
1188 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1189 ERROR: mismatched array dimensions
1191 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1192 ERROR: null value not allowed for object key
1194 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1195 ERROR: empty value not allowed for object key
1196 -- json_to_record and json_to_recordset
1197 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}',true)
1198 as x(a int, b text, d text);
1204 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]',false)
1205 as x(a int, b text, c boolean);