]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Factor out encoding specific tests for 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 -- 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(timestamp 'Infinity');
422   to_json   
423 ------------
424  "infinity"
425 (1 row)
426
427 select to_json(timestamptz 'Infinity');
428   to_json   
429 ------------
430  "infinity"
431 (1 row)
432
433 --json_agg
434 SELECT json_agg(q)
435   FROM ( SELECT $$a$$ || x AS b, y AS c,
436                ARRAY[ROW(x.*,ARRAY[1,2,3]),
437                ROW(y.*,ARRAY[4,5,6])] AS z
438          FROM generate_series(1,2) x,
439               generate_series(4,5) y) q;
440                                json_agg                                
441 -----------------------------------------------------------------------
442  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
443   {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
444   {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
445   {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
446 (1 row)
447
448 SELECT json_agg(q ORDER BY x, y)
449   FROM rows q;
450        json_agg        
451 -----------------------
452  [{"x":1,"y":"txt1"}, +
453   {"x":2,"y":"txt2"}, +
454   {"x":3,"y":"txt3"}]
455 (1 row)
456
457 UPDATE rows SET x = NULL WHERE x = 1;
458 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
459   FROM rows q;
460          json_agg         
461 --------------------------
462  [{"x":null,"y":"txt1"}, +
463   {"x":2,"y":"txt2"},    +
464   {"x":3,"y":"txt3"}]
465 (1 row)
466
467 -- non-numeric output
468 SELECT row_to_json(q)
469 FROM (SELECT 'NaN'::float8 AS "float8field") q;
470       row_to_json      
471 -----------------------
472  {"float8field":"NaN"}
473 (1 row)
474
475 SELECT row_to_json(q)
476 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
477         row_to_json         
478 ----------------------------
479  {"float8field":"Infinity"}
480 (1 row)
481
482 SELECT row_to_json(q)
483 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
484          row_to_json         
485 -----------------------------
486  {"float8field":"-Infinity"}
487 (1 row)
488
489 -- json input
490 SELECT row_to_json(q)
491 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
492                            row_to_json                            
493 ------------------------------------------------------------------
494  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
495 (1 row)
496
497 -- json extraction functions
498 CREATE TEMP TABLE test_json (
499        json_type text,
500        test_json json
501 );
502 INSERT INTO test_json VALUES
503 ('scalar','"a scalar"'),
504 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
505 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
506 SELECT test_json -> 'x'
507 FROM test_json
508 WHERE json_type = 'scalar';
509  ?column? 
510 ----------
511  
512 (1 row)
513
514 SELECT test_json -> 'x'
515 FROM test_json
516 WHERE json_type = 'array';
517  ?column? 
518 ----------
519  
520 (1 row)
521
522 SELECT test_json -> 'x'
523 FROM test_json
524 WHERE json_type = 'object';
525  ?column? 
526 ----------
527  
528 (1 row)
529
530 SELECT test_json->'field2'
531 FROM test_json
532 WHERE json_type = 'object';
533  ?column? 
534 ----------
535  "val2"
536 (1 row)
537
538 SELECT test_json->>'field2'
539 FROM test_json
540 WHERE json_type = 'object';
541  ?column? 
542 ----------
543  val2
544 (1 row)
545
546 SELECT test_json -> 2
547 FROM test_json
548 WHERE json_type = 'scalar';
549  ?column? 
550 ----------
551  
552 (1 row)
553
554 SELECT test_json -> 2
555 FROM test_json
556 WHERE json_type = 'array';
557  ?column? 
558 ----------
559  "two"
560 (1 row)
561
562 SELECT test_json -> -1
563 FROM test_json
564 WHERE json_type = 'array';
565  ?column? 
566 ----------
567  {"f1":9}
568 (1 row)
569
570 SELECT test_json -> 2
571 FROM test_json
572 WHERE json_type = 'object';
573  ?column? 
574 ----------
575  
576 (1 row)
577
578 SELECT test_json->>2
579 FROM test_json
580 WHERE json_type = 'array';
581  ?column? 
582 ----------
583  two
584 (1 row)
585
586 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
587  ?column? 
588 ----------
589  [1,2,3]
590 (1 row)
591
592 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
593  ?column? 
594 ----------
595  {"f1":9}
596 (1 row)
597
598 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
599  ?column? 
600 ----------
601  4
602 (1 row)
603
604 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
605  ?column? 
606 ----------
607  [1,2,3]
608 (1 row)
609
610 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
611  ?column? 
612 ----------
613  {"f1":9}
614 (1 row)
615
616 SELECT json_object_keys(test_json)
617 FROM test_json
618 WHERE json_type = 'scalar';
619 ERROR:  cannot call json_object_keys on a scalar
620 SELECT json_object_keys(test_json)
621 FROM test_json
622 WHERE json_type = 'array';
623 ERROR:  cannot call json_object_keys on an array
624 SELECT json_object_keys(test_json)
625 FROM test_json
626 WHERE json_type = 'object';
627  json_object_keys 
628 ------------------
629  field1
630  field2
631  field3
632  field4
633  field5
634  field6
635 (6 rows)
636
637 -- test extending object_keys resultset - initial resultset size is 256
638 select count(*) from
639     (select json_object_keys(json_object(array_agg(g)))
640      from (select unnest(array['f'||n,n::text])as g
641            from generate_series(1,300) as n) x ) y;
642  count 
643 -------
644    300
645 (1 row)
646
647 -- nulls
648 select (test_json->'field3') is null as expect_false
649 from test_json
650 where json_type = 'object';
651  expect_false 
652 --------------
653  f
654 (1 row)
655
656 select (test_json->>'field3') is null as expect_true
657 from test_json
658 where json_type = 'object';
659  expect_true 
660 -------------
661  t
662 (1 row)
663
664 select (test_json->3) is null as expect_false
665 from test_json
666 where json_type = 'array';
667  expect_false 
668 --------------
669  f
670 (1 row)
671
672 select (test_json->>3) is null as expect_true
673 from test_json
674 where json_type = 'array';
675  expect_true 
676 -------------
677  t
678 (1 row)
679
680 -- corner cases
681 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
682  ?column? 
683 ----------
684  
685 (1 row)
686
687 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
688  ?column? 
689 ----------
690  
691 (1 row)
692
693 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
694  ?column? 
695 ----------
696  
697 (1 row)
698
699 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
700  ?column? 
701 ----------
702  
703 (1 row)
704
705 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
706  ?column? 
707 ----------
708  
709 (1 row)
710
711 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
712  ?column? 
713 ----------
714  
715 (1 row)
716
717 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
718   ?column?   
719 -------------
720  {"b": "cc"}
721 (1 row)
722
723 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
724  ?column? 
725 ----------
726  
727 (1 row)
728
729 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
730  ?column? 
731 ----------
732  
733 (1 row)
734
735 select '{"a": "c", "b": null}'::json -> 'b';
736  ?column? 
737 ----------
738  null
739 (1 row)
740
741 select '"foo"'::json -> 1;
742  ?column? 
743 ----------
744  
745 (1 row)
746
747 select '"foo"'::json -> 'z';
748  ?column? 
749 ----------
750  
751 (1 row)
752
753 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
754  ?column? 
755 ----------
756  
757 (1 row)
758
759 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
760  ?column? 
761 ----------
762  
763 (1 row)
764
765 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
766  ?column? 
767 ----------
768  
769 (1 row)
770
771 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
772  ?column? 
773 ----------
774  
775 (1 row)
776
777 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
778  ?column? 
779 ----------
780  
781 (1 row)
782
783 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
784   ?column?   
785 -------------
786  {"b": "cc"}
787 (1 row)
788
789 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
790  ?column? 
791 ----------
792  
793 (1 row)
794
795 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
796  ?column? 
797 ----------
798  
799 (1 row)
800
801 select '{"a": "c", "b": null}'::json ->> 'b';
802  ?column? 
803 ----------
804  
805 (1 row)
806
807 select '"foo"'::json ->> 1;
808  ?column? 
809 ----------
810  
811 (1 row)
812
813 select '"foo"'::json ->> 'z';
814  ?column? 
815 ----------
816  
817 (1 row)
818
819 -- array length
820 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
821  json_array_length 
822 -------------------
823                  5
824 (1 row)
825
826 SELECT json_array_length('[]');
827  json_array_length 
828 -------------------
829                  0
830 (1 row)
831
832 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
833 ERROR:  cannot get array length of a non-array
834 SELECT json_array_length('4');
835 ERROR:  cannot get array length of a scalar
836 -- each
837 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
838      json_each     
839 -------------------
840  (f1,"[1,2,3]")
841  (f2,"{""f3"":1}")
842  (f4,null)
843 (3 rows)
844
845 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
846  key |   value   
847 -----+-----------
848  f1  | [1,2,3]
849  f2  | {"f3":1}
850  f4  | null
851  f5  | 99
852  f6  | "stringy"
853 (5 rows)
854
855 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
856   json_each_text   
857 -------------------
858  (f1,"[1,2,3]")
859  (f2,"{""f3"":1}")
860  (f4,)
861  (f5,null)
862 (4 rows)
863
864 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
865  key |  value   
866 -----+----------
867  f1  | [1,2,3]
868  f2  | {"f3":1}
869  f4  | 
870  f5  | 99
871  f6  | stringy
872 (5 rows)
873
874 -- extract_path, extract_path_as_text
875 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
876  json_extract_path 
877 -------------------
878  "stringy"
879 (1 row)
880
881 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
882  json_extract_path 
883 -------------------
884  {"f3":1}
885 (1 row)
886
887 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
888  json_extract_path 
889 -------------------
890  "f3"
891 (1 row)
892
893 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
894  json_extract_path 
895 -------------------
896  1
897 (1 row)
898
899 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
900  json_extract_path_text 
901 ------------------------
902  stringy
903 (1 row)
904
905 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
906  json_extract_path_text 
907 ------------------------
908  {"f3":1}
909 (1 row)
910
911 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
912  json_extract_path_text 
913 ------------------------
914  f3
915 (1 row)
916
917 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
918  json_extract_path_text 
919 ------------------------
920  1
921 (1 row)
922
923 -- extract_path nulls
924 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
925  expect_false 
926 --------------
927  f
928 (1 row)
929
930 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
931  expect_true 
932 -------------
933  t
934 (1 row)
935
936 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
937  expect_false 
938 --------------
939  f
940 (1 row)
941
942 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
943  expect_true 
944 -------------
945  t
946 (1 row)
947
948 -- extract_path operators
949 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
950  ?column?  
951 -----------
952  "stringy"
953 (1 row)
954
955 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
956  ?column? 
957 ----------
958  {"f3":1}
959 (1 row)
960
961 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
962  ?column? 
963 ----------
964  "f3"
965 (1 row)
966
967 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
968  ?column? 
969 ----------
970  1
971 (1 row)
972
973 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
974  ?column? 
975 ----------
976  stringy
977 (1 row)
978
979 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
980  ?column? 
981 ----------
982  {"f3":1}
983 (1 row)
984
985 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
986  ?column? 
987 ----------
988  f3
989 (1 row)
990
991 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
992  ?column? 
993 ----------
994  1
995 (1 row)
996
997 -- corner cases for same
998 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
999          ?column?          
1000 ---------------------------
1001  {"a": {"b":{"c": "foo"}}}
1002 (1 row)
1003
1004 select '[1,2,3]'::json #> '{}';
1005  ?column? 
1006 ----------
1007  [1,2,3]
1008 (1 row)
1009
1010 select '"foo"'::json #> '{}';
1011  ?column? 
1012 ----------
1013  "foo"
1014 (1 row)
1015
1016 select '42'::json #> '{}';
1017  ?column? 
1018 ----------
1019  42
1020 (1 row)
1021
1022 select 'null'::json #> '{}';
1023  ?column? 
1024 ----------
1025  null
1026 (1 row)
1027
1028 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1029       ?column?      
1030 --------------------
1031  {"b":{"c": "foo"}}
1032 (1 row)
1033
1034 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1035  ?column? 
1036 ----------
1037  
1038 (1 row)
1039
1040 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1041  ?column? 
1042 ----------
1043  
1044 (1 row)
1045
1046 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1047    ?column?   
1048 --------------
1049  {"c": "foo"}
1050 (1 row)
1051
1052 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1053  ?column? 
1054 ----------
1055  "foo"
1056 (1 row)
1057
1058 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1059  ?column? 
1060 ----------
1061  
1062 (1 row)
1063
1064 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1065  ?column? 
1066 ----------
1067  
1068 (1 row)
1069
1070 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1071  ?column? 
1072 ----------
1073  "cc"
1074 (1 row)
1075
1076 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1077  ?column? 
1078 ----------
1079  
1080 (1 row)
1081
1082 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1083  ?column? 
1084 ----------
1085  "cc"
1086 (1 row)
1087
1088 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1089  ?column? 
1090 ----------
1091  
1092 (1 row)
1093
1094 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1095  ?column? 
1096 ----------
1097  null
1098 (1 row)
1099
1100 select '"foo"'::json #> array['z'];
1101  ?column? 
1102 ----------
1103  
1104 (1 row)
1105
1106 select '42'::json #> array['f2'];
1107  ?column? 
1108 ----------
1109  
1110 (1 row)
1111
1112 select '42'::json #> array['0'];
1113  ?column? 
1114 ----------
1115  
1116 (1 row)
1117
1118 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1119          ?column?          
1120 ---------------------------
1121  {"a": {"b":{"c": "foo"}}}
1122 (1 row)
1123
1124 select '[1,2,3]'::json #>> '{}';
1125  ?column? 
1126 ----------
1127  [1,2,3]
1128 (1 row)
1129
1130 select '"foo"'::json #>> '{}';
1131  ?column? 
1132 ----------
1133  foo
1134 (1 row)
1135
1136 select '42'::json #>> '{}';
1137  ?column? 
1138 ----------
1139  42
1140 (1 row)
1141
1142 select 'null'::json #>> '{}';
1143  ?column? 
1144 ----------
1145  
1146 (1 row)
1147
1148 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1149       ?column?      
1150 --------------------
1151  {"b":{"c": "foo"}}
1152 (1 row)
1153
1154 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1155  ?column? 
1156 ----------
1157  
1158 (1 row)
1159
1160 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1161  ?column? 
1162 ----------
1163  
1164 (1 row)
1165
1166 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1167    ?column?   
1168 --------------
1169  {"c": "foo"}
1170 (1 row)
1171
1172 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1173  ?column? 
1174 ----------
1175  foo
1176 (1 row)
1177
1178 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1179  ?column? 
1180 ----------
1181  
1182 (1 row)
1183
1184 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1185  ?column? 
1186 ----------
1187  
1188 (1 row)
1189
1190 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1191  ?column? 
1192 ----------
1193  cc
1194 (1 row)
1195
1196 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1197  ?column? 
1198 ----------
1199  
1200 (1 row)
1201
1202 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1203  ?column? 
1204 ----------
1205  cc
1206 (1 row)
1207
1208 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1209  ?column? 
1210 ----------
1211  
1212 (1 row)
1213
1214 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1215  ?column? 
1216 ----------
1217  
1218 (1 row)
1219
1220 select '"foo"'::json #>> array['z'];
1221  ?column? 
1222 ----------
1223  
1224 (1 row)
1225
1226 select '42'::json #>> array['f2'];
1227  ?column? 
1228 ----------
1229  
1230 (1 row)
1231
1232 select '42'::json #>> array['0'];
1233  ?column? 
1234 ----------
1235  
1236 (1 row)
1237
1238 -- array_elements
1239 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1240   json_array_elements  
1241 -----------------------
1242  1
1243  true
1244  [1,[2,3]]
1245  null
1246  {"f1":1,"f2":[7,8,9]}
1247  false
1248  "stringy"
1249 (7 rows)
1250
1251 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1252          value         
1253 -----------------------
1254  1
1255  true
1256  [1,[2,3]]
1257  null
1258  {"f1":1,"f2":[7,8,9]}
1259  false
1260  "stringy"
1261 (7 rows)
1262
1263 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1264  json_array_elements_text 
1265 --------------------------
1266  1
1267  true
1268  [1,[2,3]]
1269  
1270  {"f1":1,"f2":[7,8,9]}
1271  false
1272  stringy
1273 (7 rows)
1274
1275 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1276          value         
1277 -----------------------
1278  1
1279  true
1280  [1,[2,3]]
1281  
1282  {"f1":1,"f2":[7,8,9]}
1283  false
1284  stringy
1285 (7 rows)
1286
1287 -- populate_record
1288 create type jpop as (a text, b int, c timestamp);
1289 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1290    a    | b | c 
1291 --------+---+---
1292  blurfl |   | 
1293 (1 row)
1294
1295 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1296    a    | b |            c             
1297 --------+---+--------------------------
1298  blurfl | 3 | Mon Dec 31 15:30:56 2012
1299 (1 row)
1300
1301 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1302    a    | b | c 
1303 --------+---+---
1304  blurfl |   | 
1305 (1 row)
1306
1307 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1308    a    | b |            c             
1309 --------+---+--------------------------
1310  blurfl | 3 | Mon Dec 31 15:30:56 2012
1311 (1 row)
1312
1313 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1314         a        | b | c 
1315 -----------------+---+---
1316  [100,200,false] |   | 
1317 (1 row)
1318
1319 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1320         a        | b |            c             
1321 -----------------+---+--------------------------
1322  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1323 (1 row)
1324
1325 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1326 ERROR:  invalid input syntax for type timestamp: "[100,200,false]"
1327 -- populate_recordset
1328 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1329    a    | b |            c             
1330 --------+---+--------------------------
1331  blurfl |   | 
1332         | 3 | Fri Jan 20 10:42:53 2012
1333 (2 rows)
1334
1335 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;
1336    a    | b  |            c             
1337 --------+----+--------------------------
1338  blurfl | 99 | 
1339  def    |  3 | Fri Jan 20 10:42:53 2012
1340 (2 rows)
1341
1342 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1343    a    | b |            c             
1344 --------+---+--------------------------
1345  blurfl |   | 
1346         | 3 | Fri Jan 20 10:42:53 2012
1347 (2 rows)
1348
1349 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;
1350    a    | b  |            c             
1351 --------+----+--------------------------
1352  blurfl | 99 | 
1353  def    |  3 | Fri Jan 20 10:42:53 2012
1354 (2 rows)
1355
1356 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;
1357        a       | b  |            c             
1358 ---------------+----+--------------------------
1359  [100,200,300] | 99 | 
1360  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1361 (2 rows)
1362
1363 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;
1364 ERROR:  invalid input syntax for type timestamp: "[100,200,300]"
1365 create type jpop2 as (a int, b json, c int, d int);
1366 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1367  a |    b    | c | d 
1368 ---+---------+---+---
1369  2 | {"z":4} | 3 | 6
1370 (1 row)
1371
1372 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1373    a    | b |            c             
1374 --------+---+--------------------------
1375  blurfl |   | 
1376         | 3 | Fri Jan 20 10:42:53 2012
1377 (2 rows)
1378
1379 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;
1380    a    | b  |            c             
1381 --------+----+--------------------------
1382  blurfl | 99 | 
1383  def    |  3 | Fri Jan 20 10:42:53 2012
1384 (2 rows)
1385
1386 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;
1387        a       | b  |            c             
1388 ---------------+----+--------------------------
1389  [100,200,300] | 99 | 
1390  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1391 (2 rows)
1392
1393 --json_typeof() function
1394 select value, json_typeof(value)
1395   from (values (json '123.4'),
1396                (json '-1'),
1397                (json '"foo"'),
1398                (json 'true'),
1399                (json 'false'),
1400                (json 'null'),
1401                (json '[1, 2, 3]'),
1402                (json '[]'),
1403                (json '{"x":"foo", "y":123}'),
1404                (json '{}'),
1405                (NULL::json))
1406       as data(value);
1407         value         | json_typeof 
1408 ----------------------+-------------
1409  123.4                | number
1410  -1                   | number
1411  "foo"                | string
1412  true                 | boolean
1413  false                | boolean
1414  null                 | null
1415  [1, 2, 3]            | array
1416  []                   | array
1417  {"x":"foo", "y":123} | object
1418  {}                   | object
1419                       | 
1420 (11 rows)
1421
1422 -- json_build_array, json_build_object, json_object_agg
1423 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1424                            json_build_array                            
1425 -----------------------------------------------------------------------
1426  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1427 (1 row)
1428
1429 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1430                              json_build_object                              
1431 ----------------------------------------------------------------------------
1432  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1433 (1 row)
1434
1435 SELECT json_build_object(
1436        'a', json_build_object('b',false,'c',99),
1437        'd', json_build_object('e',array[9,8,7]::int[],
1438            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1439                                         json_build_object                                        
1440 -------------------------------------------------------------------------------------------------
1441  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1442 (1 row)
1443
1444 -- empty objects/arrays
1445 SELECT json_build_array();
1446  json_build_array 
1447 ------------------
1448  []
1449 (1 row)
1450
1451 SELECT json_build_object();
1452  json_build_object 
1453 -------------------
1454  {}
1455 (1 row)
1456
1457 -- make sure keys are quoted
1458 SELECT json_build_object(1,2);
1459  json_build_object 
1460 -------------------
1461  {"1" : 2}
1462 (1 row)
1463
1464 -- keys must be scalar and not null
1465 SELECT json_build_object(null,2);
1466 ERROR:  argument 1 cannot be null
1467 HINT:  Object keys should be text.
1468 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1469 ERROR:  key value must be scalar, not array, composite, or json
1470 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1471 ERROR:  key value must be scalar, not array, composite, or json
1472 SELECT json_build_object('{1,2,3}'::int[], 3);
1473 ERROR:  key value must be scalar, not array, composite, or json
1474 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1475 INSERT INTO foo VALUES (847001,'t15','GE1043');
1476 INSERT INTO foo VALUES (847002,'t16','GE1043');
1477 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1478 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1479 FROM foo;
1480                                                                             json_build_object                                                                            
1481 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1482  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1483 (1 row)
1484
1485 SELECT json_object_agg(name, type) FROM foo;
1486                         json_object_agg                         
1487 ----------------------------------------------------------------
1488  { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1489 (1 row)
1490
1491 INSERT INTO foo VALUES (999999, NULL, 'bar');
1492 SELECT json_object_agg(name, type) FROM foo;
1493 ERROR:  field name must not be null
1494 -- json_object
1495 -- one dimension
1496 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1497                       json_object                      
1498 -------------------------------------------------------
1499  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1500 (1 row)
1501
1502 -- same but with two dimensions
1503 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1504                       json_object                      
1505 -------------------------------------------------------
1506  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1507 (1 row)
1508
1509 -- odd number error
1510 SELECT json_object('{a,b,c}');
1511 ERROR:  array must have even number of elements
1512 -- one column error
1513 SELECT json_object('{{a},{b}}');
1514 ERROR:  array must have two columns
1515 -- too many columns error
1516 SELECT json_object('{{a,b,c},{b,c,d}}');
1517 ERROR:  array must have two columns
1518 -- too many dimensions error
1519 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1520 ERROR:  wrong number of array subscripts
1521 --two argument form of json_object
1522 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1523                      json_object                      
1524 ------------------------------------------------------
1525  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1526 (1 row)
1527
1528 -- too many dimensions
1529 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"}}');
1530 ERROR:  wrong number of array subscripts
1531 -- mismatched dimensions
1532 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1533 ERROR:  mismatched array dimensions
1534 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1535 ERROR:  mismatched array dimensions
1536 -- null key error
1537 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1538 ERROR:  null value not allowed for object key
1539 -- empty key is allowed
1540 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1541                      json_object                     
1542 -----------------------------------------------------
1543  {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1544 (1 row)
1545
1546 -- json_to_record and json_to_recordset
1547 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1548     as x(a int, b text, d text);
1549  a |  b  | d 
1550 ---+-----+---
1551  1 | foo | 
1552 (1 row)
1553
1554 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1555     as x(a int, b text, c boolean);
1556  a |  b  | c 
1557 ---+-----+---
1558  1 | foo | 
1559  2 | bar | t
1560 (2 rows)
1561
1562 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1563     as x(a int, b json, c boolean);
1564  a |      b      | c 
1565 ---+-------------+---
1566  1 | {"d":"foo"} | t
1567  2 | {"d":"bar"} | f
1568 (2 rows)
1569
1570 -- json_strip_nulls
1571 select json_strip_nulls(null);
1572  json_strip_nulls 
1573 ------------------
1574  
1575 (1 row)
1576
1577 select json_strip_nulls('1');
1578  json_strip_nulls 
1579 ------------------
1580  1
1581 (1 row)
1582
1583 select json_strip_nulls('"a string"');
1584  json_strip_nulls 
1585 ------------------
1586  "a string"
1587 (1 row)
1588
1589 select json_strip_nulls('null');
1590  json_strip_nulls 
1591 ------------------
1592  null
1593 (1 row)
1594
1595 select json_strip_nulls('[1,2,null,3,4]');
1596  json_strip_nulls 
1597 ------------------
1598  [1,2,null,3,4]
1599 (1 row)
1600
1601 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1602           json_strip_nulls          
1603 ------------------------------------
1604  {"a":1,"c":[2,null,3],"d":{"e":4}}
1605 (1 row)
1606
1607 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1608   json_strip_nulls   
1609 ---------------------
1610  [1,{"a":1,"c":2},3]
1611 (1 row)
1612
1613 -- an empty object is not null and should not be stripped
1614 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
1615  json_strip_nulls 
1616 ------------------
1617  {"a":{},"d":{}}
1618 (1 row)
1619