]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Make json_populate_record and friends operate recursively
[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 -- anyarray column
387 select to_json(histogram_bounds) histogram_bounds
388 from pg_stats
389 where attname = 'tmplname' and tablename = 'pg_pltemplate';
390                                    histogram_bounds                                    
391 ---------------------------------------------------------------------------------------
392  ["plperl","plperlu","plpgsql","plpython2u","plpython3u","plpythonu","pltcl","pltclu"]
393 (1 row)
394
395 -- to_json, timestamps
396 select to_json(timestamp '2014-05-28 12:22:35.614298');
397            to_json            
398 ------------------------------
399  "2014-05-28T12:22:35.614298"
400 (1 row)
401
402 BEGIN;
403 SET LOCAL TIME ZONE 10.5;
404 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
405               to_json               
406 ------------------------------------
407  "2014-05-29T02:52:35.614298+10:30"
408 (1 row)
409
410 SET LOCAL TIME ZONE -8;
411 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
412               to_json               
413 ------------------------------------
414  "2014-05-28T08:22:35.614298-08:00"
415 (1 row)
416
417 COMMIT;
418 select to_json(date '2014-05-28');
419    to_json    
420 --------------
421  "2014-05-28"
422 (1 row)
423
424 select to_json(date 'Infinity');
425   to_json   
426 ------------
427  "infinity"
428 (1 row)
429
430 select to_json(date '-Infinity');
431    to_json   
432 -------------
433  "-infinity"
434 (1 row)
435
436 select to_json(timestamp 'Infinity');
437   to_json   
438 ------------
439  "infinity"
440 (1 row)
441
442 select to_json(timestamp '-Infinity');
443    to_json   
444 -------------
445  "-infinity"
446 (1 row)
447
448 select to_json(timestamptz 'Infinity');
449   to_json   
450 ------------
451  "infinity"
452 (1 row)
453
454 select to_json(timestamptz '-Infinity');
455    to_json   
456 -------------
457  "-infinity"
458 (1 row)
459
460 --json_agg
461 SELECT json_agg(q)
462   FROM ( SELECT $$a$$ || x AS b, y AS c,
463                ARRAY[ROW(x.*,ARRAY[1,2,3]),
464                ROW(y.*,ARRAY[4,5,6])] AS z
465          FROM generate_series(1,2) x,
466               generate_series(4,5) y) q;
467                                json_agg                                
468 -----------------------------------------------------------------------
469  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
470   {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}, +
471   {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}, +
472   {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
473 (1 row)
474
475 SELECT json_agg(q ORDER BY x, y)
476   FROM rows q;
477        json_agg        
478 -----------------------
479  [{"x":1,"y":"txt1"}, +
480   {"x":2,"y":"txt2"}, +
481   {"x":3,"y":"txt3"}]
482 (1 row)
483
484 UPDATE rows SET x = NULL WHERE x = 1;
485 SELECT json_agg(q ORDER BY x NULLS FIRST, y)
486   FROM rows q;
487          json_agg         
488 --------------------------
489  [{"x":null,"y":"txt1"}, +
490   {"x":2,"y":"txt2"},    +
491   {"x":3,"y":"txt3"}]
492 (1 row)
493
494 -- non-numeric output
495 SELECT row_to_json(q)
496 FROM (SELECT 'NaN'::float8 AS "float8field") q;
497       row_to_json      
498 -----------------------
499  {"float8field":"NaN"}
500 (1 row)
501
502 SELECT row_to_json(q)
503 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
504         row_to_json         
505 ----------------------------
506  {"float8field":"Infinity"}
507 (1 row)
508
509 SELECT row_to_json(q)
510 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
511          row_to_json         
512 -----------------------------
513  {"float8field":"-Infinity"}
514 (1 row)
515
516 -- json input
517 SELECT row_to_json(q)
518 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
519                            row_to_json                            
520 ------------------------------------------------------------------
521  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
522 (1 row)
523
524 -- json extraction functions
525 CREATE TEMP TABLE test_json (
526        json_type text,
527        test_json json
528 );
529 INSERT INTO test_json VALUES
530 ('scalar','"a scalar"'),
531 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
532 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
533 SELECT test_json -> 'x'
534 FROM test_json
535 WHERE json_type = 'scalar';
536  ?column? 
537 ----------
538  
539 (1 row)
540
541 SELECT test_json -> 'x'
542 FROM test_json
543 WHERE json_type = 'array';
544  ?column? 
545 ----------
546  
547 (1 row)
548
549 SELECT test_json -> 'x'
550 FROM test_json
551 WHERE json_type = 'object';
552  ?column? 
553 ----------
554  
555 (1 row)
556
557 SELECT test_json->'field2'
558 FROM test_json
559 WHERE json_type = 'object';
560  ?column? 
561 ----------
562  "val2"
563 (1 row)
564
565 SELECT test_json->>'field2'
566 FROM test_json
567 WHERE json_type = 'object';
568  ?column? 
569 ----------
570  val2
571 (1 row)
572
573 SELECT test_json -> 2
574 FROM test_json
575 WHERE json_type = 'scalar';
576  ?column? 
577 ----------
578  
579 (1 row)
580
581 SELECT test_json -> 2
582 FROM test_json
583 WHERE json_type = 'array';
584  ?column? 
585 ----------
586  "two"
587 (1 row)
588
589 SELECT test_json -> -1
590 FROM test_json
591 WHERE json_type = 'array';
592  ?column? 
593 ----------
594  {"f1":9}
595 (1 row)
596
597 SELECT test_json -> 2
598 FROM test_json
599 WHERE json_type = 'object';
600  ?column? 
601 ----------
602  
603 (1 row)
604
605 SELECT test_json->>2
606 FROM test_json
607 WHERE json_type = 'array';
608  ?column? 
609 ----------
610  two
611 (1 row)
612
613 SELECT test_json ->> 6 FROM test_json WHERE json_type = 'array';
614  ?column? 
615 ----------
616  [1,2,3]
617 (1 row)
618
619 SELECT test_json ->> 7 FROM test_json WHERE json_type = 'array';
620  ?column? 
621 ----------
622  {"f1":9}
623 (1 row)
624
625 SELECT test_json ->> 'field4' FROM test_json WHERE json_type = 'object';
626  ?column? 
627 ----------
628  4
629 (1 row)
630
631 SELECT test_json ->> 'field5' FROM test_json WHERE json_type = 'object';
632  ?column? 
633 ----------
634  [1,2,3]
635 (1 row)
636
637 SELECT test_json ->> 'field6' FROM test_json WHERE json_type = 'object';
638  ?column? 
639 ----------
640  {"f1":9}
641 (1 row)
642
643 SELECT json_object_keys(test_json)
644 FROM test_json
645 WHERE json_type = 'scalar';
646 ERROR:  cannot call json_object_keys on a scalar
647 SELECT json_object_keys(test_json)
648 FROM test_json
649 WHERE json_type = 'array';
650 ERROR:  cannot call json_object_keys on an array
651 SELECT json_object_keys(test_json)
652 FROM test_json
653 WHERE json_type = 'object';
654  json_object_keys 
655 ------------------
656  field1
657  field2
658  field3
659  field4
660  field5
661  field6
662 (6 rows)
663
664 -- test extending object_keys resultset - initial resultset size is 256
665 select count(*) from
666     (select json_object_keys(json_object(array_agg(g)))
667      from (select unnest(array['f'||n,n::text])as g
668            from generate_series(1,300) as n) x ) y;
669  count 
670 -------
671    300
672 (1 row)
673
674 -- nulls
675 select (test_json->'field3') is null as expect_false
676 from test_json
677 where json_type = 'object';
678  expect_false 
679 --------------
680  f
681 (1 row)
682
683 select (test_json->>'field3') is null as expect_true
684 from test_json
685 where json_type = 'object';
686  expect_true 
687 -------------
688  t
689 (1 row)
690
691 select (test_json->3) is null as expect_false
692 from test_json
693 where json_type = 'array';
694  expect_false 
695 --------------
696  f
697 (1 row)
698
699 select (test_json->>3) is null as expect_true
700 from test_json
701 where json_type = 'array';
702  expect_true 
703 -------------
704  t
705 (1 row)
706
707 -- corner cases
708 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
709  ?column? 
710 ----------
711  
712 (1 row)
713
714 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
715  ?column? 
716 ----------
717  
718 (1 row)
719
720 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
721  ?column? 
722 ----------
723  
724 (1 row)
725
726 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> -1;
727  ?column? 
728 ----------
729  
730 (1 row)
731
732 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
733  ?column? 
734 ----------
735  
736 (1 row)
737
738 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
739  ?column? 
740 ----------
741  
742 (1 row)
743
744 select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
745   ?column?   
746 -------------
747  {"b": "cc"}
748 (1 row)
749
750 select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
751  ?column? 
752 ----------
753  
754 (1 row)
755
756 select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
757  ?column? 
758 ----------
759  
760 (1 row)
761
762 select '{"a": "c", "b": null}'::json -> 'b';
763  ?column? 
764 ----------
765  null
766 (1 row)
767
768 select '"foo"'::json -> 1;
769  ?column? 
770 ----------
771  
772 (1 row)
773
774 select '"foo"'::json -> 'z';
775  ?column? 
776 ----------
777  
778 (1 row)
779
780 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
781  ?column? 
782 ----------
783  
784 (1 row)
785
786 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
787  ?column? 
788 ----------
789  
790 (1 row)
791
792 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
793  ?column? 
794 ----------
795  
796 (1 row)
797
798 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
799  ?column? 
800 ----------
801  
802 (1 row)
803
804 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
805  ?column? 
806 ----------
807  
808 (1 row)
809
810 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
811   ?column?   
812 -------------
813  {"b": "cc"}
814 (1 row)
815
816 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
817  ?column? 
818 ----------
819  
820 (1 row)
821
822 select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
823  ?column? 
824 ----------
825  
826 (1 row)
827
828 select '{"a": "c", "b": null}'::json ->> 'b';
829  ?column? 
830 ----------
831  
832 (1 row)
833
834 select '"foo"'::json ->> 1;
835  ?column? 
836 ----------
837  
838 (1 row)
839
840 select '"foo"'::json ->> 'z';
841  ?column? 
842 ----------
843  
844 (1 row)
845
846 -- array length
847 SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
848  json_array_length 
849 -------------------
850                  5
851 (1 row)
852
853 SELECT json_array_length('[]');
854  json_array_length 
855 -------------------
856                  0
857 (1 row)
858
859 SELECT json_array_length('{"f1":1,"f2":[5,6]}');
860 ERROR:  cannot get array length of a non-array
861 SELECT json_array_length('4');
862 ERROR:  cannot get array length of a scalar
863 -- each
864 select json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
865      json_each     
866 -------------------
867  (f1,"[1,2,3]")
868  (f2,"{""f3"":1}")
869  (f4,null)
870 (3 rows)
871
872 select * from json_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
873  key |   value   
874 -----+-----------
875  f1  | [1,2,3]
876  f2  | {"f3":1}
877  f4  | null
878  f5  | 99
879  f6  | "stringy"
880 (5 rows)
881
882 select json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
883   json_each_text   
884 -------------------
885  (f1,"[1,2,3]")
886  (f2,"{""f3"":1}")
887  (f4,)
888  (f5,null)
889 (4 rows)
890
891 select * from json_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
892  key |  value   
893 -----+----------
894  f1  | [1,2,3]
895  f2  | {"f3":1}
896  f4  | 
897  f5  | 99
898  f6  | stringy
899 (5 rows)
900
901 -- extract_path, extract_path_as_text
902 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
903  json_extract_path 
904 -------------------
905  "stringy"
906 (1 row)
907
908 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
909  json_extract_path 
910 -------------------
911  {"f3":1}
912 (1 row)
913
914 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
915  json_extract_path 
916 -------------------
917  "f3"
918 (1 row)
919
920 select json_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
921  json_extract_path 
922 -------------------
923  1
924 (1 row)
925
926 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
927  json_extract_path_text 
928 ------------------------
929  stringy
930 (1 row)
931
932 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
933  json_extract_path_text 
934 ------------------------
935  {"f3":1}
936 (1 row)
937
938 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
939  json_extract_path_text 
940 ------------------------
941  f3
942 (1 row)
943
944 select json_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
945  json_extract_path_text 
946 ------------------------
947  1
948 (1 row)
949
950 -- extract_path nulls
951 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_false;
952  expect_false 
953 --------------
954  f
955 (1 row)
956
957 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') is null as expect_true;
958  expect_true 
959 -------------
960  t
961 (1 row)
962
963 select json_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_false;
964  expect_false 
965 --------------
966  f
967 (1 row)
968
969 select json_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') is null as expect_true;
970  expect_true 
971 -------------
972  t
973 (1 row)
974
975 -- extract_path operators
976 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f4','f6'];
977  ?column?  
978 -----------
979  "stringy"
980 (1 row)
981
982 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2'];
983  ?column? 
984 ----------
985  {"f3":1}
986 (1 row)
987
988 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','0'];
989  ?column? 
990 ----------
991  "f3"
992 (1 row)
993
994 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>array['f2','1'];
995  ?column? 
996 ----------
997  1
998 (1 row)
999
1000 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f4','f6'];
1001  ?column? 
1002 ----------
1003  stringy
1004 (1 row)
1005
1006 select '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2'];
1007  ?column? 
1008 ----------
1009  {"f3":1}
1010 (1 row)
1011
1012 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
1013  ?column? 
1014 ----------
1015  f3
1016 (1 row)
1017
1018 select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
1019  ?column? 
1020 ----------
1021  1
1022 (1 row)
1023
1024 -- corner cases for same
1025 select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
1026          ?column?          
1027 ---------------------------
1028  {"a": {"b":{"c": "foo"}}}
1029 (1 row)
1030
1031 select '[1,2,3]'::json #> '{}';
1032  ?column? 
1033 ----------
1034  [1,2,3]
1035 (1 row)
1036
1037 select '"foo"'::json #> '{}';
1038  ?column? 
1039 ----------
1040  "foo"
1041 (1 row)
1042
1043 select '42'::json #> '{}';
1044  ?column? 
1045 ----------
1046  42
1047 (1 row)
1048
1049 select 'null'::json #> '{}';
1050  ?column? 
1051 ----------
1052  null
1053 (1 row)
1054
1055 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
1056       ?column?      
1057 --------------------
1058  {"b":{"c": "foo"}}
1059 (1 row)
1060
1061 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
1062  ?column? 
1063 ----------
1064  
1065 (1 row)
1066
1067 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
1068  ?column? 
1069 ----------
1070  
1071 (1 row)
1072
1073 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
1074    ?column?   
1075 --------------
1076  {"c": "foo"}
1077 (1 row)
1078
1079 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
1080  ?column? 
1081 ----------
1082  "foo"
1083 (1 row)
1084
1085 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
1086  ?column? 
1087 ----------
1088  
1089 (1 row)
1090
1091 select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','z','c'];
1092  ?column? 
1093 ----------
1094  
1095 (1 row)
1096
1097 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
1098  ?column? 
1099 ----------
1100  "cc"
1101 (1 row)
1102
1103 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
1104  ?column? 
1105 ----------
1106  
1107 (1 row)
1108
1109 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
1110  ?column? 
1111 ----------
1112  "cc"
1113 (1 row)
1114
1115 select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
1116  ?column? 
1117 ----------
1118  
1119 (1 row)
1120
1121 select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
1122  ?column? 
1123 ----------
1124  null
1125 (1 row)
1126
1127 select '"foo"'::json #> array['z'];
1128  ?column? 
1129 ----------
1130  
1131 (1 row)
1132
1133 select '42'::json #> array['f2'];
1134  ?column? 
1135 ----------
1136  
1137 (1 row)
1138
1139 select '42'::json #> array['0'];
1140  ?column? 
1141 ----------
1142  
1143 (1 row)
1144
1145 select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
1146          ?column?          
1147 ---------------------------
1148  {"a": {"b":{"c": "foo"}}}
1149 (1 row)
1150
1151 select '[1,2,3]'::json #>> '{}';
1152  ?column? 
1153 ----------
1154  [1,2,3]
1155 (1 row)
1156
1157 select '"foo"'::json #>> '{}';
1158  ?column? 
1159 ----------
1160  foo
1161 (1 row)
1162
1163 select '42'::json #>> '{}';
1164  ?column? 
1165 ----------
1166  42
1167 (1 row)
1168
1169 select 'null'::json #>> '{}';
1170  ?column? 
1171 ----------
1172  
1173 (1 row)
1174
1175 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
1176       ?column?      
1177 --------------------
1178  {"b":{"c": "foo"}}
1179 (1 row)
1180
1181 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1182  ?column? 
1183 ----------
1184  
1185 (1 row)
1186
1187 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1188  ?column? 
1189 ----------
1190  
1191 (1 row)
1192
1193 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
1194    ?column?   
1195 --------------
1196  {"c": "foo"}
1197 (1 row)
1198
1199 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
1200  ?column? 
1201 ----------
1202  foo
1203 (1 row)
1204
1205 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];
1206  ?column? 
1207 ----------
1208  
1209 (1 row)
1210
1211 select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','z','c'];
1212  ?column? 
1213 ----------
1214  
1215 (1 row)
1216
1217 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
1218  ?column? 
1219 ----------
1220  cc
1221 (1 row)
1222
1223 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
1224  ?column? 
1225 ----------
1226  
1227 (1 row)
1228
1229 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
1230  ?column? 
1231 ----------
1232  cc
1233 (1 row)
1234
1235 select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
1236  ?column? 
1237 ----------
1238  
1239 (1 row)
1240
1241 select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
1242  ?column? 
1243 ----------
1244  
1245 (1 row)
1246
1247 select '"foo"'::json #>> array['z'];
1248  ?column? 
1249 ----------
1250  
1251 (1 row)
1252
1253 select '42'::json #>> array['f2'];
1254  ?column? 
1255 ----------
1256  
1257 (1 row)
1258
1259 select '42'::json #>> array['0'];
1260  ?column? 
1261 ----------
1262  
1263 (1 row)
1264
1265 -- array_elements
1266 select json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1267   json_array_elements  
1268 -----------------------
1269  1
1270  true
1271  [1,[2,3]]
1272  null
1273  {"f1":1,"f2":[7,8,9]}
1274  false
1275  "stringy"
1276 (7 rows)
1277
1278 select * from json_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1279          value         
1280 -----------------------
1281  1
1282  true
1283  [1,[2,3]]
1284  null
1285  {"f1":1,"f2":[7,8,9]}
1286  false
1287  "stringy"
1288 (7 rows)
1289
1290 select json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1291  json_array_elements_text 
1292 --------------------------
1293  1
1294  true
1295  [1,[2,3]]
1296  
1297  {"f1":1,"f2":[7,8,9]}
1298  false
1299  stringy
1300 (7 rows)
1301
1302 select * from json_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1303          value         
1304 -----------------------
1305  1
1306  true
1307  [1,[2,3]]
1308  
1309  {"f1":1,"f2":[7,8,9]}
1310  false
1311  stringy
1312 (7 rows)
1313
1314 -- populate_record
1315 create type jpop as (a text, b int, c timestamp);
1316 CREATE DOMAIN js_int_not_null  AS int     NOT NULL;
1317 CREATE DOMAIN js_int_array_1d  AS int[]   CHECK(array_length(VALUE, 1) = 3);
1318 CREATE DOMAIN js_int_array_2d  AS int[][] CHECK(array_length(VALUE, 2) = 3);
1319 CREATE TYPE jsrec AS (
1320         i       int,
1321         ia      _int4,
1322         ia1     int[],
1323         ia2     int[][],
1324         ia3     int[][][],
1325         ia1d    js_int_array_1d,
1326         ia2d    js_int_array_2d,
1327         t       text,
1328         ta      text[],
1329         c       char(10),
1330         ca      char(10)[],
1331         ts      timestamp,
1332         js      json,
1333         jsb     jsonb,
1334         jsa     json[],
1335         rec     jpop,
1336         reca    jpop[]
1337 );
1338 CREATE TYPE jsrec_i_not_null AS (
1339         i       js_int_not_null
1340 );
1341 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1342    a    | b | c 
1343 --------+---+---
1344  blurfl |   | 
1345 (1 row)
1346
1347 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1348    a    | b |            c             
1349 --------+---+--------------------------
1350  blurfl | 3 | Mon Dec 31 15:30:56 2012
1351 (1 row)
1352
1353 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1354    a    | b | c 
1355 --------+---+---
1356  blurfl |   | 
1357 (1 row)
1358
1359 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1360    a    | b |            c             
1361 --------+---+--------------------------
1362  blurfl | 3 | Mon Dec 31 15:30:56 2012
1363 (1 row)
1364
1365 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1366         a        | b | c 
1367 -----------------+---+---
1368  [100,200,false] |   | 
1369 (1 row)
1370
1371 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1372         a        | b |            c             
1373 -----------------+---+--------------------------
1374  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1375 (1 row)
1376
1377 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1378 ERROR:  invalid input syntax for type timestamp: "[100,200,false]"
1379 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{}') q;
1380  a | b |            c             
1381 ---+---+--------------------------
1382  x | 3 | Mon Dec 31 15:30:56 2012
1383 (1 row)
1384
1385 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"x": 43.2}') q;
1386 ERROR:  domain js_int_not_null does not allow null values
1387 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": null}') q;
1388 ERROR:  domain js_int_not_null does not allow null values
1389 SELECT i FROM json_populate_record(NULL::jsrec_i_not_null, '{"i": 12345}') q;
1390    i   
1391 -------
1392  12345
1393 (1 row)
1394
1395 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": null}') q;
1396  ia 
1397 ----
1398  
1399 (1 row)
1400
1401 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": 123}') q;
1402 ERROR:  expected json array
1403 HINT:  see the value of key "ia"
1404 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [1, "2", null, 4]}') q;
1405       ia      
1406 --------------
1407  {1,2,NULL,4}
1408 (1 row)
1409
1410 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1, 2], [3, 4]]}') q;
1411       ia       
1412 ---------------
1413  {{1,2},{3,4}}
1414 (1 row)
1415
1416 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], 2]}') q;
1417 ERROR:  expected json array
1418 HINT:  see the array element [1] of key "ia"
1419 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": [[1], [2, 3]]}') q;
1420 ERROR:  malformed json array
1421 DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.
1422 SELECT ia FROM json_populate_record(NULL::jsrec, '{"ia": "{1,2,3}"}') q;
1423    ia    
1424 ---------
1425  {1,2,3}
1426 (1 row)
1427
1428 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": null}') q;
1429  ia1 
1430 -----
1431  
1432 (1 row)
1433
1434 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": 123}') q;
1435 ERROR:  expected json array
1436 HINT:  see the value of key "ia1"
1437 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [1, "2", null, 4]}') q;
1438      ia1      
1439 --------------
1440  {1,2,NULL,4}
1441 (1 row)
1442
1443 SELECT ia1 FROM json_populate_record(NULL::jsrec, '{"ia1": [[1, 2, 3]]}') q;
1444     ia1    
1445 -----------
1446  {{1,2,3}}
1447 (1 row)
1448
1449 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": null}') q;
1450  ia1d 
1451 ------
1452  
1453 (1 row)
1454
1455 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": 123}') q;
1456 ERROR:  expected json array
1457 HINT:  see the value of key "ia1d"
1458 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null, 4]}') q;
1459 ERROR:  value for domain js_int_array_1d violates check constraint "js_int_array_1d_check"
1460 SELECT ia1d FROM json_populate_record(NULL::jsrec, '{"ia1d": [1, "2", null]}') q;
1461     ia1d    
1462 ------------
1463  {1,2,NULL}
1464 (1 row)
1465
1466 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [1, "2", null, 4]}') q;
1467      ia2      
1468 --------------
1469  {1,2,NULL,4}
1470 (1 row)
1471
1472 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [null, 4]]}') q;
1473        ia2        
1474 ------------------
1475  {{1,2},{NULL,4}}
1476 (1 row)
1477
1478 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[], []]}') q;
1479  ia2 
1480 -----
1481  {}
1482 (1 row)
1483
1484 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], [3]]}') q;
1485 ERROR:  malformed json array
1486 DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.
1487 SELECT ia2 FROM json_populate_record(NULL::jsrec, '{"ia2": [[1, 2], 3, 4]}') q;
1488 ERROR:  expected json array
1489 HINT:  see the array element [1] of key "ia2"
1490 SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
1491 ERROR:  value for domain js_int_array_2d violates check constraint "js_int_array_2d_check"
1492 SELECT ia2d FROM json_populate_record(NULL::jsrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
1493          ia2d         
1494 ----------------------
1495  {{1,2,3},{NULL,5,6}}
1496 (1 row)
1497
1498 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [1, "2", null, 4]}') q;
1499      ia3      
1500 --------------
1501  {1,2,NULL,4}
1502 (1 row)
1503
1504 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [[1, 2], [null, 4]]}') q;
1505        ia3        
1506 ------------------
1507  {{1,2},{NULL,4}}
1508 (1 row)
1509
1510 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
1511  ia3 
1512 -----
1513  {}
1514 (1 row)
1515
1516 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
1517         ia3        
1518 -------------------
1519  {{{1,2}},{{3,4}}}
1520 (1 row)
1521
1522 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
1523               ia3              
1524 -------------------------------
1525  {{{1,2},{3,4}},{{5,6},{7,8}}}
1526 (1 row)
1527
1528 SELECT ia3 FROM json_populate_record(NULL::jsrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
1529 ERROR:  malformed json array
1530 DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.
1531 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": null}') q;
1532  ta 
1533 ----
1534  
1535 (1 row)
1536
1537 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": 123}') q;
1538 ERROR:  expected json array
1539 HINT:  see the value of key "ta"
1540 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [1, "2", null, 4]}') q;
1541       ta      
1542 --------------
1543  {1,2,NULL,4}
1544 (1 row)
1545
1546 SELECT ta FROM json_populate_record(NULL::jsrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
1547 ERROR:  expected json array
1548 HINT:  see the array element [1] of key "ta"
1549 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": null}') q;
1550  c 
1551 ---
1552  
1553 (1 row)
1554
1555 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaa"}') q;
1556      c      
1557 ------------
1558  aaa       
1559 (1 row)
1560
1561 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaa"}') q;
1562      c      
1563 ------------
1564  aaaaaaaaaa
1565 (1 row)
1566
1567 SELECT c FROM json_populate_record(NULL::jsrec, '{"c": "aaaaaaaaaaaaa"}') q;
1568 ERROR:  value too long for type character(10)
1569 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": null}') q;
1570  ca 
1571 ----
1572  
1573 (1 row)
1574
1575 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": 123}') q;
1576 ERROR:  expected json array
1577 HINT:  see the value of key "ca"
1578 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [1, "2", null, 4]}') q;
1579                       ca                       
1580 -----------------------------------------------
1581  {"1         ","2         ",NULL,"4         "}
1582 (1 row)
1583
1584 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
1585 ERROR:  value too long for type character(10)
1586 SELECT ca FROM json_populate_record(NULL::jsrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
1587 ERROR:  expected json array
1588 HINT:  see the array element [1] of key "ca"
1589 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": null}') q;
1590  js 
1591 ----
1592  
1593 (1 row)
1594
1595 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": true}') q;
1596   js  
1597 ------
1598  true
1599 (1 row)
1600
1601 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": 123.45}') q;
1602    js   
1603 --------
1604  123.45
1605 (1 row)
1606
1607 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "123.45"}') q;
1608     js    
1609 ----------
1610  "123.45"
1611 (1 row)
1612
1613 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": "abc"}') q;
1614   js   
1615 -------
1616  "abc"
1617 (1 row)
1618
1619 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
1620                   js                  
1621 --------------------------------------
1622  [123, "123", null, {"key": "value"}]
1623 (1 row)
1624
1625 SELECT js FROM json_populate_record(NULL::jsrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
1626                   js                  
1627 --------------------------------------
1628  {"a": "bbb", "b": null, "c": 123.45}
1629 (1 row)
1630
1631 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": null}') q;
1632  jsb 
1633 -----
1634  
1635 (1 row)
1636
1637 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": true}') q;
1638  jsb  
1639 ------
1640  true
1641 (1 row)
1642
1643 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": 123.45}') q;
1644   jsb   
1645 --------
1646  123.45
1647 (1 row)
1648
1649 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "123.45"}') q;
1650    jsb    
1651 ----------
1652  "123.45"
1653 (1 row)
1654
1655 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": "abc"}') q;
1656   jsb  
1657 -------
1658  "abc"
1659 (1 row)
1660
1661 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
1662                  jsb                  
1663 --------------------------------------
1664  [123, "123", null, {"key": "value"}]
1665 (1 row)
1666
1667 SELECT jsb FROM json_populate_record(NULL::jsrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
1668                  jsb                  
1669 --------------------------------------
1670  {"a": "bbb", "b": null, "c": 123.45}
1671 (1 row)
1672
1673 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": null}') q;
1674  jsa 
1675 -----
1676  
1677 (1 row)
1678
1679 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": 123}') q;
1680 ERROR:  expected json array
1681 HINT:  see the value of key "jsa"
1682 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": [1, "2", null, 4]}') q;
1683         jsa         
1684 --------------------
1685  {1,"\"2\"",NULL,4}
1686 (1 row)
1687
1688 SELECT jsa FROM json_populate_record(NULL::jsrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
1689                            jsa                            
1690 ----------------------------------------------------------
1691  {"\"aaa\"",NULL,"[1, 2, \"3\", {}]","{ \"k\" : \"v\" }"}
1692 (1 row)
1693
1694 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": 123}') q;
1695 ERROR:  cannot call populate_composite on a scalar
1696 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": [1, 2]}') q;
1697 ERROR:  cannot call populate_composite on an array
1698 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
1699                 rec                
1700 -----------------------------------
1701  (abc,,"Thu Jan 02 00:00:00 2003")
1702 (1 row)
1703
1704 SELECT rec FROM json_populate_record(NULL::jsrec, '{"rec": "(abc,42,01.02.2003)"}') q;
1705                  rec                 
1706 -------------------------------------
1707  (abc,42,"Thu Jan 02 00:00:00 2003")
1708 (1 row)
1709
1710 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": 123}') q;
1711 ERROR:  expected json array
1712 HINT:  see the value of key "reca"
1713 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [1, 2]}') q;
1714 ERROR:  cannot call populate_composite on a scalar
1715 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
1716                           reca                          
1717 --------------------------------------------------------
1718  {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1719 (1 row)
1720
1721 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
1722                    reca                    
1723 -------------------------------------------
1724  {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
1725 (1 row)
1726
1727 SELECT reca FROM json_populate_record(NULL::jsrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
1728                    reca                    
1729 -------------------------------------------
1730  {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
1731 (1 row)
1732
1733 SELECT rec FROM json_populate_record(
1734         row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
1735                 row('x',3,'2012-12-31 15:30:56')::jpop,NULL)::jsrec,
1736         '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
1737 ) q;
1738                 rec                 
1739 ------------------------------------
1740  (abc,3,"Thu Jan 02 00:00:00 2003")
1741 (1 row)
1742
1743 -- populate_recordset
1744 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1745    a    | b |            c             
1746 --------+---+--------------------------
1747  blurfl |   | 
1748         | 3 | Fri Jan 20 10:42:53 2012
1749 (2 rows)
1750
1751 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;
1752    a    | b  |            c             
1753 --------+----+--------------------------
1754  blurfl | 99 | 
1755  def    |  3 | Fri Jan 20 10:42:53 2012
1756 (2 rows)
1757
1758 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1759    a    | b |            c             
1760 --------+---+--------------------------
1761  blurfl |   | 
1762         | 3 | Fri Jan 20 10:42:53 2012
1763 (2 rows)
1764
1765 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;
1766    a    | b  |            c             
1767 --------+----+--------------------------
1768  blurfl | 99 | 
1769  def    |  3 | Fri Jan 20 10:42:53 2012
1770 (2 rows)
1771
1772 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;
1773        a       | b  |            c             
1774 ---------------+----+--------------------------
1775  [100,200,300] | 99 | 
1776  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1777 (2 rows)
1778
1779 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;
1780 ERROR:  invalid input syntax for type timestamp: "[100,200,300]"
1781 create type jpop2 as (a int, b json, c int, d int);
1782 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1783  a |    b    | c | d 
1784 ---+---------+---+---
1785  2 | {"z":4} | 3 | 6
1786 (1 row)
1787
1788 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1789    a    | b |            c             
1790 --------+---+--------------------------
1791  blurfl |   | 
1792         | 3 | Fri Jan 20 10:42:53 2012
1793 (2 rows)
1794
1795 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;
1796    a    | b  |            c             
1797 --------+----+--------------------------
1798  blurfl | 99 | 
1799  def    |  3 | Fri Jan 20 10:42:53 2012
1800 (2 rows)
1801
1802 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;
1803        a       | b  |            c             
1804 ---------------+----+--------------------------
1805  [100,200,300] | 99 | 
1806  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1807 (2 rows)
1808
1809 -- test type info caching in json_populate_record()
1810 CREATE TEMP TABLE jspoptest (js json);
1811 INSERT INTO jspoptest
1812 SELECT '{
1813         "jsa": [1, "2", null, 4],
1814         "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
1815         "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
1816 }'::json
1817 FROM generate_series(1, 3);
1818 SELECT (json_populate_record(NULL::jsrec, js)).* FROM jspoptest;
1819  i | ia | ia1 | ia2 | ia3 | ia1d | ia2d | t | ta | c | ca | ts | js | jsb |        jsa         |                rec                |                          reca                          
1820 ---+----+-----+-----+-----+------+------+---+----+---+----+----+----+-----+--------------------+-----------------------------------+--------------------------------------------------------
1821    |    |     |     |     |      |      |   |    |   |    |    |    |     | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1822    |    |     |     |     |      |      |   |    |   |    |    |    |     | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1823    |    |     |     |     |      |      |   |    |   |    |    |    |     | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
1824 (3 rows)
1825
1826 DROP TYPE jsrec;
1827 DROP TYPE jsrec_i_not_null;
1828 DROP DOMAIN js_int_not_null;
1829 DROP DOMAIN js_int_array_1d;
1830 DROP DOMAIN js_int_array_2d;
1831 --json_typeof() function
1832 select value, json_typeof(value)
1833   from (values (json '123.4'),
1834                (json '-1'),
1835                (json '"foo"'),
1836                (json 'true'),
1837                (json 'false'),
1838                (json 'null'),
1839                (json '[1, 2, 3]'),
1840                (json '[]'),
1841                (json '{"x":"foo", "y":123}'),
1842                (json '{}'),
1843                (NULL::json))
1844       as data(value);
1845         value         | json_typeof 
1846 ----------------------+-------------
1847  123.4                | number
1848  -1                   | number
1849  "foo"                | string
1850  true                 | boolean
1851  false                | boolean
1852  null                 | null
1853  [1, 2, 3]            | array
1854  []                   | array
1855  {"x":"foo", "y":123} | object
1856  {}                   | object
1857                       | 
1858 (11 rows)
1859
1860 -- json_build_array, json_build_object, json_object_agg
1861 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1862                            json_build_array                            
1863 -----------------------------------------------------------------------
1864  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1865 (1 row)
1866
1867 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1868                              json_build_object                              
1869 ----------------------------------------------------------------------------
1870  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1871 (1 row)
1872
1873 SELECT json_build_object(
1874        'a', json_build_object('b',false,'c',99),
1875        'd', json_build_object('e',array[9,8,7]::int[],
1876            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1877                                         json_build_object                                        
1878 -------------------------------------------------------------------------------------------------
1879  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1880 (1 row)
1881
1882 -- empty objects/arrays
1883 SELECT json_build_array();
1884  json_build_array 
1885 ------------------
1886  []
1887 (1 row)
1888
1889 SELECT json_build_object();
1890  json_build_object 
1891 -------------------
1892  {}
1893 (1 row)
1894
1895 -- make sure keys are quoted
1896 SELECT json_build_object(1,2);
1897  json_build_object 
1898 -------------------
1899  {"1" : 2}
1900 (1 row)
1901
1902 -- keys must be scalar and not null
1903 SELECT json_build_object(null,2);
1904 ERROR:  argument 1 cannot be null
1905 HINT:  Object keys should be text.
1906 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1907 ERROR:  key value must be scalar, not array, composite, or json
1908 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1909 ERROR:  key value must be scalar, not array, composite, or json
1910 SELECT json_build_object('{1,2,3}'::int[], 3);
1911 ERROR:  key value must be scalar, not array, composite, or json
1912 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1913 INSERT INTO foo VALUES (847001,'t15','GE1043');
1914 INSERT INTO foo VALUES (847002,'t16','GE1043');
1915 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1916 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1917 FROM foo;
1918                                                                             json_build_object                                                                            
1919 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1920  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1921 (1 row)
1922
1923 SELECT json_object_agg(name, type) FROM foo;
1924                         json_object_agg                         
1925 ----------------------------------------------------------------
1926  { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1927 (1 row)
1928
1929 INSERT INTO foo VALUES (999999, NULL, 'bar');
1930 SELECT json_object_agg(name, type) FROM foo;
1931 ERROR:  field name must not be null
1932 -- json_object
1933 -- empty object, one dimension
1934 SELECT json_object('{}');
1935  json_object 
1936 -------------
1937  {}
1938 (1 row)
1939
1940 -- empty object, two dimensions
1941 SELECT json_object('{}', '{}');
1942  json_object 
1943 -------------
1944  {}
1945 (1 row)
1946
1947 -- one dimension
1948 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1949                       json_object                      
1950 -------------------------------------------------------
1951  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1952 (1 row)
1953
1954 -- same but with two dimensions
1955 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1956                       json_object                      
1957 -------------------------------------------------------
1958  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1959 (1 row)
1960
1961 -- odd number error
1962 SELECT json_object('{a,b,c}');
1963 ERROR:  array must have even number of elements
1964 -- one column error
1965 SELECT json_object('{{a},{b}}');
1966 ERROR:  array must have two columns
1967 -- too many columns error
1968 SELECT json_object('{{a,b,c},{b,c,d}}');
1969 ERROR:  array must have two columns
1970 -- too many dimensions error
1971 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1972 ERROR:  wrong number of array subscripts
1973 --two argument form of json_object
1974 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1975                      json_object                      
1976 ------------------------------------------------------
1977  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1978 (1 row)
1979
1980 -- too many dimensions
1981 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"}}');
1982 ERROR:  wrong number of array subscripts
1983 -- mismatched dimensions
1984 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1985 ERROR:  mismatched array dimensions
1986 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1987 ERROR:  mismatched array dimensions
1988 -- null key error
1989 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1990 ERROR:  null value not allowed for object key
1991 -- empty key is allowed
1992 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1993                      json_object                     
1994 -----------------------------------------------------
1995  {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1996 (1 row)
1997
1998 -- json_to_record and json_to_recordset
1999 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
2000     as x(a int, b text, d text);
2001  a |  b  | d 
2002 ---+-----+---
2003  1 | foo | 
2004 (1 row)
2005
2006 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
2007     as x(a int, b text, c boolean);
2008  a |  b  | c 
2009 ---+-----+---
2010  1 | foo | 
2011  2 | bar | t
2012 (2 rows)
2013
2014 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
2015     as x(a int, b json, c boolean);
2016  a |      b      | c 
2017 ---+-------------+---
2018  1 | {"d":"foo"} | t
2019  2 | {"d":"bar"} | f
2020 (2 rows)
2021
2022 select *, c is null as c_is_null
2023 from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::json)
2024     as t(a int, b json, c text, x int, ca char(5)[], ia int[][], r jpop);
2025  a |        b        | c | x |        ca         |      ia       |     r      | c_is_null 
2026 ---+-----------------+---+---+-------------------+---------------+------------+-----------
2027  1 | {"c":16, "d":2} |   | 8 | {"1 2  ","3    "} | {{1,2},{3,4}} | (aaa,123,) | t
2028 (1 row)
2029
2030 select *, c is null as c_is_null
2031 from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
2032     as t(a int, b json, c text, x int);
2033  a |        b        | c | x | c_is_null 
2034 ---+-----------------+---+---+-----------
2035  1 | {"c":16, "d":2} |   | 8 | t
2036 (1 row)
2037
2038 select * from json_to_record('{"ia": null}') as x(ia _int4);
2039  ia 
2040 ----
2041  
2042 (1 row)
2043
2044 select * from json_to_record('{"ia": 123}') as x(ia _int4);
2045 ERROR:  expected json array
2046 HINT:  see the value of key "ia"
2047 select * from json_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
2048       ia      
2049 --------------
2050  {1,2,NULL,4}
2051 (1 row)
2052
2053 select * from json_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
2054       ia       
2055 ---------------
2056  {{1,2},{3,4}}
2057 (1 row)
2058
2059 select * from json_to_record('{"ia": [[1], 2]}') as x(ia _int4);
2060 ERROR:  expected json array
2061 HINT:  see the array element [1] of key "ia"
2062 select * from json_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
2063 ERROR:  malformed json array
2064 DETAIL:  Multidimensional arrays must have sub-arrays with matching dimensions.
2065 select * from json_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
2066    ia2   
2067 ---------
2068  {1,2,3}
2069 (1 row)
2070
2071 select * from json_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
2072       ia2      
2073 ---------------
2074  {{1,2},{3,4}}
2075 (1 row)
2076
2077 select * from json_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
2078        ia2       
2079 -----------------
2080  {{{1},{2},{3}}}
2081 (1 row)
2082
2083 -- json_strip_nulls
2084 select json_strip_nulls(null);
2085  json_strip_nulls 
2086 ------------------
2087  
2088 (1 row)
2089
2090 select json_strip_nulls('1');
2091  json_strip_nulls 
2092 ------------------
2093  1
2094 (1 row)
2095
2096 select json_strip_nulls('"a string"');
2097  json_strip_nulls 
2098 ------------------
2099  "a string"
2100 (1 row)
2101
2102 select json_strip_nulls('null');
2103  json_strip_nulls 
2104 ------------------
2105  null
2106 (1 row)
2107
2108 select json_strip_nulls('[1,2,null,3,4]');
2109  json_strip_nulls 
2110 ------------------
2111  [1,2,null,3,4]
2112 (1 row)
2113
2114 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
2115           json_strip_nulls          
2116 ------------------------------------
2117  {"a":1,"c":[2,null,3],"d":{"e":4}}
2118 (1 row)
2119
2120 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
2121   json_strip_nulls   
2122 ---------------------
2123  [1,{"a":1,"c":2},3]
2124 (1 row)
2125
2126 -- an empty object is not null and should not be stripped
2127 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
2128  json_strip_nulls 
2129 ------------------
2130  {"a":{},"d":{}}
2131 (1 row)
2132
2133 -- json to tsvector
2134 select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
2135                                 to_tsvector                                
2136 ---------------------------------------------------------------------------
2137  'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
2138 (1 row)
2139
2140 -- json to tsvector with config
2141 select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
2142                                 to_tsvector                                
2143 ---------------------------------------------------------------------------
2144  'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
2145 (1 row)
2146
2147 -- json to tsvector with stop words
2148 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
2149                                 to_tsvector                                 
2150 ----------------------------------------------------------------------------
2151  'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
2152 (1 row)
2153
2154 -- ts_vector corner cases
2155 select to_tsvector('""'::json);
2156  to_tsvector 
2157 -------------
2158  
2159 (1 row)
2160
2161 select to_tsvector('{}'::json);
2162  to_tsvector 
2163 -------------
2164  
2165 (1 row)
2166
2167 select to_tsvector('[]'::json);
2168  to_tsvector 
2169 -------------
2170  
2171 (1 row)
2172
2173 select to_tsvector('null'::json);
2174  to_tsvector 
2175 -------------
2176  
2177 (1 row)
2178
2179 -- ts_headline for json
2180 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
2181                                                ts_headline                                               
2182 ---------------------------------------------------------------------------------------------------------
2183  {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff","c1":"ccc1 ddd1"},"d":["ggg <b>hhh</b>","iii jjj"]}
2184 (1 row)
2185
2186 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
2187                                       ts_headline                                       
2188 ----------------------------------------------------------------------------------------
2189  {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff"},"d":["ggg <b>hhh</b>","iii jjj"]}
2190 (1 row)
2191
2192 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
2193                                        ts_headline                                        
2194 ------------------------------------------------------------------------------------------
2195  {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
2196 (1 row)
2197
2198 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
2199                                        ts_headline                                        
2200 ------------------------------------------------------------------------------------------
2201  {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
2202 (1 row)
2203
2204 -- corner cases for ts_headline with json
2205 select ts_headline('null'::json, tsquery('aaa & bbb'));
2206  ts_headline 
2207 -------------
2208  null
2209 (1 row)
2210
2211 select ts_headline('{}'::json, tsquery('aaa & bbb'));
2212  ts_headline 
2213 -------------
2214  {}
2215 (1 row)
2216
2217 select ts_headline('[]'::json, tsquery('aaa & bbb'));
2218  ts_headline 
2219 -------------
2220  []
2221 (1 row)
2222