]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Fix unstable regression test result.
[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 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1317    a    | b | c 
1318 --------+---+---
1319  blurfl |   | 
1320 (1 row)
1321
1322 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1323    a    | b |            c             
1324 --------+---+--------------------------
1325  blurfl | 3 | Mon Dec 31 15:30:56 2012
1326 (1 row)
1327
1328 select * from json_populate_record(null::jpop,'{"a":"blurfl","x":43.2}') q;
1329    a    | b | c 
1330 --------+---+---
1331  blurfl |   | 
1332 (1 row)
1333
1334 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":"blurfl","x":43.2}') q;
1335    a    | b |            c             
1336 --------+---+--------------------------
1337  blurfl | 3 | Mon Dec 31 15:30:56 2012
1338 (1 row)
1339
1340 select * from json_populate_record(null::jpop,'{"a":[100,200,false],"x":43.2}') q;
1341         a        | b | c 
1342 -----------------+---+---
1343  [100,200,false] |   | 
1344 (1 row)
1345
1346 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"a":[100,200,false],"x":43.2}') q;
1347         a        | b |            c             
1348 -----------------+---+--------------------------
1349  [100,200,false] | 3 | Mon Dec 31 15:30:56 2012
1350 (1 row)
1351
1352 select * from json_populate_record(row('x',3,'2012-12-31 15:30:56')::jpop,'{"c":[100,200,false],"x":43.2}') q;
1353 ERROR:  invalid input syntax for type timestamp: "[100,200,false]"
1354 -- populate_recordset
1355 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1356    a    | b |            c             
1357 --------+---+--------------------------
1358  blurfl |   | 
1359         | 3 | Fri Jan 20 10:42:53 2012
1360 (2 rows)
1361
1362 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;
1363    a    | b  |            c             
1364 --------+----+--------------------------
1365  blurfl | 99 | 
1366  def    |  3 | Fri Jan 20 10:42:53 2012
1367 (2 rows)
1368
1369 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1370    a    | b |            c             
1371 --------+---+--------------------------
1372  blurfl |   | 
1373         | 3 | Fri Jan 20 10:42:53 2012
1374 (2 rows)
1375
1376 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;
1377    a    | b  |            c             
1378 --------+----+--------------------------
1379  blurfl | 99 | 
1380  def    |  3 | Fri Jan 20 10:42:53 2012
1381 (2 rows)
1382
1383 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;
1384        a       | b  |            c             
1385 ---------------+----+--------------------------
1386  [100,200,300] | 99 | 
1387  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1388 (2 rows)
1389
1390 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;
1391 ERROR:  invalid input syntax for type timestamp: "[100,200,300]"
1392 create type jpop2 as (a int, b json, c int, d int);
1393 select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]') q;
1394  a |    b    | c | d 
1395 ---+---------+---+---
1396  2 | {"z":4} | 3 | 6
1397 (1 row)
1398
1399 select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1400    a    | b |            c             
1401 --------+---+--------------------------
1402  blurfl |   | 
1403         | 3 | Fri Jan 20 10:42:53 2012
1404 (2 rows)
1405
1406 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;
1407    a    | b  |            c             
1408 --------+----+--------------------------
1409  blurfl | 99 | 
1410  def    |  3 | Fri Jan 20 10:42:53 2012
1411 (2 rows)
1412
1413 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;
1414        a       | b  |            c             
1415 ---------------+----+--------------------------
1416  [100,200,300] | 99 | 
1417  {"z":true}    |  3 | Fri Jan 20 10:42:53 2012
1418 (2 rows)
1419
1420 --json_typeof() function
1421 select value, json_typeof(value)
1422   from (values (json '123.4'),
1423                (json '-1'),
1424                (json '"foo"'),
1425                (json 'true'),
1426                (json 'false'),
1427                (json 'null'),
1428                (json '[1, 2, 3]'),
1429                (json '[]'),
1430                (json '{"x":"foo", "y":123}'),
1431                (json '{}'),
1432                (NULL::json))
1433       as data(value);
1434         value         | json_typeof 
1435 ----------------------+-------------
1436  123.4                | number
1437  -1                   | number
1438  "foo"                | string
1439  true                 | boolean
1440  false                | boolean
1441  null                 | null
1442  [1, 2, 3]            | array
1443  []                   | array
1444  {"x":"foo", "y":123} | object
1445  {}                   | object
1446                       | 
1447 (11 rows)
1448
1449 -- json_build_array, json_build_object, json_object_agg
1450 SELECT json_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1451                            json_build_array                            
1452 -----------------------------------------------------------------------
1453  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1,2,3]}]
1454 (1 row)
1455
1456 SELECT json_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1457                              json_build_object                              
1458 ----------------------------------------------------------------------------
1459  {"a" : 1, "b" : 1.2, "c" : true, "d" : null, "e" : {"x": 3, "y": [1,2,3]}}
1460 (1 row)
1461
1462 SELECT json_build_object(
1463        'a', json_build_object('b',false,'c',99),
1464        'd', json_build_object('e',array[9,8,7]::int[],
1465            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1466                                         json_build_object                                        
1467 -------------------------------------------------------------------------------------------------
1468  {"a" : {"b" : false, "c" : 99}, "d" : {"e" : [9,8,7], "f" : {"relkind":"r","name":"pg_class"}}}
1469 (1 row)
1470
1471 -- empty objects/arrays
1472 SELECT json_build_array();
1473  json_build_array 
1474 ------------------
1475  []
1476 (1 row)
1477
1478 SELECT json_build_object();
1479  json_build_object 
1480 -------------------
1481  {}
1482 (1 row)
1483
1484 -- make sure keys are quoted
1485 SELECT json_build_object(1,2);
1486  json_build_object 
1487 -------------------
1488  {"1" : 2}
1489 (1 row)
1490
1491 -- keys must be scalar and not null
1492 SELECT json_build_object(null,2);
1493 ERROR:  argument 1 cannot be null
1494 HINT:  Object keys should be text.
1495 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1496 ERROR:  key value must be scalar, not array, composite, or json
1497 SELECT json_build_object(json '{"a":1,"b":2}', 3);
1498 ERROR:  key value must be scalar, not array, composite, or json
1499 SELECT json_build_object('{1,2,3}'::int[], 3);
1500 ERROR:  key value must be scalar, not array, composite, or json
1501 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1502 INSERT INTO foo VALUES (847001,'t15','GE1043');
1503 INSERT INTO foo VALUES (847002,'t16','GE1043');
1504 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1505 SELECT json_build_object('turbines',json_object_agg(serial_num,json_build_object('name',name,'type',type)))
1506 FROM foo;
1507                                                                             json_build_object                                                                            
1508 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1509  {"turbines" : { "847001" : {"name" : "t15", "type" : "GE1043"}, "847002" : {"name" : "t16", "type" : "GE1043"}, "847003" : {"name" : "sub-alpha", "type" : "GESS90"} }}
1510 (1 row)
1511
1512 SELECT json_object_agg(name, type) FROM foo;
1513                         json_object_agg                         
1514 ----------------------------------------------------------------
1515  { "t15" : "GE1043", "t16" : "GE1043", "sub-alpha" : "GESS90" }
1516 (1 row)
1517
1518 INSERT INTO foo VALUES (999999, NULL, 'bar');
1519 SELECT json_object_agg(name, type) FROM foo;
1520 ERROR:  field name must not be null
1521 -- json_object
1522 -- empty object, one dimension
1523 SELECT json_object('{}');
1524  json_object 
1525 -------------
1526  {}
1527 (1 row)
1528
1529 -- empty object, two dimensions
1530 SELECT json_object('{}', '{}');
1531  json_object 
1532 -------------
1533  {}
1534 (1 row)
1535
1536 -- one dimension
1537 SELECT json_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1538                       json_object                      
1539 -------------------------------------------------------
1540  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1541 (1 row)
1542
1543 -- same but with two dimensions
1544 SELECT json_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1545                       json_object                      
1546 -------------------------------------------------------
1547  {"a" : "1", "b" : "2", "3" : null, "d e f" : "a b c"}
1548 (1 row)
1549
1550 -- odd number error
1551 SELECT json_object('{a,b,c}');
1552 ERROR:  array must have even number of elements
1553 -- one column error
1554 SELECT json_object('{{a},{b}}');
1555 ERROR:  array must have two columns
1556 -- too many columns error
1557 SELECT json_object('{{a,b,c},{b,c,d}}');
1558 ERROR:  array must have two columns
1559 -- too many dimensions error
1560 SELECT json_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1561 ERROR:  wrong number of array subscripts
1562 --two argument form of json_object
1563 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1564                      json_object                      
1565 ------------------------------------------------------
1566  {"a" : "1", "b" : "2", "c" : "3", "d e f" : "a b c"}
1567 (1 row)
1568
1569 -- too many dimensions
1570 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"}}');
1571 ERROR:  wrong number of array subscripts
1572 -- mismatched dimensions
1573 select json_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1574 ERROR:  mismatched array dimensions
1575 select json_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1576 ERROR:  mismatched array dimensions
1577 -- null key error
1578 select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1579 ERROR:  null value not allowed for object key
1580 -- empty key is allowed
1581 select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1582                      json_object                     
1583 -----------------------------------------------------
1584  {"a" : "1", "b" : "2", "" : "3", "d e f" : "a b c"}
1585 (1 row)
1586
1587 -- json_to_record and json_to_recordset
1588 select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
1589     as x(a int, b text, d text);
1590  a |  b  | d 
1591 ---+-----+---
1592  1 | foo | 
1593 (1 row)
1594
1595 select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
1596     as x(a int, b text, c boolean);
1597  a |  b  | c 
1598 ---+-----+---
1599  1 | foo | 
1600  2 | bar | t
1601 (2 rows)
1602
1603 select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
1604     as x(a int, b json, c boolean);
1605  a |      b      | c 
1606 ---+-------------+---
1607  1 | {"d":"foo"} | t
1608  2 | {"d":"bar"} | f
1609 (2 rows)
1610
1611 select *, c is null as c_is_null
1612 from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
1613     as t(a int, b json, c text, x int);
1614  a |        b        | c | x | c_is_null 
1615 ---+-----------------+---+---+-----------
1616  1 | {"c":16, "d":2} |   | 8 | t
1617 (1 row)
1618
1619 select *, c is null as c_is_null
1620 from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
1621     as t(a int, b json, c text, x int);
1622  a |        b        | c | x | c_is_null 
1623 ---+-----------------+---+---+-----------
1624  1 | {"c":16, "d":2} |   | 8 | t
1625 (1 row)
1626
1627 -- json_strip_nulls
1628 select json_strip_nulls(null);
1629  json_strip_nulls 
1630 ------------------
1631  
1632 (1 row)
1633
1634 select json_strip_nulls('1');
1635  json_strip_nulls 
1636 ------------------
1637  1
1638 (1 row)
1639
1640 select json_strip_nulls('"a string"');
1641  json_strip_nulls 
1642 ------------------
1643  "a string"
1644 (1 row)
1645
1646 select json_strip_nulls('null');
1647  json_strip_nulls 
1648 ------------------
1649  null
1650 (1 row)
1651
1652 select json_strip_nulls('[1,2,null,3,4]');
1653  json_strip_nulls 
1654 ------------------
1655  [1,2,null,3,4]
1656 (1 row)
1657
1658 select json_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
1659           json_strip_nulls          
1660 ------------------------------------
1661  {"a":1,"c":[2,null,3],"d":{"e":4}}
1662 (1 row)
1663
1664 select json_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
1665   json_strip_nulls   
1666 ---------------------
1667  [1,{"a":1,"c":2},3]
1668 (1 row)
1669
1670 -- an empty object is not null and should not be stripped
1671 select json_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
1672  json_strip_nulls 
1673 ------------------
1674  {"a":{},"d":{}}
1675 (1 row)
1676
1677 -- json to tsvector
1678 select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
1679                                 to_tsvector                                
1680 ---------------------------------------------------------------------------
1681  'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
1682 (1 row)
1683
1684 -- json to tsvector with config
1685 select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::json);
1686                                 to_tsvector                                
1687 ---------------------------------------------------------------------------
1688  'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
1689 (1 row)
1690
1691 -- json to tsvector with stop words
1692 select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::json);
1693                                 to_tsvector                                 
1694 ----------------------------------------------------------------------------
1695  'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
1696 (1 row)
1697
1698 -- ts_vector corner cases
1699 select to_tsvector('""'::json);
1700  to_tsvector 
1701 -------------
1702  
1703 (1 row)
1704
1705 select to_tsvector('{}'::json);
1706  to_tsvector 
1707 -------------
1708  
1709 (1 row)
1710
1711 select to_tsvector('[]'::json);
1712  to_tsvector 
1713 -------------
1714  
1715 (1 row)
1716
1717 select to_tsvector('null'::json);
1718  to_tsvector 
1719 -------------
1720  
1721 (1 row)
1722
1723 -- ts_headline for json
1724 select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
1725                                                ts_headline                                               
1726 ---------------------------------------------------------------------------------------------------------
1727  {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff","c1":"ccc1 ddd1"},"d":["ggg <b>hhh</b>","iii jjj"]}
1728 (1 row)
1729
1730 select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::json, tsquery('bbb & ddd & hhh'));
1731                                       ts_headline                                       
1732 ----------------------------------------------------------------------------------------
1733  {"a":"aaa <b>bbb</b>","b":{"c":"ccc <b>ddd</b> fff"},"d":["ggg <b>hhh</b>","iii jjj"]}
1734 (1 row)
1735
1736 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 = >');
1737                                        ts_headline                                        
1738 ------------------------------------------------------------------------------------------
1739  {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
1740 (1 row)
1741
1742 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 = >');
1743                                        ts_headline                                        
1744 ------------------------------------------------------------------------------------------
1745  {"a":"aaa <bbb>","b":{"c":"ccc <ddd> fff","c1":"ccc1 ddd1"},"d":["ggg <hhh>","iii jjj"]}
1746 (1 row)
1747
1748 -- corner cases for ts_headline with json
1749 select ts_headline('null'::json, tsquery('aaa & bbb'));
1750  ts_headline 
1751 -------------
1752  null
1753 (1 row)
1754
1755 select ts_headline('{}'::json, tsquery('aaa & bbb'));
1756  ts_headline 
1757 -------------
1758  {}
1759 (1 row)
1760
1761 select ts_headline('[]'::json, tsquery('aaa & bbb'));
1762  ts_headline 
1763 -------------
1764  []
1765 (1 row)
1766