]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Fix incorrect translation of minus-infinity datetimes for json/jsonb.
[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 -- see json_encoding test for input with unicode escapes
46 -- Numbers.
47 SELECT '1'::json;                               -- OK
48  json 
49 ------
50  1
51 (1 row)
52
53 SELECT '0'::json;                               -- OK
54  json 
55 ------
56  0
57 (1 row)
58
59 SELECT '01'::json;                              -- ERROR, not valid according to JSON spec
60 ERROR:  invalid input syntax for type json
61 LINE 1: SELECT '01'::json;
62                ^
63 DETAIL:  Token "01" is invalid.
64 CONTEXT:  JSON data, line 1: 01
65 SELECT '0.1'::json;                             -- OK
66  json 
67 ------
68  0.1
69 (1 row)
70
71 SELECT '9223372036854775808'::json;     -- OK, even though it's too large for int8
72         json         
73 ---------------------
74  9223372036854775808
75 (1 row)
76
77 SELECT '1e100'::json;                   -- OK
78  json  
79 -------
80  1e100
81 (1 row)
82
83 SELECT '1.3e100'::json;                 -- OK
84   json   
85 ---------
86  1.3e100
87 (1 row)
88
89 SELECT '1f2'::json;                             -- ERROR
90 ERROR:  invalid input syntax for type json
91 LINE 1: SELECT '1f2'::json;
92                ^
93 DETAIL:  Token "1f2" is invalid.
94 CONTEXT:  JSON data, line 1: 1f2
95 SELECT '0.x1'::json;                    -- ERROR
96 ERROR:  invalid input syntax for type json
97 LINE 1: SELECT '0.x1'::json;
98                ^
99 DETAIL:  Token "0.x1" is invalid.
100 CONTEXT:  JSON data, line 1: 0.x1
101 SELECT '1.3ex100'::json;                -- ERROR
102 ERROR:  invalid input syntax for type json
103 LINE 1: SELECT '1.3ex100'::json;
104                ^
105 DETAIL:  Token "1.3ex100" is invalid.
106 CONTEXT:  JSON data, line 1: 1.3ex100
107 -- Arrays.
108 SELECT '[]'::json;                              -- OK
109  json 
110 ------
111  []
112 (1 row)
113
114 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json;  -- OK
115                                                                                                    json                                                                                                   
116 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
117  [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
118 (1 row)
119
120 SELECT '[1,2]'::json;                   -- OK
121  json  
122 -------
123  [1,2]
124 (1 row)
125
126 SELECT '[1,2,]'::json;                  -- ERROR, trailing comma
127 ERROR:  invalid input syntax for type json
128 LINE 1: SELECT '[1,2,]'::json;
129                ^
130 DETAIL:  Expected JSON value, but found "]".
131 CONTEXT:  JSON data, line 1: [1,2,]
132 SELECT '[1,2'::json;                    -- ERROR, no closing bracket
133 ERROR:  invalid input syntax for type json
134 LINE 1: SELECT '[1,2'::json;
135                ^
136 DETAIL:  The input string ended unexpectedly.
137 CONTEXT:  JSON data, line 1: [1,2
138 SELECT '[1,[2]'::json;                  -- ERROR, no closing bracket
139 ERROR:  invalid input syntax for type json
140 LINE 1: SELECT '[1,[2]'::json;
141                ^
142 DETAIL:  The input string ended unexpectedly.
143 CONTEXT:  JSON data, line 1: [1,[2]
144 -- Objects.
145 SELECT '{}'::json;                              -- OK
146  json 
147 ------
148  {}
149 (1 row)
150
151 SELECT '{"abc"}'::json;                 -- ERROR, no value
152 ERROR:  invalid input syntax for type json
153 LINE 1: SELECT '{"abc"}'::json;
154                ^
155 DETAIL:  Expected ":", but found "}".
156 CONTEXT:  JSON data, line 1: {"abc"}
157 SELECT '{"abc":1}'::json;               -- OK
158    json    
159 -----------
160  {"abc":1}
161 (1 row)
162
163 SELECT '{1:"abc"}'::json;               -- ERROR, keys must be strings
164 ERROR:  invalid input syntax for type json
165 LINE 1: SELECT '{1:"abc"}'::json;
166                ^
167 DETAIL:  Expected string or "}", but found "1".
168 CONTEXT:  JSON data, line 1: {1...
169 SELECT '{"abc",1}'::json;               -- ERROR, wrong separator
170 ERROR:  invalid input syntax for type json
171 LINE 1: SELECT '{"abc",1}'::json;
172                ^
173 DETAIL:  Expected ":", but found ",".
174 CONTEXT:  JSON data, line 1: {"abc",...
175 SELECT '{"abc"=1}'::json;               -- ERROR, totally wrong separator
176 ERROR:  invalid input syntax for type json
177 LINE 1: SELECT '{"abc"=1}'::json;
178                ^
179 DETAIL:  Token "=" is invalid.
180 CONTEXT:  JSON data, line 1: {"abc"=...
181 SELECT '{"abc"::1}'::json;              -- ERROR, another wrong separator
182 ERROR:  invalid input syntax for type json
183 LINE 1: SELECT '{"abc"::1}'::json;
184                ^
185 DETAIL:  Expected JSON value, but found ":".
186 CONTEXT:  JSON data, line 1: {"abc"::...
187 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
188                           json                           
189 ---------------------------------------------------------
190  {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
191 (1 row)
192
193 SELECT '{"abc":1:2}'::json;             -- ERROR, colon in wrong spot
194 ERROR:  invalid input syntax for type json
195 LINE 1: SELECT '{"abc":1:2}'::json;
196                ^
197 DETAIL:  Expected "," or "}", but found ":".
198 CONTEXT:  JSON data, line 1: {"abc":1:...
199 SELECT '{"abc":1,3}'::json;             -- ERROR, no value
200 ERROR:  invalid input syntax for type json
201 LINE 1: SELECT '{"abc":1,3}'::json;
202                ^
203 DETAIL:  Expected string, but found "3".
204 CONTEXT:  JSON data, line 1: {"abc":1,3...
205 -- Recursion.
206 SET max_stack_depth = '100kB';
207 SELECT repeat('[', 10000)::json;
208 ERROR:  stack depth limit exceeded
209 HINT:  Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
210 SELECT repeat('{"a":', 10000)::json;
211 ERROR:  stack depth limit exceeded
212 HINT:  Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
213 RESET max_stack_depth;
214 -- Miscellaneous stuff.
215 SELECT 'true'::json;                    -- OK
216  json 
217 ------
218  true
219 (1 row)
220
221 SELECT 'false'::json;                   -- OK
222  json  
223 -------
224  false
225 (1 row)
226
227 SELECT 'null'::json;                    -- OK
228  json 
229 ------
230  null
231 (1 row)
232
233 SELECT ' true '::json;                  -- OK, even with extra whitespace
234   json  
235 --------
236   true 
237 (1 row)
238
239 SELECT 'true false'::json;              -- ERROR, too many values
240 ERROR:  invalid input syntax for type json
241 LINE 1: SELECT 'true false'::json;
242                ^
243 DETAIL:  Expected end of input, but found "false".
244 CONTEXT:  JSON data, line 1: true false
245 SELECT 'true, false'::json;             -- ERROR, too many values
246 ERROR:  invalid input syntax for type json
247 LINE 1: SELECT 'true, false'::json;
248                ^
249 DETAIL:  Expected end of input, but found ",".
250 CONTEXT:  JSON data, line 1: true,...
251 SELECT 'truf'::json;                    -- ERROR, not a keyword
252 ERROR:  invalid input syntax for type json
253 LINE 1: SELECT 'truf'::json;
254                ^
255 DETAIL:  Token "truf" is invalid.
256 CONTEXT:  JSON data, line 1: truf
257 SELECT 'trues'::json;                   -- ERROR, not a keyword
258 ERROR:  invalid input syntax for type json
259 LINE 1: SELECT 'trues'::json;
260                ^
261 DETAIL:  Token "trues" is invalid.
262 CONTEXT:  JSON data, line 1: trues
263 SELECT ''::json;                                -- ERROR, no value
264 ERROR:  invalid input syntax for type json
265 LINE 1: SELECT ''::json;
266                ^
267 DETAIL:  The input string ended unexpectedly.
268 CONTEXT:  JSON data, line 1: 
269 SELECT '    '::json;                    -- ERROR, no value
270 ERROR:  invalid input syntax for type json
271 LINE 1: SELECT '    '::json;
272                ^
273 DETAIL:  The input string ended unexpectedly.
274 CONTEXT:  JSON data, line 1:     
275 --constructors
276 -- array_to_json
277 SELECT array_to_json(array(select 1 as a));
278  array_to_json 
279 ---------------
280  [1]
281 (1 row)
282
283 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
284                 array_to_json                
285 ---------------------------------------------
286  [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
287 (1 row)
288
289 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
290   array_to_json  
291 -----------------
292  [{"b":1,"c":2},+
293   {"b":2,"c":4},+
294   {"b":3,"c":6}]
295 (1 row)
296
297 SELECT array_to_json(array_agg(q),false)
298   FROM ( SELECT $$a$$ || x AS b, y AS c,
299                ARRAY[ROW(x.*,ARRAY[1,2,3]),
300                ROW(y.*,ARRAY[4,5,6])] AS z
301          FROM generate_series(1,2) x,
302               generate_series(4,5) y) q;
303                                                                                                                                  array_to_json                                                                                                                                 
304 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
305  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
306 (1 row)
307
308 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
309  array_to_json  
310 ----------------
311  [5,6,7,8,9,10]
312 (1 row)
313
314 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
315   array_to_json   
316 ------------------
317  [[1,5],[99,100]]
318 (1 row)
319
320 -- row_to_json
321 SELECT row_to_json(row(1,'foo'));
322      row_to_json     
323 ---------------------
324  {"f1":1,"f2":"foo"}
325 (1 row)
326
327 SELECT row_to_json(q)
328 FROM (SELECT $$a$$ || x AS b,
329          y AS c,
330          ARRAY[ROW(x.*,ARRAY[1,2,3]),
331                ROW(y.*,ARRAY[4,5,6])] AS z
332       FROM generate_series(1,2) x,
333            generate_series(4,5) y) q;
334                             row_to_json                             
335 --------------------------------------------------------------------
336  {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
337  {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
338  {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
339  {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
340 (4 rows)
341
342 SELECT row_to_json(q,true)
343 FROM (SELECT $$a$$ || x AS b,
344          y AS c,
345          ARRAY[ROW(x.*,ARRAY[1,2,3]),
346                ROW(y.*,ARRAY[4,5,6])] AS z
347       FROM generate_series(1,2) x,
348            generate_series(4,5) y) q;
349                      row_to_json                     
350 -----------------------------------------------------
351  {"b":"a1",                                         +
352   "c":4,                                            +
353   "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
354  {"b":"a1",                                         +
355   "c":5,                                            +
356   "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
357  {"b":"a2",                                         +
358   "c":4,                                            +
359   "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
360  {"b":"a2",                                         +
361   "c":5,                                            +
362   "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
363 (4 rows)
364
365 CREATE TEMP TABLE rows AS
366 SELECT x, 'txt' || x as y
367 FROM generate_series(1,3) AS x;
368 SELECT row_to_json(q,true)
369 FROM rows q;
370  row_to_json  
371 --------------
372  {"x":1,     +
373   "y":"txt1"}
374  {"x":2,     +
375   "y":"txt2"}
376  {"x":3,     +
377   "y":"txt3"}
378 (3 rows)
379
380 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
381       row_to_json      
382 -----------------------
383  {"f1":[5,6,7,8,9,10]}
384 (1 row)
385
386 -- to_json, timestamps
387 select to_json(timestamp '2014-05-28 12:22:35.614298');
388            to_json            
389 ------------------------------
390  "2014-05-28T12:22:35.614298"
391 (1 row)
392
393 BEGIN;
394 SET LOCAL TIME ZONE 10.5;
395 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
396               to_json               
397 ------------------------------------
398  "2014-05-29T02:52:35.614298+10:30"
399 (1 row)
400
401 SET LOCAL TIME ZONE -8;
402 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
403               to_json               
404 ------------------------------------
405  "2014-05-28T08:22:35.614298-08:00"
406 (1 row)
407
408 COMMIT;
409 select to_json(date '2014-05-28');
410    to_json    
411 --------------
412  "2014-05-28"
413 (1 row)
414
415 select to_json(date 'Infinity');
416   to_json   
417 ------------
418  "infinity"
419 (1 row)
420
421 select to_json(date '-Infinity');
422    to_json   
423 -------------
424  "-infinity"
425 (1 row)
426
427 select to_json(timestamp 'Infinity');
428   to_json   
429 ------------
430  "infinity"
431 (1 row)
432
433 select to_json(timestamp '-Infinity');
434    to_json   
435 -------------
436  "-infinity"
437 (1 row)
438
439 select to_json(timestamptz 'Infinity');
440   to_json   
441 ------------
442  "infinity"
443 (1 row)
444
445 select to_json(timestamptz '-Infinity');
446    to_json   
447 -------------
448  "-infinity"
449 (1 row)
450
451 --json_agg
452 SELECT json_agg(q)
453   FROM ( SELECT $$a$$ || x AS b, y AS c,
454                ARRAY[ROW(x.*,ARRAY[1,2,3]),
455                ROW(y.*,ARRAY[4,5,6])] AS z
456          FROM generate_series(1,2) x,
457               generate_series(4,5) y) q;
458                                json_agg                                
459 -----------------------------------------------------------------------
460  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
461   {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
462   {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
463   {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
464 (1 row)
465
466 SELECT json_agg(q ORDER BY x, y)
467   FROM rows q;
468        json_agg        
469 -----------------------
470  [{"x":1,"y":"txt1"}, +
471   {"x":2,"y":"txt2"}, +
472   {"x":3,"y":"txt3"}]
473 (1 row)
474
475 UPDATE rows SET x = NULL WHERE x = 1;
476 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
477   FROM rows q;
478          json_agg         
479 --------------------------
480  [{"x":null,"y":"txt1"}, +
481   {"x":2,"y":"txt2"},    +
482   {"x":3,"y":"txt3"}]
483 (1 row)
484
485 -- non-numeric output
486 SELECT row_to_json(q)
487 FROM (SELECT 'NaN'::float8 AS "float8field") q;
488       row_to_json      
489 -----------------------
490  {"float8field":"NaN"}
491 (1 row)
492
493 SELECT row_to_json(q)
494 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
495         row_to_json         
496 ----------------------------
497  {"float8field":"Infinity"}
498 (1 row)
499
500 SELECT row_to_json(q)
501 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
502          row_to_json         
503 -----------------------------
504  {"float8field":"-Infinity"}
505 (1 row)
506
507 -- json input
508 SELECT row_to_json(q)
509 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
510                            row_to_json                            
511 ------------------------------------------------------------------
512  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
513 (1 row)
514
515 -- json extraction functions
516 CREATE TEMP TABLE test_json (
517        json_type text,
518        test_json json
519 );
520 INSERT INTO test_json VALUES
521 ('scalar','"a scalar"'),
522 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
523 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
524 SELECT test_json -> 'x'
525 FROM test_json
526 WHERE json_type = 'scalar';
527  ?column? 
528 ----------
529  
530 (1 row)
531
532 SELECT test_json -> 'x'
533 FROM test_json
534 WHERE json_type = 'array';
535  ?column? 
536 ----------
537  
538 (1 row)
539
540 SELECT test_json -> 'x'
541 FROM test_json
542 WHERE json_type = 'object';
543  ?column? 
544 ----------
545  
546 (1 row)
547
548 SELECT test_json->'field2'
549 FROM test_json
550 WHERE json_type = 'object';
551  ?column? 
552 ----------
553  "val2"
554 (1 row)
555
556 SELECT test_json->>'field2'
557 FROM test_json
558 WHERE json_type = 'object';
559  ?column? 
560 ----------
561  val2
562 (1 row)
563
564 SELECT test_json -> 2
565 FROM test_json
566 WHERE json_type = 'scalar';
567  ?column? 
568 ----------
569  
570 (1 row)
571
572 SELECT test_json -> 2
573 FROM test_json
574 WHERE json_type = 'array';
575  ?column? 
576 ----------
577  "two"
578 (1 row)
579
580 SELECT test_json -> -1
581 FROM test_json
582 WHERE json_type = 'array';
583  ?column? 
584 ----------
585  {"f1":9}
586 (1 row)
587
588 SELECT test_json -> 2
589 FROM test_json
590 WHERE json_type = 'object';
591  ?column? 
592 ----------
593  
594 (1 row)
595
596 SELECT test_json->>2
597 FROM test_json
598 WHERE json_type = 'array';
599  ?column? 
600 ----------
601  two
602 (1 row)
603
604 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
605  ?column? 
606 ----------
607  [1,2,3]
608 (1 row)
609
610 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
611  ?column? 
612 ----------
613  {"f1":9}
614 (1 row)
615
616 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
617  ?column? 
618 ----------
619  4
620 (1 row)
621
622 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
623  ?column? 
624 ----------
625  [1,2,3]
626 (1 row)
627
628 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
629  ?column? 
630 ----------
631  {"f1":9}
632 (1 row)
633
634 SELECT json_object_keys(test_json)
635 FROM test_json
636 WHERE json_type = 'scalar';
637 ERROR:  cannot call json_object_keys on a scalar
638 SELECT json_object_keys(test_json)
639 FROM test_json
640 WHERE json_type = 'array';
641 ERROR:  cannot call json_object_keys on an array
642 SELECT json_object_keys(test_json)
643 FROM test_json
644 WHERE json_type = 'object';
645  json_object_keys 
646 ------------------
647  field1
648  field2
649  field3
650  field4
651  field5
652  field6
653 (6 rows)
654
655 -- test extending object_keys resultset - initial resultset size is 256
656 select count(*) from
657     (select json_object_keys(json_object(array_agg(g)))
658      from (select unnest(array['f'||n,n::text])as g
659            from generate_series(1,300) as n) x ) y;
660  count 
661 -------
662    300
663 (1 row)
664
665 -- nulls
666 select (test_json->'field3') is null as expect_false
667 from test_json
668 where json_type = 'object';
669  expect_false 
670 --------------
671  f
672 (1 row)
673
674 select (test_json->>'field3') is null as expect_true
675 from test_json
676 where json_type = 'object';
677  expect_true 
678 -------------
679  t
680 (1 row)
681
682 select (test_json->3) is null as expect_false
683 from test_json
684 where json_type = 'array';
685  expect_false 
686 --------------
687  f
688 (1 row)
689
690 select (test_json->>3) is null as expect_true
691 from test_json
692 where json_type = 'array';
693  expect_true 
694 -------------
695  t
696 (1 row)
697
698 -- corner cases
699 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
700  ?column? 
701 ----------
702  
703 (1 row)
704
705 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
706  ?column? 
707 ----------
708  
709 (1 row)
710
711 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
712  ?column? 
713 ----------
714  
715 (1 row)
716
717 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
718  ?column? 
719 ----------
720  
721 (1 row)
722
723 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
724  ?column? 
725 ----------
726  
727 (1 row)
728
729 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
730  ?column? 
731 ----------
732  
733 (1 row)
734
735 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
736   ?column?   
737 -------------
738  {"b": "cc"}
739 (1 row)
740
741 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
742  ?column? 
743 ----------
744  
745 (1 row)
746
747 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
748  ?column? 
749 ----------
750  
751 (1 row)
752
753 select '{"a": "c", "b": null}'::json -> 'b';
754  ?column? 
755 ----------
756  null
757 (1 row)
758
759 select '"foo"'::json -> 1;
760  ?column? 
761 ----------
762  
763 (1 row)
764
765 select '"foo"'::json -> 'z';
766  ?column? 
767 ----------
768  
769 (1 row)
770
771 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
772  ?column? 
773 ----------
774  
775 (1 row)
776
777 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
778  ?column? 
779 ----------
780  
781 (1 row)
782
783 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
784  ?column? 
785 ----------
786  
787 (1 row)
788
789 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
790  ?column? 
791 ----------
792  
793 (1 row)
794
795 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
796  ?column? 
797 ----------
798  
799 (1 row)
800
801 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
802   ?column?   
803 -------------
804  {"b": "cc"}
805 (1 row)
806
807 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
808  ?column? 
809 ----------
810  
811 (1 row)
812
813 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
814  ?column? 
815 ----------
816  
817 (1 row)
818
819 select '{"a": "c", "b": null}'::json ->> 'b';
820  ?column? 
821 ----------
822  
823 (1 row)
824
825 select '"foo"'::json ->> 1;
826  ?column? 
827 ----------
828  
829 (1 row)
830
831 select '"foo"'::json ->> 'z';
832  ?column? 
833 ----------
834  
835 (1 row)
836
837 -- array length
838 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
839  json_array_length 
840 -------------------
841                  5
842 (1 row)
843
844 SELECT json_array_length('[]');
845  json_array_length 
846 -------------------
847                  0
848 (1 row)
849
850 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
851 ERROR:  cannot get array length of a non-array
852 SELECT json_array_length('4');
853 ERROR:  cannot get array length of a scalar
854 -- each
855 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
856      json_each     
857 -------------------
858  (f1,"[1,2,3]")
859  (f2,"{""f3"":1}")
860  (f4,null)
861 (3 rows)
862
863 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
864  key |   value   
865 -----+-----------
866  f1  | [1,2,3]
867  f2  | {"f3":1}
868  f4  | null
869  f5  | 99
870  f6  | "stringy"
871 (5 rows)
872
873 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
874   json_each_text   
875 -------------------
876  (f1,"[1,2,3]")
877  (f2,"{""f3"":1}")
878  (f4,)
879  (f5,null)
880 (4 rows)
881
882 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
883  key |  value   
884 -----+----------
885  f1  | [1,2,3]
886  f2  | {"f3":1}
887  f4  | 
888  f5  | 99
889  f6  | stringy
890 (5 rows)
891
892 -- extract_path, extract_path_as_text
893 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
894  json_extract_path 
895 -------------------
896  "stringy"
897 (1 row)
898
899 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
900  json_extract_path 
901 -------------------
902  {"f3":1}
903 (1 row)
904
905 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
906  json_extract_path 
907 -------------------
908  "f3"
909 (1 row)
910
911 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
912  json_extract_path 
913 -------------------
914  1
915 (1 row)
916
917 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
918  json_extract_path_text 
919 ------------------------
920  stringy
921 (1 row)
922
923 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
924  json_extract_path_text 
925 ------------------------
926  {"f3":1}
927 (1 row)
928
929 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
930  json_extract_path_text 
931 ------------------------
932  f3
933 (1 row)
934
935 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
936  json_extract_path_text 
937 ------------------------
938  1
939 (1 row)
940
941 -- extract_path nulls
942 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
943  expect_false 
944 --------------
945  f
946 (1 row)
947
948 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
949  expect_true 
950 -------------
951  t
952 (1 row)
953
954 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
955  expect_false 
956 --------------
957  f
958 (1 row)
959
960 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
961  expect_true 
962 -------------
963  t
964 (1 row)
965
966 -- extract_path operators
967 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
968  ?column?  
969 -----------
970  "stringy"
971 (1 row)
972
973 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
974  ?column? 
975 ----------
976  {"f3":1}
977 (1 row)
978
979 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
980  ?column? 
981 ----------
982  "f3"
983 (1 row)
984
985 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
986  ?column? 
987 ----------
988  1
989 (1 row)
990
991 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
992  ?column? 
993 ----------
994  stringy
995 (1 row)
996
997 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
998  ?column? 
999 ----------
1000  {"f3":1}
1001 (1 row)
1002
1003 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1004  ?column? 
1005 ----------
1006  f3
1007 (1 row)
1008
1009 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1010  ?column? 
1011 ----------
1012  1
1013 (1 row)
1014
1015 -- corner cases for same
1016 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1017          ?column?          
1018 ---------------------------
1019  {"a": {"b":{"c": "foo"}}}
1020 (1 row)
1021
1022 select '[1,2,3]'::json #> '{}';
1023  ?column? 
1024 ----------
1025  [1,2,3]
1026 (1 row)
1027
1028 select '"foo"'::json #> '{}';
1029  ?column? 
1030 ----------
1031  "foo"
1032 (1 row)
1033
1034 select '42'::json #> '{}';
1035  ?column? 
1036 ----------
1037  42
1038 (1 row)
1039
1040 select 'null'::json #> '{}';
1041  ?column? 
1042 ----------
1043  null
1044 (1 row)
1045
1046 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1047       ?column?      
1048 --------------------
1049  {"b":{"c": "foo"}}
1050 (1 row)
1051
1052 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1053  ?column? 
1054 ----------
1055  
1056 (1 row)
1057
1058 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1059  ?column? 
1060 ----------
1061  
1062 (1 row)
1063
1064 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1065    ?column?   
1066 --------------
1067  {"c": "foo"}
1068 (1 row)
1069
1070 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1071  ?column? 
1072 ----------
1073  "foo"
1074 (1 row)
1075
1076 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1077  ?column? 
1078 ----------
1079  
1080 (1 row)
1081
1082 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1083  ?column? 
1084 ----------
1085  
1086 (1 row)
1087
1088 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1089  ?column? 
1090 ----------
1091  "cc"
1092 (1 row)
1093
1094 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1095  ?column? 
1096 ----------
1097  
1098 (1 row)
1099
1100 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1101  ?column? 
1102 ----------
1103  "cc"
1104 (1 row)
1105
1106 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1107  ?column? 
1108 ----------
1109  
1110 (1 row)
1111
1112 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1113  ?column? 
1114 ----------
1115  null
1116 (1 row)
1117
1118 select '"foo"'::json #> array['z'];
1119  ?column? 
1120 ----------
1121  
1122 (1 row)
1123
1124 select '42'::json #> array['f2'];
1125  ?column? 
1126 ----------
1127  
1128 (1 row)
1129
1130 select '42'::json #> array['0'];
1131  ?column? 
1132 ----------
1133  
1134 (1 row)
1135
1136 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1137          ?column?          
1138 ---------------------------
1139  {"a": {"b":{"c": "foo"}}}
1140 (1 row)
1141
1142 select '[1,2,3]'::json #>> '{}';
1143  ?column? 
1144 ----------
1145  [1,2,3]
1146 (1 row)
1147
1148 select '"foo"'::json #>> '{}';
1149  ?column? 
1150 ----------
1151  foo
1152 (1 row)
1153
1154 select '42'::json #>> '{}';
1155  ?column? 
1156 ----------
1157  42
1158 (1 row)
1159
1160 select 'null'::json #>> '{}';
1161  ?column? 
1162 ----------
1163  
1164 (1 row)
1165
1166 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1167       ?column?      
1168 --------------------
1169  {"b":{"c": "foo"}}
1170 (1 row)
1171
1172 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1173  ?column? 
1174 ----------
1175  
1176 (1 row)
1177
1178 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1179  ?column? 
1180 ----------
1181  
1182 (1 row)
1183
1184 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1185    ?column?   
1186 --------------
1187  {"c": "foo"}
1188 (1 row)
1189
1190 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1191  ?column? 
1192 ----------
1193  foo
1194 (1 row)
1195
1196 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1197  ?column? 
1198 ----------
1199  
1200 (1 row)
1201
1202 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1203  ?column? 
1204 ----------
1205  
1206 (1 row)
1207
1208 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1209  ?column? 
1210 ----------
1211  cc
1212 (1 row)
1213
1214 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1215  ?column? 
1216 ----------
1217  
1218 (1 row)
1219
1220 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1221  ?column? 
1222 ----------
1223  cc
1224 (1 row)
1225
1226 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1227  ?column? 
1228 ----------
1229  
1230 (1 row)
1231
1232 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1233  ?column? 
1234 ----------
1235  
1236 (1 row)
1237
1238 select '"foo"'::json #>> array['z'];
1239  ?column? 
1240 ----------
1241  
1242 (1 row)
1243
1244 select '42'::json #>> array['f2'];
1245  ?column? 
1246 ----------
1247  
1248 (1 row)
1249
1250 select '42'::json #>> array['0'];
1251  ?column? 
1252 ----------
1253  
1254 (1 row)
1255
1256 -- array_elements
1257 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1258   json_array_elements  
1259 -----------------------
1260  1
1261  true
1262  [1,[2,3]]
1263  null
1264  {"f1":1,"f2":[7,8,9]}
1265  false
1266  "stringy"
1267 (7 rows)
1268
1269 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1270          value         
1271 -----------------------
1272  1
1273  true
1274  [1,[2,3]]
1275  null
1276  {"f1":1,"f2":[7,8,9]}
1277  false
1278  "stringy"
1279 (7 rows)
1280
1281 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1282  json_array_elements_text 
1283 --------------------------
1284  1
1285  true
1286  [1,[2,3]]
1287  
1288  {"f1":1,"f2":[7,8,9]}
1289  false
1290  stringy
1291 (7 rows)
1292
1293 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1294          value         
1295 -----------------------
1296  1
1297  true
1298  [1,[2,3]]
1299  
1300  {"f1":1,"f2":[7,8,9]}
1301  false
1302  stringy
1303 (7 rows)
1304
1305 -- populate_record
1306 create type jpop as (a text, b int, c timestamp);
1307 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1308    a    | b | c 
1309 --------+---+---
1310  blurfl |   | 
1311 (1 row)
1312
1313 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1314    a    | b |            c             
1315 --------+---+--------------------------
1316  blurfl | 3 | Mon Dec 31 15:30:56 2012
1317 (1 row)
1318
1319 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1320    a    | b | c 
1321 --------+---+---
1322  blurfl |   | 
1323 (1 row)
1324
1325 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1326    a    | b |            c             
1327 --------+---+--------------------------
1328  blurfl | 3 | Mon Dec 31 15:30:56 2012
1329 (1 row)
1330
1331 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1332         a        | b | c 
1333 -----------------+---+---
1334  [100,200,false] |   | 
1335 (1 row)
1336
1337 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1338         a        | b |            c             
1339 -----------------+---+--------------------------
1340  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1341 (1 row)
1342
1343 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1344 ERROR:  invalid input syntax for type timestamp: "[100,200,false]"
1345 -- populate_recordset
1346 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1347    a    | b |            c             
1348 --------+---+--------------------------
1349  blurfl |   | 
1350         | 3 | Fri Jan 20 10:42:53 2012
1351 (2 rows)
1352
1353 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;
1354    a    | b  |            c             
1355 --------+----+--------------------------
1356  blurfl | 99 | 
1357  def    |  3 | Fri Jan 20 10:42:53 2012
1358 (2 rows)
1359
1360 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1361    a    | b |            c             
1362 --------+---+--------------------------
1363  blurfl |   | 
1364         | 3 | Fri Jan 20 10:42:53 2012
1365 (2 rows)
1366
1367 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;
1368    a    | b  |            c             
1369 --------+----+--------------------------
1370  blurfl | 99 | 
1371  def    |  3 | Fri Jan 20 10:42:53 2012
1372 (2 rows)
1373
1374 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;
1375        a       | b  |            c             
1376 ---------------+----+--------------------------
1377  [100,200,300] | 99 | 
1378  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1379 (2 rows)
1380
1381 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;
1382 ERROR:  invalid input syntax for type timestamp: "[100,200,300]"
1383 create type jpop2 as (a int, b json, c int, d int);
1384 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1385  a |    b    | c | d 
1386 ---+---------+---+---
1387  2 | {"z":4} | 3 | 6
1388 (1 row)
1389
1390 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1391    a    | b |            c             
1392 --------+---+--------------------------
1393  blurfl |   | 
1394         | 3 | Fri Jan 20 10:42:53 2012
1395 (2 rows)
1396
1397 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;
1398    a    | b  |            c             
1399 --------+----+--------------------------
1400  blurfl | 99 | 
1401  def    |  3 | Fri Jan 20 10:42:53 2012
1402 (2 rows)
1403
1404 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;
1405        a       | b  |            c             
1406 ---------------+----+--------------------------
1407  [100,200,300] | 99 | 
1408  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1409 (2 rows)
1410
1411 --json_typeof() function
1412 select value, json_typeof(value)
1413   from (values (json '123.4'),
1414                (json '-1'),
1415                (json '"foo"'),
1416                (json 'true'),
1417                (json 'false'),
1418                (json 'null'),
1419                (json '[1, 2, 3]'),
1420                (json '[]'),
1421                (json '{"x":"foo", "y":123}'),
1422                (json '{}'),
1423                (NULL::json))
1424       as data(value);
1425         value         | json_typeof 
1426 ----------------------+-------------
1427  123.4                | number
1428  -1                   | number
1429  "foo"                | string
1430  true                 | boolean
1431  false                | boolean
1432  null                 | null
1433  [1, 2, 3]            | array
1434  []                   | array
1435  {"x":"foo", "y":123} | object
1436  {}                   | object
1437                       | 
1438 (11 rows)
1439
1440 -- json_build_array, json_build_object, json_object_agg
1441 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1442                            json_build_array                            
1443 -----------------------------------------------------------------------
1444  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1445 (1 row)
1446
1447 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1448                              json_build_object                              
1449 ----------------------------------------------------------------------------
1450  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1451 (1 row)
1452
1453 SELECT json_build_object(
1454        'a', json_build_object('b',false,'c',99),
1455        'd', json_build_object('e',array[9,8,7]::int[],
1456            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1457                                         json_build_object                                        
1458 -------------------------------------------------------------------------------------------------
1459  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1460 (1 row)
1461
1462 -- empty objects/arrays
1463 SELECT json_build_array();
1464  json_build_array 
1465 ------------------
1466  []
1467 (1 row)
1468
1469 SELECT json_build_object();
1470  json_build_object 
1471 -------------------
1472  {}
1473 (1 row)
1474
1475 -- make sure keys are quoted
1476 SELECT json_build_object(1,2);
1477  json_build_object 
1478 -------------------
1479  {"1" : 2}
1480 (1 row)
1481
1482 -- keys must be scalar and not null
1483 SELECT json_build_object(null,2);
1484 ERROR:  argument 1 cannot be null
1485 HINT:  Object keys should be text.
1486 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1487 ERROR:  key value must be scalar, not array, composite, or json
1488 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1489 ERROR:  key value must be scalar, not array, composite, or json
1490 SELECT json_build_object('{1,2,3}'::int[], 3);
1491 ERROR:  key value must be scalar, not array, composite, or json
1492 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1493 INSERT INTO foo VALUES (847001,'t15','GE1043');
1494 INSERT INTO foo VALUES (847002,'t16','GE1043');
1495 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1496 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1497 FROM foo;
1498                                                                             json_build_object                                                                            
1499 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1500  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1501 (1 row)
1502
1503 SELECT json_object_agg(name, type) FROM foo;
1504                         json_object_agg                         
1505 ----------------------------------------------------------------
1506  { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1507 (1 row)
1508
1509 INSERT INTO foo VALUES (999999, NULL, 'bar');
1510 SELECT json_object_agg(name, type) FROM foo;
1511 ERROR:  field name must not be null
1512 -- json_object
1513 -- one dimension
1514 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1515                       json_object                      
1516 -------------------------------------------------------
1517  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1518 (1 row)
1519
1520 -- same but with two dimensions
1521 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1522                       json_object                      
1523 -------------------------------------------------------
1524  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1525 (1 row)
1526
1527 -- odd number error
1528 SELECT json_object('{a,b,c}');
1529 ERROR:  array must have even number of elements
1530 -- one column error
1531 SELECT json_object('{{a},{b}}');
1532 ERROR:  array must have two columns
1533 -- too many columns error
1534 SELECT json_object('{{a,b,c},{b,c,d}}');
1535 ERROR:  array must have two columns
1536 -- too many dimensions error
1537 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1538 ERROR:  wrong number of array subscripts
1539 --two argument form of json_object
1540 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1541                      json_object                      
1542 ------------------------------------------------------
1543  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1544 (1 row)
1545
1546 -- too many dimensions
1547 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"}}');
1548 ERROR:  wrong number of array subscripts
1549 -- mismatched dimensions
1550 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1551 ERROR:  mismatched array dimensions
1552 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1553 ERROR:  mismatched array dimensions
1554 -- null key error
1555 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1556 ERROR:  null value not allowed for object key
1557 -- empty key is allowed
1558 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1559                      json_object                     
1560 -----------------------------------------------------
1561  {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1562 (1 row)
1563
1564 -- json_to_record and json_to_recordset
1565 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1566     as x(a int, b text, d text);
1567  a |  b  | d 
1568 ---+-----+---
1569  1 | foo | 
1570 (1 row)
1571
1572 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1573     as x(a int, b text, c boolean);
1574  a |  b  | c 
1575 ---+-----+---
1576  1 | foo | 
1577  2 | bar | t
1578 (2 rows)
1579
1580 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1581     as x(a int, b json, c boolean);
1582  a |      b      | c 
1583 ---+-------------+---
1584  1 | {"d":"foo"} | t
1585  2 | {"d":"bar"} | f
1586 (2 rows)
1587
1588 -- json_strip_nulls
1589 select json_strip_nulls(null);
1590  json_strip_nulls 
1591 ------------------
1592  
1593 (1 row)
1594
1595 select json_strip_nulls('1');
1596  json_strip_nulls 
1597 ------------------
1598  1
1599 (1 row)
1600
1601 select json_strip_nulls('"a string"');
1602  json_strip_nulls 
1603 ------------------
1604  "a string"
1605 (1 row)
1606
1607 select json_strip_nulls('null');
1608  json_strip_nulls 
1609 ------------------
1610  null
1611 (1 row)
1612
1613 select json_strip_nulls('[1,2,null,3,4]');
1614  json_strip_nulls 
1615 ------------------
1616  [1,2,null,3,4]
1617 (1 row)
1618
1619 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1620           json_strip_nulls          
1621 ------------------------------------
1622  {"a":1,"c":[2,null,3],"d":{"e":4}}
1623 (1 row)
1624
1625 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1626   json_strip_nulls   
1627 ---------------------
1628  [1,{"a":1,"c":2},3]
1629 (1 row)
1630
1631 -- an empty object is not null and should not be stripped
1632 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
1633  json_strip_nulls 
1634 ------------------
1635  {"a":{},"d":{}}
1636 (1 row)
1637