]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Output timestamps in ISO 8601 format when rendering JSON.
[postgresql] / src / test / regress / expected / json.out
1 -- Strings.
2 SELECT '""'::json;                              -- OK.
3  json 
4 ------
5  ""
6 (1 row)
7
8 SELECT $$''$$::json;                    -- ERROR, single quotes are not allowed
9 ERROR:  invalid input syntax for type json
10 LINE 1: SELECT $$''$$::json;
11                ^
12 DETAIL:  Token "'" is invalid.
13 CONTEXT:  JSON data, line 1: '...
14 SELECT '"abc"'::json;                   -- OK
15  json  
16 -------
17  "abc"
18 (1 row)
19
20 SELECT '"abc'::json;                    -- ERROR, quotes not closed
21 ERROR:  invalid input syntax for type json
22 LINE 1: SELECT '"abc'::json;
23                ^
24 DETAIL:  Token ""abc" is invalid.
25 CONTEXT:  JSON data, line 1: "abc
26 SELECT '"abc
27 def"'::json;                                    -- ERROR, unescaped newline in string constant
28 ERROR:  invalid input syntax for type json
29 LINE 1: SELECT '"abc
30                ^
31 DETAIL:  Character with value 0x0a must be escaped.
32 CONTEXT:  JSON data, line 1: "abc
33 SELECT '"\n\"\\"'::json;                -- OK, legal escapes
34    json   
35 ----------
36  "\n\"\\"
37 (1 row)
38
39 SELECT '"\v"'::json;                    -- ERROR, not a valid JSON escape
40 ERROR:  invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::json;
42                ^
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;
48                ^
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;
54                ^
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;
60                ^
61 DETAIL:  "\u" must be followed by four hexadecimal digits.
62 CONTEXT:  JSON data, line 1: "\u000g...
63 SELECT '"\u0000"'::json;                -- OK, legal escape
64    json   
65 ----------
66  "\u0000"
67 (1 row)
68
69 SELECT '"\uaBcD"'::json;                -- OK, uppercase and lower case both OK
70    json   
71 ----------
72  "\uaBcD"
73 (1 row)
74
75 -- Numbers.
76 SELECT '1'::json;                               -- OK
77  json 
78 ------
79  1
80 (1 row)
81
82 SELECT '0'::json;                               -- OK
83  json 
84 ------
85  0
86 (1 row)
87
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;
91                ^
92 DETAIL:  Token "01" is invalid.
93 CONTEXT:  JSON data, line 1: 01
94 SELECT '0.1'::json;                             -- OK
95  json 
96 ------
97  0.1
98 (1 row)
99
100 SELECT '9223372036854775808'::json;     -- OK, even though it's too large for int8
101         json         
102 ---------------------
103  9223372036854775808
104 (1 row)
105
106 SELECT '1e100'::json;                   -- OK
107  json  
108 -------
109  1e100
110 (1 row)
111
112 SELECT '1.3e100'::json;                 -- OK
113   json   
114 ---------
115  1.3e100
116 (1 row)
117
118 SELECT '1f2'::json;                             -- ERROR
119 ERROR:  invalid input syntax for type json
120 LINE 1: SELECT '1f2'::json;
121                ^
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;
127                ^
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;
133                ^
134 DETAIL:  Token "1.3ex100" is invalid.
135 CONTEXT:  JSON data, line 1: 1.3ex100
136 -- Arrays.
137 SELECT '[]'::json;                              -- OK
138  json 
139 ------
140  []
141 (1 row)
142
143 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json;  -- OK
144                                                                                                    json                                                                                                   
145 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
146  [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
147 (1 row)
148
149 SELECT '[1,2]'::json;                   -- OK
150  json  
151 -------
152  [1,2]
153 (1 row)
154
155 SELECT '[1,2,]'::json;                  -- ERROR, trailing comma
156 ERROR:  invalid input syntax for type json
157 LINE 1: SELECT '[1,2,]'::json;
158                ^
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;
164                ^
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;
170                ^
171 DETAIL:  The input string ended unexpectedly.
172 CONTEXT:  JSON data, line 1: [1,[2]
173 -- Objects.
174 SELECT '{}'::json;                              -- OK
175  json 
176 ------
177  {}
178 (1 row)
179
180 SELECT '{"abc"}'::json;                 -- ERROR, no value
181 ERROR:  invalid input syntax for type json
182 LINE 1: SELECT '{"abc"}'::json;
183                ^
184 DETAIL:  Expected ":", but found "}".
185 CONTEXT:  JSON data, line 1: {"abc"}
186 SELECT '{"abc":1}'::json;               -- OK
187    json    
188 -----------
189  {"abc":1}
190 (1 row)
191
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;
195                ^
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;
201                ^
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;
207                ^
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;
213                ^
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
217                           json                           
218 ---------------------------------------------------------
219  {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
220 (1 row)
221
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;
225                ^
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;
231                ^
232 DETAIL:  Expected string, but found "3".
233 CONTEXT:  JSON data, line 1: {"abc":1,3...
234 -- Miscellaneous stuff.
235 SELECT 'true'::json;                    -- OK
236  json 
237 ------
238  true
239 (1 row)
240
241 SELECT 'false'::json;                   -- OK
242  json  
243 -------
244  false
245 (1 row)
246
247 SELECT 'null'::json;                    -- OK
248  json 
249 ------
250  null
251 (1 row)
252
253 SELECT ' true '::json;                  -- OK, even with extra whitespace
254   json  
255 --------
256   true 
257 (1 row)
258
259 SELECT 'true false'::json;              -- ERROR, too many values
260 ERROR:  invalid input syntax for type json
261 LINE 1: SELECT 'true false'::json;
262                ^
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;
268                ^
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;
274                ^
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;
280                ^
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;
286                ^
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;
292                ^
293 DETAIL:  The input string ended unexpectedly.
294 CONTEXT:  JSON data, line 1:     
295 --constructors
296 -- array_to_json
297 SELECT array_to_json(array(select 1 as a));
298  array_to_json 
299 ---------------
300  [1]
301 (1 row)
302
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;
304                 array_to_json                
305 ---------------------------------------------
306  [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
307 (1 row)
308
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;
310   array_to_json  
311 -----------------
312  [{"b":1,"c":2},+
313   {"b":2,"c":4},+
314   {"b":3,"c":6}]
315 (1 row)
316
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;
323                                                                                                                                  array_to_json                                                                                                                                 
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]}]}]
326 (1 row)
327
328 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
329  array_to_json  
330 ----------------
331  [5,6,7,8,9,10]
332 (1 row)
333
334 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
335   array_to_json   
336 ------------------
337  [[1,5],[99,100]]
338 (1 row)
339
340 -- row_to_json
341 SELECT row_to_json(row(1,'foo'));
342      row_to_json     
343 ---------------------
344  {"f1":1,"f2":"foo"}
345 (1 row)
346
347 SELECT row_to_json(q)
348 FROM (SELECT $$a$$ || x AS b,
349          y AS c,
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;
354                             row_to_json                             
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]}]}
360 (4 rows)
361
362 SELECT row_to_json(q,true)
363 FROM (SELECT $$a$$ || x AS b,
364          y AS c,
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;
369                      row_to_json                     
370 -----------------------------------------------------
371  {"b":"a1",                                         +
372   "c":4,                                            +
373   "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
374  {"b":"a1",                                         +
375   "c":5,                                            +
376   "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
377  {"b":"a2",                                         +
378   "c":4,                                            +
379   "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
380  {"b":"a2",                                         +
381   "c":5,                                            +
382   "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
383 (4 rows)
384
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)
389 FROM rows q;
390  row_to_json  
391 --------------
392  {"x":1,     +
393   "y":"txt1"}
394  {"x":2,     +
395   "y":"txt2"}
396  {"x":3,     +
397   "y":"txt3"}
398 (3 rows)
399
400 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
401       row_to_json      
402 -----------------------
403  {"f1":[5,6,7,8,9,10]}
404 (1 row)
405
406 -- to_json, timestamps
407 select to_json(timestamp '2014-05-28 12:22:35.614298');
408            to_json            
409 ------------------------------
410  "2014-05-28T12:22:35.614298"
411 (1 row)
412
413 BEGIN;
414 SET LOCAL TIME ZONE 10.5;
415 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
416               to_json               
417 ------------------------------------
418  "2014-05-29T02:52:35.614298+10:30"
419 (1 row)
420
421 SET LOCAL TIME ZONE -8;
422 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
423              to_json             
424 ---------------------------------
425  "2014-05-28T08:22:35.614298-08"
426 (1 row)
427
428 COMMIT;
429 --json_agg
430 SELECT json_agg(q)
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;
436                                json_agg                                
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]}]}]
442 (1 row)
443
444 SELECT json_agg(q)
445   FROM rows q;
446        json_agg        
447 -----------------------
448  [{"x":1,"y":"txt1"}, +
449   {"x":2,"y":"txt2"}, +
450   {"x":3,"y":"txt3"}]
451 (1 row)
452
453 -- non-numeric output
454 SELECT row_to_json(q)
455 FROM (SELECT 'NaN'::float8 AS "float8field") q;
456       row_to_json      
457 -----------------------
458  {"float8field":"NaN"}
459 (1 row)
460
461 SELECT row_to_json(q)
462 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
463         row_to_json         
464 ----------------------------
465  {"float8field":"Infinity"}
466 (1 row)
467
468 SELECT row_to_json(q)
469 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
470          row_to_json         
471 -----------------------------
472  {"float8field":"-Infinity"}
473 (1 row)
474
475 -- json input
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;
478                            row_to_json                            
479 ------------------------------------------------------------------
480  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
481 (1 row)
482
483 -- json extraction functions
484 CREATE TEMP TABLE test_json (
485        json_type text,
486        test_json json
487 );
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'
493 FROM test_json
494 WHERE json_type = 'scalar';
495 ERROR:  cannot extract element from a scalar
496 SELECT test_json -> 'x'
497 FROM test_json
498 WHERE json_type = 'array';
499 ERROR:  cannot extract field from a non-object
500 SELECT test_json -> 'x'
501 FROM test_json
502 WHERE json_type = 'object';
503  ?column? 
504 ----------
505  
506 (1 row)
507
508 SELECT test_json->'field2'
509 FROM test_json
510 WHERE json_type = 'object';
511  ?column? 
512 ----------
513  "val2"
514 (1 row)
515
516 SELECT test_json->>'field2'
517 FROM test_json
518 WHERE json_type = 'object';
519  ?column? 
520 ----------
521  val2
522 (1 row)
523
524 SELECT test_json -> 2
525 FROM test_json
526 WHERE json_type = 'scalar';
527 ERROR:  cannot extract element from a scalar
528 SELECT test_json -> 2
529 FROM test_json
530 WHERE json_type = 'array';
531  ?column? 
532 ----------
533  "two"
534 (1 row)
535
536 SELECT test_json -> 2
537 FROM test_json
538 WHERE json_type = 'object';
539 ERROR:  cannot extract array element from a non-array
540 SELECT test_json->>2
541 FROM test_json
542 WHERE json_type = 'array';
543  ?column? 
544 ----------
545  two
546 (1 row)
547
548 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
549  ?column? 
550 ----------
551  [1,2,3]
552 (1 row)
553
554 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
555  ?column? 
556 ----------
557  {"f1":9}
558 (1 row)
559
560 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
561  ?column? 
562 ----------
563  4
564 (1 row)
565
566 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
567  ?column? 
568 ----------
569  [1,2,3]
570 (1 row)
571
572 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
573  ?column? 
574 ----------
575  {"f1":9}
576 (1 row)
577
578 SELECT json_object_keys(test_json)
579 FROM test_json
580 WHERE json_type = 'scalar';
581 ERROR:  cannot call json_object_keys on a scalar
582 SELECT json_object_keys(test_json)
583 FROM test_json
584 WHERE json_type = 'array';
585 ERROR:  cannot call json_object_keys on an array
586 SELECT json_object_keys(test_json)
587 FROM test_json
588 WHERE json_type = 'object';
589  json_object_keys 
590 ------------------
591  field1
592  field2
593  field3
594  field4
595  field5
596  field6
597 (6 rows)
598
599 -- test extending object_keys resultset - initial resultset size is 256
600 select count(*) from
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;
604  count 
605 -------
606    300
607 (1 row)
608
609 -- nulls
610 select (test_json->'field3') is null as expect_false
611 from test_json
612 where json_type = 'object';
613  expect_false 
614 --------------
615  f
616 (1 row)
617
618 select (test_json->>'field3') is null as expect_true
619 from test_json
620 where json_type = 'object';
621  expect_true 
622 -------------
623  t
624 (1 row)
625
626 select (test_json->3) is null as expect_false
627 from test_json
628 where json_type = 'array';
629  expect_false 
630 --------------
631  f
632 (1 row)
633
634 select (test_json->>3) is null as expect_true
635 from test_json
636 where json_type = 'array';
637  expect_true 
638 -------------
639  t
640 (1 row)
641
642 -- array length
643 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
644  json_array_length 
645 -------------------
646                  5
647 (1 row)
648
649 SELECT json_array_length('[]');
650  json_array_length 
651 -------------------
652                  0
653 (1 row)
654
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
659 -- each
660 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
661      json_each     
662 -------------------
663  (f1,"[1,2,3]")
664  (f2,"{""f3"":1}")
665  (f4,null)
666 (3 rows)
667
668 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
669  key |   value   
670 -----+-----------
671  f1  | [1,2,3]
672  f2  | {"f3":1}
673  f4  | null
674  f5  | 99
675  f6  | "stringy"
676 (5 rows)
677
678 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
679   json_each_text   
680 -------------------
681  (f1,"[1,2,3]")
682  (f2,"{""f3"":1}")
683  (f4,)
684  (f5,null)
685 (4 rows)
686
687 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
688  key |  value   
689 -----+----------
690  f1  | [1,2,3]
691  f2  | {"f3":1}
692  f4  | 
693  f5  | 99
694  f6  | stringy
695 (5 rows)
696
697 -- extract_path, extract_path_as_text
698 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
699  json_extract_path 
700 -------------------
701  "stringy"
702 (1 row)
703
704 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
705  json_extract_path 
706 -------------------
707  {"f3":1}
708 (1 row)
709
710 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
711  json_extract_path 
712 -------------------
713  "f3"
714 (1 row)
715
716 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
717  json_extract_path 
718 -------------------
719  1
720 (1 row)
721
722 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
723  json_extract_path_text 
724 ------------------------
725  stringy
726 (1 row)
727
728 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
729  json_extract_path_text 
730 ------------------------
731  {"f3":1}
732 (1 row)
733
734 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
735  json_extract_path_text 
736 ------------------------
737  f3
738 (1 row)
739
740 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
741  json_extract_path_text 
742 ------------------------
743  1
744 (1 row)
745
746 -- extract_path nulls
747 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
748  expect_false 
749 --------------
750  f
751 (1 row)
752
753 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
754  expect_true 
755 -------------
756  t
757 (1 row)
758
759 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
760  expect_false 
761 --------------
762  f
763 (1 row)
764
765 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
766  expect_true 
767 -------------
768  t
769 (1 row)
770
771 -- extract_path operators
772 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
773  ?column?  
774 -----------
775  "stringy"
776 (1 row)
777
778 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
779  ?column? 
780 ----------
781  {"f3":1}
782 (1 row)
783
784 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
785  ?column? 
786 ----------
787  "f3"
788 (1 row)
789
790 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
791  ?column? 
792 ----------
793  1
794 (1 row)
795
796 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
797  ?column? 
798 ----------
799  stringy
800 (1 row)
801
802 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
803  ?column? 
804 ----------
805  {"f3":1}
806 (1 row)
807
808 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
809  ?column? 
810 ----------
811  f3
812 (1 row)
813
814 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
815  ?column? 
816 ----------
817  1
818 (1 row)
819
820 -- same using array literals
821 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f4,f6}';
822  ?column?  
823 -----------
824  "stringy"
825 (1 row)
826
827 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2}';
828  ?column? 
829 ----------
830  {"f3":1}
831 (1 row)
832
833 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2,0}';
834  ?column? 
835 ----------
836  "f3"
837 (1 row)
838
839 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>'{f2,1}';
840  ?column? 
841 ----------
842  1
843 (1 row)
844
845 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f4,f6}';
846  ?column? 
847 ----------
848  stringy
849 (1 row)
850
851 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2}';
852  ?column? 
853 ----------
854  {"f3":1}
855 (1 row)
856
857 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2,0}';
858  ?column? 
859 ----------
860  f3
861 (1 row)
862
863 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>'{f2,1}';
864  ?column? 
865 ----------
866  1
867 (1 row)
868
869 -- array_elements
870 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
871   json_array_elements  
872 -----------------------
873  1
874  true
875  [1,[2,3]]
876  null
877  {"f1":1,"f2":[7,8,9]}
878  false
879  "stringy"
880 (7 rows)
881
882 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
883          value         
884 -----------------------
885  1
886  true
887  [1,[2,3]]
888  null
889  {"f1":1,"f2":[7,8,9]}
890  false
891  "stringy"
892 (7 rows)
893
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 --------------------------
897  1
898  true
899  [1,[2,3]]
900  
901  {"f1":1,"f2":[7,8,9]}
902  false
903  stringy
904 (7 rows)
905
906 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
907          value         
908 -----------------------
909  1
910  true
911  [1,[2,3]]
912  
913  {"f1":1,"f2":[7,8,9]}
914  false
915  stringy
916 (7 rows)
917
918 -- populate_record
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;
921    a    | b | c 
922 --------+---+---
923  blurfl |   | 
924 (1 row)
925
926 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
927    a    | b |            c             
928 --------+---+--------------------------
929  blurfl | 3 | Mon Dec 31 15:30:56 2012
930 (1 row)
931
932 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}', true) q;
933    a    | b | c 
934 --------+---+---
935  blurfl |   | 
936 (1 row)
937
938 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}', true) q;
939    a    | b |            c             
940 --------+---+--------------------------
941  blurfl | 3 | Mon Dec 31 15:30:56 2012
942 (1 row)
943
944 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}', true) q;
945         a        | b | c 
946 -----------------+---+---
947  [100,200,false] |   | 
948 (1 row)
949
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;
951         a        | b |            c             
952 -----------------+---+--------------------------
953  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
954 (1 row)
955
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;
960    a    | b |            c             
961 --------+---+--------------------------
962  blurfl |   | 
963         | 3 | Fri Jan 20 10:42:53 2012
964 (2 rows)
965
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;
967    a    | b  |            c             
968 --------+----+--------------------------
969  blurfl | 99 | 
970  def    |  3 | Fri Jan 20 10:42:53 2012
971 (2 rows)
972
973 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',true) q;
974    a    | b |            c             
975 --------+---+--------------------------
976  blurfl |   | 
977         | 3 | Fri Jan 20 10:42:53 2012
978 (2 rows)
979
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;
981    a    | b  |            c             
982 --------+----+--------------------------
983  blurfl | 99 | 
984  def    |  3 | Fri Jan 20 10:42:53 2012
985 (2 rows)
986
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;
988        a       | b  |            c             
989 ---------------+----+--------------------------
990  [100,200,300] | 99 | 
991  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
992 (2 rows)
993
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;
998    a    | b |            c             
999 --------+---+--------------------------
1000  blurfl |   | 
1001         | 3 | Fri Jan 20 10:42:53 2012
1002 (2 rows)
1003
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;
1005    a    | b  |            c             
1006 --------+----+--------------------------
1007  blurfl | 99 | 
1008  def    |  3 | Fri Jan 20 10:42:53 2012
1009 (2 rows)
1010
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;
1017       correct_in_utf8       
1018 ----------------------------
1019  "\ud83d\ude04\ud83d\udc36"
1020 (1 row)
1021
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;
1040    correct_in_utf8    
1041 ----------------------
1042  the Copyright © sign
1043 (1 row)
1044
1045 select json '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
1046  correct_everywhere 
1047 --------------------
1048  dollar $ character
1049 (1 row)
1050
1051 select json '{ "a":  "null \u0000 escape" }' ->> 'a' as not_unescaped;
1052    not_unescaped    
1053 --------------------
1054  null \u0000 escape
1055 (1 row)
1056
1057 --json_typeof() function
1058 select value, json_typeof(value)
1059   from (values (json '123.4'),
1060                (json '-1'),
1061                (json '"foo"'),
1062                (json 'true'),
1063                (json 'false'),
1064                (json 'null'),
1065                (json '[1, 2, 3]'),
1066                (json '[]'),
1067                (json '{"x":"foo", "y":123}'),
1068                (json '{}'),
1069                (NULL::json))
1070       as data(value);
1071         value         | json_typeof 
1072 ----------------------+-------------
1073  123.4                | number
1074  -1                   | number
1075  "foo"                | string
1076  true                 | boolean
1077  false                | boolean
1078  null                 | null
1079  [1, 2, 3]            | array
1080  []                   | array
1081  {"x":"foo", "y":123} | object
1082  {}                   | object
1083                       | 
1084 (11 rows)
1085
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]}');
1088                            json_build_array                            
1089 -----------------------------------------------------------------------
1090  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1091 (1 row)
1092
1093 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1094                              json_build_object                              
1095 ----------------------------------------------------------------------------
1096  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1097 (1 row)
1098
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)));
1103                                         json_build_object                                        
1104 -------------------------------------------------------------------------------------------------
1105  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1106 (1 row)
1107
1108 -- empty objects/arrays
1109 SELECT json_build_array();
1110  json_build_array 
1111 ------------------
1112  []
1113 (1 row)
1114
1115 SELECT json_build_object();
1116  json_build_object 
1117 -------------------
1118  {}
1119 (1 row)
1120
1121 -- make sure keys are quoted
1122 SELECT json_build_object(1,2);
1123  json_build_object 
1124 -------------------
1125  {"1" : 2}
1126 (1 row)
1127
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)))
1142 FROM foo;
1143                                                                             json_build_object                                                                            
1144 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1145  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1146 (1 row)
1147
1148 -- json_object
1149 -- one dimension
1150 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1151                       json_object                      
1152 -------------------------------------------------------
1153  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1154 (1 row)
1155
1156 -- same but with two dimensions
1157 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1158                       json_object                      
1159 -------------------------------------------------------
1160  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1161 (1 row)
1162
1163 -- odd number error
1164 SELECT json_object('{a,b,c}');
1165 ERROR:  array must have even number of elements
1166 -- one column error
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"}');
1177                      json_object                      
1178 ------------------------------------------------------
1179  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1180 (1 row)
1181
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
1190 -- null key error
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
1193 -- empty key error
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);
1199  a |  b  | d 
1200 ---+-----+---
1201  1 | foo | 
1202 (1 row)
1203
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);
1206  a |  b  | c 
1207 ---+-----+---
1208  1 | foo | 
1209  2 | bar | t
1210 (2 rows)
1211