]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/jsonb_1.out
Improve documentation of the role-dropping process.
[postgresql] / src / test / regress / expected / jsonb_1.out
1 -- Strings.
2 SELECT '""'::jsonb;                             -- OK.
3  jsonb 
4 -------
5  ""
6 (1 row)
7
8 SELECT $$''$$::jsonb;                   -- ERROR, single quotes are not allowed
9 ERROR:  invalid input syntax for type json
10 LINE 1: SELECT $$''$$::jsonb;
11                ^
12 DETAIL:  Token "'" is invalid.
13 CONTEXT:  JSON data, line 1: '...
14 SELECT '"abc"'::jsonb;                  -- OK
15  jsonb 
16 -------
17  "abc"
18 (1 row)
19
20 SELECT '"abc'::jsonb;                   -- ERROR, quotes not closed
21 ERROR:  invalid input syntax for type json
22 LINE 1: SELECT '"abc'::jsonb;
23                ^
24 DETAIL:  Token ""abc" is invalid.
25 CONTEXT:  JSON data, line 1: "abc
26 SELECT '"abc
27 def"'::jsonb;                                   -- 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\"\\"'::jsonb;               -- OK, legal escapes
34   jsonb   
35 ----------
36  "\n\"\\"
37 (1 row)
38
39 SELECT '"\v"'::jsonb;                   -- ERROR, not a valid JSON escape
40 ERROR:  invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::jsonb;
42                ^
43 DETAIL:  Escape sequence "\v" is invalid.
44 CONTEXT:  JSON data, line 1: "\v...
45 SELECT '"\u"'::jsonb;                   -- ERROR, incomplete escape
46 ERROR:  invalid input syntax for type json
47 LINE 1: SELECT '"\u"'::jsonb;
48                ^
49 DETAIL:  "\u" must be followed by four hexadecimal digits.
50 CONTEXT:  JSON data, line 1: "\u"
51 SELECT '"\u00"'::jsonb;                 -- ERROR, incomplete escape
52 ERROR:  invalid input syntax for type json
53 LINE 1: SELECT '"\u00"'::jsonb;
54                ^
55 DETAIL:  "\u" must be followed by four hexadecimal digits.
56 CONTEXT:  JSON data, line 1: "\u00"
57 SELECT '"\u000g"'::jsonb;               -- ERROR, g is not a hex digit
58 ERROR:  invalid input syntax for type json
59 LINE 1: SELECT '"\u000g"'::jsonb;
60                ^
61 DETAIL:  "\u" must be followed by four hexadecimal digits.
62 CONTEXT:  JSON data, line 1: "\u000g...
63 SELECT '"\u0045"'::jsonb;               -- OK, legal escape
64  jsonb 
65 -------
66  "E"
67 (1 row)
68
69 SELECT '"\u0000"'::jsonb;               -- ERROR, we don't support U+0000
70 ERROR:  unsupported Unicode escape sequence
71 LINE 1: SELECT '"\u0000"'::jsonb;
72                ^
73 DETAIL:  \u0000 cannot be converted to text.
74 CONTEXT:  JSON data, line 1: ...
75 -- use octet_length here so we don't get an odd unicode char in the
76 -- output
77 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
78 ERROR:  unsupported Unicode escape sequence
79 LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text);
80                             ^
81 DETAIL:  Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8.
82 CONTEXT:  JSON data, line 1: ...
83 -- Numbers.
84 SELECT '1'::jsonb;                              -- OK
85  jsonb 
86 -------
87  1
88 (1 row)
89
90 SELECT '0'::jsonb;                              -- OK
91  jsonb 
92 -------
93  0
94 (1 row)
95
96 SELECT '01'::jsonb;                             -- ERROR, not valid according to JSON spec
97 ERROR:  invalid input syntax for type json
98 LINE 1: SELECT '01'::jsonb;
99                ^
100 DETAIL:  Token "01" is invalid.
101 CONTEXT:  JSON data, line 1: 01
102 SELECT '0.1'::jsonb;                            -- OK
103  jsonb 
104 -------
105  0.1
106 (1 row)
107
108 SELECT '9223372036854775808'::jsonb;    -- OK, even though it's too large for int8
109         jsonb        
110 ---------------------
111  9223372036854775808
112 (1 row)
113
114 SELECT '1e100'::jsonb;                  -- OK
115                                                  jsonb                                                 
116 -------------------------------------------------------------------------------------------------------
117  10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
118 (1 row)
119
120 SELECT '1.3e100'::jsonb;                        -- OK
121                                                  jsonb                                                 
122 -------------------------------------------------------------------------------------------------------
123  13000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
124 (1 row)
125
126 SELECT '1f2'::jsonb;                            -- ERROR
127 ERROR:  invalid input syntax for type json
128 LINE 1: SELECT '1f2'::jsonb;
129                ^
130 DETAIL:  Token "1f2" is invalid.
131 CONTEXT:  JSON data, line 1: 1f2
132 SELECT '0.x1'::jsonb;                   -- ERROR
133 ERROR:  invalid input syntax for type json
134 LINE 1: SELECT '0.x1'::jsonb;
135                ^
136 DETAIL:  Token "0.x1" is invalid.
137 CONTEXT:  JSON data, line 1: 0.x1
138 SELECT '1.3ex100'::jsonb;               -- ERROR
139 ERROR:  invalid input syntax for type json
140 LINE 1: SELECT '1.3ex100'::jsonb;
141                ^
142 DETAIL:  Token "1.3ex100" is invalid.
143 CONTEXT:  JSON data, line 1: 1.3ex100
144 -- Arrays.
145 SELECT '[]'::jsonb;                             -- OK
146  jsonb 
147 -------
148  []
149 (1 row)
150
151 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb;  -- OK
152                                                                                                   jsonb                                                                                                   
153 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
154  [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
155 (1 row)
156
157 SELECT '[1,2]'::jsonb;                  -- OK
158  jsonb  
159 --------
160  [1, 2]
161 (1 row)
162
163 SELECT '[1,2,]'::jsonb;                 -- ERROR, trailing comma
164 ERROR:  invalid input syntax for type json
165 LINE 1: SELECT '[1,2,]'::jsonb;
166                ^
167 DETAIL:  Expected JSON value, but found "]".
168 CONTEXT:  JSON data, line 1: [1,2,]
169 SELECT '[1,2'::jsonb;                   -- ERROR, no closing bracket
170 ERROR:  invalid input syntax for type json
171 LINE 1: SELECT '[1,2'::jsonb;
172                ^
173 DETAIL:  The input string ended unexpectedly.
174 CONTEXT:  JSON data, line 1: [1,2
175 SELECT '[1,[2]'::jsonb;                 -- ERROR, no closing bracket
176 ERROR:  invalid input syntax for type json
177 LINE 1: SELECT '[1,[2]'::jsonb;
178                ^
179 DETAIL:  The input string ended unexpectedly.
180 CONTEXT:  JSON data, line 1: [1,[2]
181 -- Objects.
182 SELECT '{}'::jsonb;                             -- OK
183  jsonb 
184 -------
185  {}
186 (1 row)
187
188 SELECT '{"abc"}'::jsonb;                        -- ERROR, no value
189 ERROR:  invalid input syntax for type json
190 LINE 1: SELECT '{"abc"}'::jsonb;
191                ^
192 DETAIL:  Expected ":", but found "}".
193 CONTEXT:  JSON data, line 1: {"abc"}
194 SELECT '{"abc":1}'::jsonb;              -- OK
195    jsonb    
196 ------------
197  {"abc": 1}
198 (1 row)
199
200 SELECT '{1:"abc"}'::jsonb;              -- ERROR, keys must be strings
201 ERROR:  invalid input syntax for type json
202 LINE 1: SELECT '{1:"abc"}'::jsonb;
203                ^
204 DETAIL:  Expected string or "}", but found "1".
205 CONTEXT:  JSON data, line 1: {1...
206 SELECT '{"abc",1}'::jsonb;              -- ERROR, wrong separator
207 ERROR:  invalid input syntax for type json
208 LINE 1: SELECT '{"abc",1}'::jsonb;
209                ^
210 DETAIL:  Expected ":", but found ",".
211 CONTEXT:  JSON data, line 1: {"abc",...
212 SELECT '{"abc"=1}'::jsonb;              -- ERROR, totally wrong separator
213 ERROR:  invalid input syntax for type json
214 LINE 1: SELECT '{"abc"=1}'::jsonb;
215                ^
216 DETAIL:  Token "=" is invalid.
217 CONTEXT:  JSON data, line 1: {"abc"=...
218 SELECT '{"abc"::1}'::jsonb;             -- ERROR, another wrong separator
219 ERROR:  invalid input syntax for type json
220 LINE 1: SELECT '{"abc"::1}'::jsonb;
221                ^
222 DETAIL:  Expected JSON value, but found ":".
223 CONTEXT:  JSON data, line 1: {"abc"::...
224 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
225                                jsonb                                
226 --------------------------------------------------------------------
227  {"abc": 1, "def": 2, "ghi": [3, 4], "hij": {"klm": 5, "nop": [6]}}
228 (1 row)
229
230 SELECT '{"abc":1:2}'::jsonb;            -- ERROR, colon in wrong spot
231 ERROR:  invalid input syntax for type json
232 LINE 1: SELECT '{"abc":1:2}'::jsonb;
233                ^
234 DETAIL:  Expected "," or "}", but found ":".
235 CONTEXT:  JSON data, line 1: {"abc":1:...
236 SELECT '{"abc":1,3}'::jsonb;            -- ERROR, no value
237 ERROR:  invalid input syntax for type json
238 LINE 1: SELECT '{"abc":1,3}'::jsonb;
239                ^
240 DETAIL:  Expected string, but found "3".
241 CONTEXT:  JSON data, line 1: {"abc":1,3...
242 -- Recursion.
243 SET max_stack_depth = '100kB';
244 SELECT repeat('[', 10000)::jsonb;
245 ERROR:  stack depth limit exceeded
246 HINT:  Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
247 SELECT repeat('{"a":', 10000)::jsonb;
248 ERROR:  stack depth limit exceeded
249 HINT:  Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
250 RESET max_stack_depth;
251 -- Miscellaneous stuff.
252 SELECT 'true'::jsonb;                   -- OK
253  jsonb 
254 -------
255  true
256 (1 row)
257
258 SELECT 'false'::jsonb;                  -- OK
259  jsonb 
260 -------
261  false
262 (1 row)
263
264 SELECT 'null'::jsonb;                   -- OK
265  jsonb 
266 -------
267  null
268 (1 row)
269
270 SELECT ' true '::jsonb;                 -- OK, even with extra whitespace
271  jsonb 
272 -------
273  true
274 (1 row)
275
276 SELECT 'true false'::jsonb;             -- ERROR, too many values
277 ERROR:  invalid input syntax for type json
278 LINE 1: SELECT 'true false'::jsonb;
279                ^
280 DETAIL:  Expected end of input, but found "false".
281 CONTEXT:  JSON data, line 1: true false
282 SELECT 'true, false'::jsonb;            -- ERROR, too many values
283 ERROR:  invalid input syntax for type json
284 LINE 1: SELECT 'true, false'::jsonb;
285                ^
286 DETAIL:  Expected end of input, but found ",".
287 CONTEXT:  JSON data, line 1: true,...
288 SELECT 'truf'::jsonb;                   -- ERROR, not a keyword
289 ERROR:  invalid input syntax for type json
290 LINE 1: SELECT 'truf'::jsonb;
291                ^
292 DETAIL:  Token "truf" is invalid.
293 CONTEXT:  JSON data, line 1: truf
294 SELECT 'trues'::jsonb;                  -- ERROR, not a keyword
295 ERROR:  invalid input syntax for type json
296 LINE 1: SELECT 'trues'::jsonb;
297                ^
298 DETAIL:  Token "trues" is invalid.
299 CONTEXT:  JSON data, line 1: trues
300 SELECT ''::jsonb;                               -- ERROR, no value
301 ERROR:  invalid input syntax for type json
302 LINE 1: SELECT ''::jsonb;
303                ^
304 DETAIL:  The input string ended unexpectedly.
305 CONTEXT:  JSON data, line 1: 
306 SELECT '    '::jsonb;                   -- ERROR, no value
307 ERROR:  invalid input syntax for type json
308 LINE 1: SELECT '    '::jsonb;
309                ^
310 DETAIL:  The input string ended unexpectedly.
311 CONTEXT:  JSON data, line 1:     
312 -- make sure jsonb is passed through json generators without being escaped
313 SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
314       array_to_json       
315 --------------------------
316  [{"a": 1},{"b": [2, 3]}]
317 (1 row)
318
319 -- to_jsonb, timestamps
320 select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
321            to_jsonb           
322 ------------------------------
323  "2014-05-28T12:22:35.614298"
324 (1 row)
325
326 BEGIN;
327 SET LOCAL TIME ZONE 10.5;
328 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
329               to_jsonb              
330 ------------------------------------
331  "2014-05-29T02:52:35.614298+10:30"
332 (1 row)
333
334 SET LOCAL TIME ZONE -8;
335 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
336               to_jsonb              
337 ------------------------------------
338  "2014-05-28T08:22:35.614298-08:00"
339 (1 row)
340
341 COMMIT;
342 select to_jsonb(date '2014-05-28');
343    to_jsonb   
344 --------------
345  "2014-05-28"
346 (1 row)
347
348 select to_jsonb(date 'Infinity');
349   to_jsonb  
350 ------------
351  "infinity"
352 (1 row)
353
354 select to_jsonb(timestamp 'Infinity');
355   to_jsonb  
356 ------------
357  "infinity"
358 (1 row)
359
360 select to_jsonb(timestamptz 'Infinity');
361   to_jsonb  
362 ------------
363  "infinity"
364 (1 row)
365
366 --jsonb_agg
367 CREATE TEMP TABLE rows AS
368 SELECT x, 'txt' || x as y
369 FROM generate_series(1,3) AS x;
370 SELECT jsonb_agg(q)
371   FROM ( SELECT $$a$$ || x AS b, y AS c,
372                ARRAY[ROW(x.*,ARRAY[1,2,3]),
373                ROW(y.*,ARRAY[4,5,6])] AS z
374          FROM generate_series(1,2) x,
375               generate_series(4,5) y) q;
376                                                                                                                                                                     jsonb_agg                                                                                                                                                                     
377 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
378  [{"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]}]}]
379 (1 row)
380
381 SELECT jsonb_agg(q ORDER BY x, y)
382   FROM rows q;
383                                jsonb_agg                               
384 -----------------------------------------------------------------------
385  [{"x": 1, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
386 (1 row)
387
388 UPDATE rows SET x = NULL WHERE x = 1;
389 SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
390   FROM rows q;
391                                 jsonb_agg                                 
392 --------------------------------------------------------------------------
393  [{"x": null, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
394 (1 row)
395
396 -- jsonb extraction functions
397 CREATE TEMP TABLE test_jsonb (
398        json_type text,
399        test_json jsonb
400 );
401 INSERT INTO test_jsonb VALUES
402 ('scalar','"a scalar"'),
403 ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
404 ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
405 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
406  ?column? 
407 ----------
408  
409 (1 row)
410
411 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
412  ?column? 
413 ----------
414  
415 (1 row)
416
417 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
418  ?column? 
419 ----------
420  
421 (1 row)
422
423 SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
424  ?column? 
425 ----------
426  "val2"
427 (1 row)
428
429 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
430  ?column? 
431 ----------
432  
433 (1 row)
434
435 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
436  ?column? 
437 ----------
438  
439 (1 row)
440
441 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
442  ?column? 
443 ----------
444  val2
445 (1 row)
446
447 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
448  ?column? 
449 ----------
450  
451 (1 row)
452
453 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
454  ?column? 
455 ----------
456  "two"
457 (1 row)
458
459 SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
460  ?column? 
461 ----------
462  
463 (1 row)
464
465 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
466  ?column? 
467 ----------
468  
469 (1 row)
470
471 SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
472  ?column?  
473 -----------
474  [1, 2, 3]
475 (1 row)
476
477 SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
478  ?column?  
479 -----------
480  {"f1": 9}
481 (1 row)
482
483 SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
484  ?column? 
485 ----------
486  4
487 (1 row)
488
489 SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
490  ?column?  
491 -----------
492  [1, 2, 3]
493 (1 row)
494
495 SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
496  ?column?  
497 -----------
498  {"f1": 9}
499 (1 row)
500
501 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
502  ?column? 
503 ----------
504  
505 (1 row)
506
507 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
508  ?column? 
509 ----------
510  two
511 (1 row)
512
513 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
514  ?column? 
515 ----------
516  
517 (1 row)
518
519 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
520 ERROR:  cannot call jsonb_object_keys on a scalar
521 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
522 ERROR:  cannot call jsonb_object_keys on an array
523 SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object';
524  jsonb_object_keys 
525 -------------------
526  field1
527  field2
528  field3
529  field4
530  field5
531  field6
532 (6 rows)
533
534 -- nulls
535 SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
536  expect_false 
537 --------------
538  f
539 (1 row)
540
541 SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
542  expect_true 
543 -------------
544  t
545 (1 row)
546
547 SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
548  expect_false 
549 --------------
550  f
551 (1 row)
552
553 SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
554  expect_true 
555 -------------
556  t
557 (1 row)
558
559 -- corner cases
560 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
561  ?column? 
562 ----------
563  
564 (1 row)
565
566 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
567  ?column? 
568 ----------
569  
570 (1 row)
571
572 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
573  ?column? 
574 ----------
575  
576 (1 row)
577
578 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
579  ?column? 
580 ----------
581  
582 (1 row)
583
584 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
585  ?column? 
586 ----------
587  
588 (1 row)
589
590 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
591   ?column?   
592 -------------
593  {"b": "cc"}
594 (1 row)
595
596 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
597  ?column? 
598 ----------
599  
600 (1 row)
601
602 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
603  ?column? 
604 ----------
605  
606 (1 row)
607
608 select '{"a": "c", "b": null}'::jsonb -> 'b';
609  ?column? 
610 ----------
611  null
612 (1 row)
613
614 select '"foo"'::jsonb -> 1;
615  ?column? 
616 ----------
617  
618 (1 row)
619
620 select '"foo"'::jsonb -> 'z';
621  ?column? 
622 ----------
623  
624 (1 row)
625
626 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
627  ?column? 
628 ----------
629  
630 (1 row)
631
632 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
633  ?column? 
634 ----------
635  
636 (1 row)
637
638 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
639  ?column? 
640 ----------
641  
642 (1 row)
643
644 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
645  ?column? 
646 ----------
647  
648 (1 row)
649
650 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
651  ?column? 
652 ----------
653  
654 (1 row)
655
656 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
657   ?column?   
658 -------------
659  {"b": "cc"}
660 (1 row)
661
662 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
663  ?column? 
664 ----------
665  
666 (1 row)
667
668 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
669  ?column? 
670 ----------
671  
672 (1 row)
673
674 select '{"a": "c", "b": null}'::jsonb ->> 'b';
675  ?column? 
676 ----------
677  
678 (1 row)
679
680 select '"foo"'::jsonb ->> 1;
681  ?column? 
682 ----------
683  
684 (1 row)
685
686 select '"foo"'::jsonb ->> 'z';
687  ?column? 
688 ----------
689  
690 (1 row)
691
692 -- equality and inequality
693 SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
694  ?column? 
695 ----------
696  t
697 (1 row)
698
699 SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
700  ?column? 
701 ----------
702  f
703 (1 row)
704
705 SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
706  ?column? 
707 ----------
708  f
709 (1 row)
710
711 SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
712  ?column? 
713 ----------
714  t
715 (1 row)
716
717 -- containment
718 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
719  jsonb_contains 
720 ----------------
721  t
722 (1 row)
723
724 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
725  jsonb_contains 
726 ----------------
727  t
728 (1 row)
729
730 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
731  jsonb_contains 
732 ----------------
733  f
734 (1 row)
735
736 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
737  jsonb_contains 
738 ----------------
739  f
740 (1 row)
741
742 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
743  jsonb_contains 
744 ----------------
745  f
746 (1 row)
747
748 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
749  jsonb_contains 
750 ----------------
751  t
752 (1 row)
753
754 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
755  jsonb_contains 
756 ----------------
757  f
758 (1 row)
759
760 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
761  ?column? 
762 ----------
763  t
764 (1 row)
765
766 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
767  ?column? 
768 ----------
769  t
770 (1 row)
771
772 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
773  ?column? 
774 ----------
775  f
776 (1 row)
777
778 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
779  ?column? 
780 ----------
781  f
782 (1 row)
783
784 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
785  ?column? 
786 ----------
787  f
788 (1 row)
789
790 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
791  ?column? 
792 ----------
793  t
794 (1 row)
795
796 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
797  ?column? 
798 ----------
799  f
800 (1 row)
801
802 SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
803  ?column? 
804 ----------
805  t
806 (1 row)
807
808 SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
809  ?column? 
810 ----------
811  t
812 (1 row)
813
814 SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
815  ?column? 
816 ----------
817  t
818 (1 row)
819
820 SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
821  ?column? 
822 ----------
823  t
824 (1 row)
825
826 SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
827  ?column? 
828 ----------
829  t
830 (1 row)
831
832 SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
833  ?column? 
834 ----------
835  t
836 (1 row)
837
838 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
839  jsonb_contained 
840 -----------------
841  t
842 (1 row)
843
844 SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
845  jsonb_contained 
846 -----------------
847  t
848 (1 row)
849
850 SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
851  jsonb_contained 
852 -----------------
853  f
854 (1 row)
855
856 SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
857  jsonb_contained 
858 -----------------
859  f
860 (1 row)
861
862 SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
863  jsonb_contained 
864 -----------------
865  f
866 (1 row)
867
868 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
869  jsonb_contained 
870 -----------------
871  t
872 (1 row)
873
874 SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
875  jsonb_contained 
876 -----------------
877  f
878 (1 row)
879
880 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
881  ?column? 
882 ----------
883  t
884 (1 row)
885
886 SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
887  ?column? 
888 ----------
889  t
890 (1 row)
891
892 SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
893  ?column? 
894 ----------
895  f
896 (1 row)
897
898 SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
899  ?column? 
900 ----------
901  f
902 (1 row)
903
904 SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
905  ?column? 
906 ----------
907  f
908 (1 row)
909
910 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
911  ?column? 
912 ----------
913  t
914 (1 row)
915
916 SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
917  ?column? 
918 ----------
919  f
920 (1 row)
921
922 -- Raw scalar may contain another raw scalar, array may contain a raw scalar
923 SELECT '[5]'::jsonb @> '[5]';
924  ?column? 
925 ----------
926  t
927 (1 row)
928
929 SELECT '5'::jsonb @> '5';
930  ?column? 
931 ----------
932  t
933 (1 row)
934
935 SELECT '[5]'::jsonb @> '5';
936  ?column? 
937 ----------
938  t
939 (1 row)
940
941 -- But a raw scalar cannot contain an array
942 SELECT '5'::jsonb @> '[5]';
943  ?column? 
944 ----------
945  f
946 (1 row)
947
948 -- In general, one thing should always contain itself. Test array containment:
949 SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
950  ?column? 
951 ----------
952  t
953 (1 row)
954
955 SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
956  ?column? 
957 ----------
958  t
959 (1 row)
960
961 -- array containment string matching confusion bug
962 SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
963  ?column? 
964 ----------
965  f
966 (1 row)
967
968 -- array length
969 SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
970  jsonb_array_length 
971 --------------------
972                   5
973 (1 row)
974
975 SELECT jsonb_array_length('[]');
976  jsonb_array_length 
977 --------------------
978                   0
979 (1 row)
980
981 SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
982 ERROR:  cannot get array length of a non-array
983 SELECT jsonb_array_length('4');
984 ERROR:  cannot get array length of a scalar
985 -- each
986 SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
987      jsonb_each     
988 --------------------
989  (f1,"[1, 2, 3]")
990  (f2,"{""f3"": 1}")
991  (f4,null)
992 (3 rows)
993
994 SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
995                           q                           
996 ------------------------------------------------------
997  (1,"""first""")
998  (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
999  (b,"[1, 2]")
1000  (c,"""cc""")
1001  (n,null)
1002 (5 rows)
1003
1004 SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
1005  key |   value   
1006 -----+-----------
1007  f1  | [1, 2, 3]
1008  f2  | {"f3": 1}
1009  f4  | null
1010  f5  | 99
1011  f6  | "stringy"
1012 (5 rows)
1013
1014 SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1015  key |               value                
1016 -----+------------------------------------
1017  1   | "first"
1018  a   | {"1": "first", "b": "c", "c": "b"}
1019  b   | [1, 2]
1020  c   | "cc"
1021  n   | null
1022 (5 rows)
1023
1024 SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
1025   jsonb_each_text   
1026 --------------------
1027  (f1,"[1, 2, 3]")
1028  (f2,"{""f3"": 1}")
1029  (f4,)
1030  (f5,null)
1031 (4 rows)
1032
1033 SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1034                           q                           
1035 ------------------------------------------------------
1036  (1,first)
1037  (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
1038  (b,"[1, 2]")
1039  (c,cc)
1040  (n,)
1041 (5 rows)
1042
1043 SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
1044  key |   value   
1045 -----+-----------
1046  f1  | [1, 2, 3]
1047  f2  | {"f3": 1}
1048  f4  | 
1049  f5  | 99
1050  f6  | stringy
1051 (5 rows)
1052
1053 SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1054  key |               value                
1055 -----+------------------------------------
1056  1   | first
1057  a   | {"1": "first", "b": "c", "c": "b"}
1058  b   | [1, 2]
1059  c   | cc
1060  n   | 
1061 (5 rows)
1062
1063 -- exists
1064 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
1065  jsonb_exists 
1066 --------------
1067  t
1068 (1 row)
1069
1070 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
1071  jsonb_exists 
1072 --------------
1073  t
1074 (1 row)
1075
1076 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
1077  jsonb_exists 
1078 --------------
1079  f
1080 (1 row)
1081
1082 SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
1083  jsonb_exists 
1084 --------------
1085  t
1086 (1 row)
1087
1088 SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
1089  ?column? 
1090 ----------
1091  t
1092 (1 row)
1093
1094 SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
1095  ?column? 
1096 ----------
1097  t
1098 (1 row)
1099
1100 SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
1101  ?column? 
1102 ----------
1103  f
1104 (1 row)
1105
1106 SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
1107  ?column? 
1108 ----------
1109  t
1110 (1 row)
1111
1112 -- array exists - array elements should behave as keys
1113 SELECT count(*) from testjsonb  WHERE j->'array' ? 'bar';
1114  count 
1115 -------
1116      3
1117 (1 row)
1118
1119 -- type sensitive array exists - should return no rows (since "exists" only
1120 -- matches strings that are either object keys or array elements)
1121 SELECT count(*) from testjsonb  WHERE j->'array' ? '5'::text;
1122  count 
1123 -------
1124      0
1125 (1 row)
1126
1127 -- However, a raw scalar is *contained* within the array
1128 SELECT count(*) from testjsonb  WHERE j->'array' @> '5'::jsonb;
1129  count 
1130 -------
1131      1
1132 (1 row)
1133
1134 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
1135  jsonb_exists_any 
1136 ------------------
1137  t
1138 (1 row)
1139
1140 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
1141  jsonb_exists_any 
1142 ------------------
1143  t
1144 (1 row)
1145
1146 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
1147  jsonb_exists_any 
1148 ------------------
1149  t
1150 (1 row)
1151
1152 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
1153  jsonb_exists_any 
1154 ------------------
1155  f
1156 (1 row)
1157
1158 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
1159  jsonb_exists_any 
1160 ------------------
1161  f
1162 (1 row)
1163
1164 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
1165  ?column? 
1166 ----------
1167  t
1168 (1 row)
1169
1170 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
1171  ?column? 
1172 ----------
1173  t
1174 (1 row)
1175
1176 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
1177  ?column? 
1178 ----------
1179  t
1180 (1 row)
1181
1182 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
1183  ?column? 
1184 ----------
1185  f
1186 (1 row)
1187
1188 SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
1189  ?column? 
1190 ----------
1191  f
1192 (1 row)
1193
1194 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
1195  jsonb_exists_all 
1196 ------------------
1197  t
1198 (1 row)
1199
1200 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
1201  jsonb_exists_all 
1202 ------------------
1203  t
1204 (1 row)
1205
1206 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
1207  jsonb_exists_all 
1208 ------------------
1209  f
1210 (1 row)
1211
1212 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
1213  jsonb_exists_all 
1214 ------------------
1215  f
1216 (1 row)
1217
1218 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
1219  jsonb_exists_all 
1220 ------------------
1221  t
1222 (1 row)
1223
1224 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
1225  ?column? 
1226 ----------
1227  t
1228 (1 row)
1229
1230 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
1231  ?column? 
1232 ----------
1233  t
1234 (1 row)
1235
1236 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
1237  ?column? 
1238 ----------
1239  f
1240 (1 row)
1241
1242 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
1243  ?column? 
1244 ----------
1245  f
1246 (1 row)
1247
1248 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
1249  ?column? 
1250 ----------
1251  t
1252 (1 row)
1253
1254 SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
1255  ?column? 
1256 ----------
1257  t
1258 (1 row)
1259
1260 -- typeof
1261 SELECT jsonb_typeof('{}') AS object;
1262  object 
1263 --------
1264  object
1265 (1 row)
1266
1267 SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
1268  object 
1269 --------
1270  object
1271 (1 row)
1272
1273 SELECT jsonb_typeof('[]') AS array;
1274  array 
1275 -------
1276  array
1277 (1 row)
1278
1279 SELECT jsonb_typeof('["a", 1]') AS array;
1280  array 
1281 -------
1282  array
1283 (1 row)
1284
1285 SELECT jsonb_typeof('null') AS "null";
1286  null 
1287 ------
1288  null
1289 (1 row)
1290
1291 SELECT jsonb_typeof('1') AS number;
1292  number 
1293 --------
1294  number
1295 (1 row)
1296
1297 SELECT jsonb_typeof('-1') AS number;
1298  number 
1299 --------
1300  number
1301 (1 row)
1302
1303 SELECT jsonb_typeof('1.0') AS number;
1304  number 
1305 --------
1306  number
1307 (1 row)
1308
1309 SELECT jsonb_typeof('1e2') AS number;
1310  number 
1311 --------
1312  number
1313 (1 row)
1314
1315 SELECT jsonb_typeof('-1.0') AS number;
1316  number 
1317 --------
1318  number
1319 (1 row)
1320
1321 SELECT jsonb_typeof('true') AS boolean;
1322  boolean 
1323 ---------
1324  boolean
1325 (1 row)
1326
1327 SELECT jsonb_typeof('false') AS boolean;
1328  boolean 
1329 ---------
1330  boolean
1331 (1 row)
1332
1333 SELECT jsonb_typeof('"hello"') AS string;
1334  string 
1335 --------
1336  string
1337 (1 row)
1338
1339 SELECT jsonb_typeof('"true"') AS string;
1340  string 
1341 --------
1342  string
1343 (1 row)
1344
1345 SELECT jsonb_typeof('"1.0"') AS string;
1346  string 
1347 --------
1348  string
1349 (1 row)
1350
1351 -- jsonb_build_array, jsonb_build_object, jsonb_object_agg
1352 SELECT jsonb_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1353                             jsonb_build_array                            
1354 -------------------------------------------------------------------------
1355  ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1, 2, 3]}]
1356 (1 row)
1357
1358 SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1359                            jsonb_build_object                            
1360 -------------------------------------------------------------------------
1361  {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1, 2, 3]}}
1362 (1 row)
1363
1364 SELECT jsonb_build_object(
1365        'a', jsonb_build_object('b',false,'c',99),
1366        'd', jsonb_build_object('e',array[9,8,7]::int[],
1367            'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
1368                                        jsonb_build_object                                       
1369 ------------------------------------------------------------------------------------------------
1370  {"a": {"b": false, "c": 99}, "d": {"e": [9, 8, 7], "f": {"name": "pg_class", "relkind": "r"}}}
1371 (1 row)
1372
1373 -- empty objects/arrays
1374 SELECT jsonb_build_array();
1375  jsonb_build_array 
1376 -------------------
1377  []
1378 (1 row)
1379
1380 SELECT jsonb_build_object();
1381  jsonb_build_object 
1382 --------------------
1383  {}
1384 (1 row)
1385
1386 -- make sure keys are quoted
1387 SELECT jsonb_build_object(1,2);
1388  jsonb_build_object 
1389 --------------------
1390  {"1": 2}
1391 (1 row)
1392
1393 -- keys must be scalar and not null
1394 SELECT jsonb_build_object(null,2);
1395 ERROR:  arg 1: key cannot be null
1396 SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
1397 ERROR:  key value must be scalar, not array, composite or json
1398 SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
1399 ERROR:  key value must be scalar, not array, composite or json
1400 SELECT jsonb_build_object('{1,2,3}'::int[], 3);
1401 ERROR:  key value must be scalar, not array, composite or json
1402 CREATE TEMP TABLE foo (serial_num int, name text, type text);
1403 INSERT INTO foo VALUES (847001,'t15','GE1043');
1404 INSERT INTO foo VALUES (847002,'t16','GE1043');
1405 INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
1406 SELECT jsonb_build_object('turbines',jsonb_object_agg(serial_num,jsonb_build_object('name',name,'type',type)))
1407 FROM foo;
1408                                                                      jsonb_build_object                                                                      
1409 -------------------------------------------------------------------------------------------------------------------------------------------------------------
1410  {"turbines": {"847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"}}}
1411 (1 row)
1412
1413 SELECT jsonb_object_agg(name, type) FROM foo;
1414                      jsonb_object_agg                      
1415 -----------------------------------------------------------
1416  {"t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90"}
1417 (1 row)
1418
1419 INSERT INTO foo VALUES (999999, NULL, 'bar');
1420 SELECT jsonb_object_agg(name, type) FROM foo;
1421 ERROR:  field name must not be null
1422 -- jsonb_object
1423 -- one dimension
1424 SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1425                    jsonb_object                    
1426 ---------------------------------------------------
1427  {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1428 (1 row)
1429
1430 -- same but with two dimensions
1431 SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1432                    jsonb_object                    
1433 ---------------------------------------------------
1434  {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1435 (1 row)
1436
1437 -- odd number error
1438 SELECT jsonb_object('{a,b,c}');
1439 ERROR:  array must have even number of elements
1440 -- one column error
1441 SELECT jsonb_object('{{a},{b}}');
1442 ERROR:  array must have two columns
1443 -- too many columns error
1444 SELECT jsonb_object('{{a,b,c},{b,c,d}}');
1445 ERROR:  array must have two columns
1446 -- too many dimensions error
1447 SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
1448 ERROR:  wrong number of array subscripts
1449 --two argument form of jsonb_object
1450 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
1451                    jsonb_object                   
1452 --------------------------------------------------
1453  {"a": "1", "b": "2", "c": "3", "d e f": "a b c"}
1454 (1 row)
1455
1456 -- too many dimensions
1457 SELECT jsonb_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"}}');
1458 ERROR:  wrong number of array subscripts
1459 -- mismatched dimensions
1460 select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
1461 ERROR:  mismatched array dimensions
1462 select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
1463 ERROR:  mismatched array dimensions
1464 -- null key error
1465 select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
1466 ERROR:  null value not allowed for object key
1467 -- empty key is allowed
1468 select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
1469                   jsonb_object                   
1470 -------------------------------------------------
1471  {"": "3", "a": "1", "b": "2", "d e f": "a b c"}
1472 (1 row)
1473
1474 -- extract_path, extract_path_as_text
1475 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1476  jsonb_extract_path 
1477 --------------------
1478  "stringy"
1479 (1 row)
1480
1481 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1482  jsonb_extract_path 
1483 --------------------
1484  {"f3": 1}
1485 (1 row)
1486
1487 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1488  jsonb_extract_path 
1489 --------------------
1490  "f3"
1491 (1 row)
1492
1493 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1494  jsonb_extract_path 
1495 --------------------
1496  1
1497 (1 row)
1498
1499 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1500  jsonb_extract_path_text 
1501 -------------------------
1502  stringy
1503 (1 row)
1504
1505 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1506  jsonb_extract_path_text 
1507 -------------------------
1508  {"f3": 1}
1509 (1 row)
1510
1511 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1512  jsonb_extract_path_text 
1513 -------------------------
1514  f3
1515 (1 row)
1516
1517 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1518  jsonb_extract_path_text 
1519 -------------------------
1520  1
1521 (1 row)
1522
1523 -- extract_path nulls
1524 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
1525  expect_false 
1526 --------------
1527  f
1528 (1 row)
1529
1530 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
1531  expect_true 
1532 -------------
1533  t
1534 (1 row)
1535
1536 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
1537  expect_false 
1538 --------------
1539  f
1540 (1 row)
1541
1542 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
1543  expect_true 
1544 -------------
1545  t
1546 (1 row)
1547
1548 -- extract_path operators
1549 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
1550  ?column?  
1551 -----------
1552  "stringy"
1553 (1 row)
1554
1555 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
1556  ?column?  
1557 -----------
1558  {"f3": 1}
1559 (1 row)
1560
1561 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
1562  ?column? 
1563 ----------
1564  "f3"
1565 (1 row)
1566
1567 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
1568  ?column? 
1569 ----------
1570  1
1571 (1 row)
1572
1573 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
1574  ?column? 
1575 ----------
1576  stringy
1577 (1 row)
1578
1579 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
1580  ?column?  
1581 -----------
1582  {"f3": 1}
1583 (1 row)
1584
1585 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
1586  ?column? 
1587 ----------
1588  f3
1589 (1 row)
1590
1591 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
1592  ?column? 
1593 ----------
1594  1
1595 (1 row)
1596
1597 -- corner cases for same
1598 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
1599           ?column?          
1600 ----------------------------
1601  {"a": {"b": {"c": "foo"}}}
1602 (1 row)
1603
1604 select '[1,2,3]'::jsonb #> '{}';
1605  ?column?  
1606 -----------
1607  [1, 2, 3]
1608 (1 row)
1609
1610 select '"foo"'::jsonb #> '{}';
1611  ?column? 
1612 ----------
1613  "foo"
1614 (1 row)
1615
1616 select '42'::jsonb #> '{}';
1617  ?column? 
1618 ----------
1619  42
1620 (1 row)
1621
1622 select 'null'::jsonb #> '{}';
1623  ?column? 
1624 ----------
1625  null
1626 (1 row)
1627
1628 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
1629       ?column?       
1630 ---------------------
1631  {"b": {"c": "foo"}}
1632 (1 row)
1633
1634 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
1635  ?column? 
1636 ----------
1637  
1638 (1 row)
1639
1640 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
1641  ?column? 
1642 ----------
1643  
1644 (1 row)
1645
1646 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
1647    ?column?   
1648 --------------
1649  {"c": "foo"}
1650 (1 row)
1651
1652 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
1653  ?column? 
1654 ----------
1655  "foo"
1656 (1 row)
1657
1658 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
1659  ?column? 
1660 ----------
1661  
1662 (1 row)
1663
1664 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
1665  ?column? 
1666 ----------
1667  
1668 (1 row)
1669
1670 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
1671  ?column? 
1672 ----------
1673  "cc"
1674 (1 row)
1675
1676 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
1677  ?column? 
1678 ----------
1679  
1680 (1 row)
1681
1682 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
1683  ?column? 
1684 ----------
1685  "cc"
1686 (1 row)
1687
1688 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
1689  ?column? 
1690 ----------
1691  
1692 (1 row)
1693
1694 select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
1695  ?column? 
1696 ----------
1697  null
1698 (1 row)
1699
1700 select '"foo"'::jsonb #> array['z'];
1701  ?column? 
1702 ----------
1703  
1704 (1 row)
1705
1706 select '42'::jsonb #> array['f2'];
1707  ?column? 
1708 ----------
1709  
1710 (1 row)
1711
1712 select '42'::jsonb #> array['0'];
1713  ?column? 
1714 ----------
1715  
1716 (1 row)
1717
1718 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
1719           ?column?          
1720 ----------------------------
1721  {"a": {"b": {"c": "foo"}}}
1722 (1 row)
1723
1724 select '[1,2,3]'::jsonb #>> '{}';
1725  ?column?  
1726 -----------
1727  [1, 2, 3]
1728 (1 row)
1729
1730 select '"foo"'::jsonb #>> '{}';
1731  ?column? 
1732 ----------
1733  foo
1734 (1 row)
1735
1736 select '42'::jsonb #>> '{}';
1737  ?column? 
1738 ----------
1739  42
1740 (1 row)
1741
1742 select 'null'::jsonb #>> '{}';
1743  ?column? 
1744 ----------
1745  
1746 (1 row)
1747
1748 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
1749       ?column?       
1750 ---------------------
1751  {"b": {"c": "foo"}}
1752 (1 row)
1753
1754 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
1755  ?column? 
1756 ----------
1757  
1758 (1 row)
1759
1760 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
1761  ?column? 
1762 ----------
1763  
1764 (1 row)
1765
1766 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
1767    ?column?   
1768 --------------
1769  {"c": "foo"}
1770 (1 row)
1771
1772 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
1773  ?column? 
1774 ----------
1775  foo
1776 (1 row)
1777
1778 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
1779  ?column? 
1780 ----------
1781  
1782 (1 row)
1783
1784 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
1785  ?column? 
1786 ----------
1787  
1788 (1 row)
1789
1790 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
1791  ?column? 
1792 ----------
1793  cc
1794 (1 row)
1795
1796 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
1797  ?column? 
1798 ----------
1799  
1800 (1 row)
1801
1802 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
1803  ?column? 
1804 ----------
1805  cc
1806 (1 row)
1807
1808 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
1809  ?column? 
1810 ----------
1811  
1812 (1 row)
1813
1814 select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
1815  ?column? 
1816 ----------
1817  
1818 (1 row)
1819
1820 select '"foo"'::jsonb #>> array['z'];
1821  ?column? 
1822 ----------
1823  
1824 (1 row)
1825
1826 select '42'::jsonb #>> array['f2'];
1827  ?column? 
1828 ----------
1829  
1830 (1 row)
1831
1832 select '42'::jsonb #>> array['0'];
1833  ?column? 
1834 ----------
1835  
1836 (1 row)
1837
1838 -- array_elements
1839 SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
1840     jsonb_array_elements    
1841 ----------------------------
1842  1
1843  true
1844  [1, [2, 3]]
1845  null
1846  {"f1": 1, "f2": [7, 8, 9]}
1847  false
1848 (6 rows)
1849
1850 SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
1851            value            
1852 ----------------------------
1853  1
1854  true
1855  [1, [2, 3]]
1856  null
1857  {"f1": 1, "f2": [7, 8, 9]}
1858  false
1859 (6 rows)
1860
1861 SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
1862  jsonb_array_elements_text  
1863 ----------------------------
1864  1
1865  true
1866  [1, [2, 3]]
1867  
1868  {"f1": 1, "f2": [7, 8, 9]}
1869  false
1870  stringy
1871 (7 rows)
1872
1873 SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1874            value            
1875 ----------------------------
1876  1
1877  true
1878  [1, [2, 3]]
1879  
1880  {"f1": 1, "f2": [7, 8, 9]}
1881  false
1882  stringy
1883 (7 rows)
1884
1885 -- populate_record
1886 CREATE TYPE jbpop AS (a text, b int, c timestamp);
1887 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
1888    a    | b | c 
1889 --------+---+---
1890  blurfl |   | 
1891 (1 row)
1892
1893 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
1894    a    | b |            c             
1895 --------+---+--------------------------
1896  blurfl | 3 | Mon Dec 31 15:30:56 2012
1897 (1 row)
1898
1899 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
1900    a    | b | c 
1901 --------+---+---
1902  blurfl |   | 
1903 (1 row)
1904
1905 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
1906    a    | b |            c             
1907 --------+---+--------------------------
1908  blurfl | 3 | Mon Dec 31 15:30:56 2012
1909 (1 row)
1910
1911 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
1912          a         | b | c 
1913 -------------------+---+---
1914  [100, 200, false] |   | 
1915 (1 row)
1916
1917 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
1918          a         | b |            c             
1919 -------------------+---+--------------------------
1920  [100, 200, false] | 3 | Mon Dec 31 15:30:56 2012
1921 (1 row)
1922
1923 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}') q;
1924 ERROR:  invalid input syntax for type timestamp: "[100, 200, false]"
1925 -- populate_recordset
1926 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1927    a    | b |            c             
1928 --------+---+--------------------------
1929  blurfl |   | 
1930         | 3 | Fri Jan 20 10:42:53 2012
1931 (2 rows)
1932
1933 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1934    a    | b  |            c             
1935 --------+----+--------------------------
1936  blurfl | 99 | 
1937  def    |  3 | Fri Jan 20 10:42:53 2012
1938 (2 rows)
1939
1940 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1941    a    | b |            c             
1942 --------+---+--------------------------
1943  blurfl |   | 
1944         | 3 | Fri Jan 20 10:42:53 2012
1945 (2 rows)
1946
1947 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1948    a    | b  |            c             
1949 --------+----+--------------------------
1950  blurfl | 99 | 
1951  def    |  3 | Fri Jan 20 10:42:53 2012
1952 (2 rows)
1953
1954 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1955         a        | b  |            c             
1956 -----------------+----+--------------------------
1957  [100, 200, 300] | 99 | 
1958  {"z": true}     |  3 | Fri Jan 20 10:42:53 2012
1959 (2 rows)
1960
1961 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1962 ERROR:  invalid input syntax for type timestamp: "[100, 200, 300]"
1963 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1964    a    | b |            c             
1965 --------+---+--------------------------
1966  blurfl |   | 
1967         | 3 | Fri Jan 20 10:42:53 2012
1968 (2 rows)
1969
1970 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1971    a    | b  |            c             
1972 --------+----+--------------------------
1973  blurfl | 99 | 
1974  def    |  3 | Fri Jan 20 10:42:53 2012
1975 (2 rows)
1976
1977 SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
1978         a        | b  |            c             
1979 -----------------+----+--------------------------
1980  [100, 200, 300] | 99 | 
1981  {"z": true}     |  3 | Fri Jan 20 10:42:53 2012
1982 (2 rows)
1983
1984 -- handling of unicode surrogate pairs
1985 SELECT octet_length((jsonb '{ "a":  "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
1986 ERROR:  unsupported Unicode escape sequence
1987 LINE 1: SELECT octet_length((jsonb '{ "a":  "\ud83d\ude04\ud83d\udc3...
1988                                    ^
1989 DETAIL:  Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8.
1990 CONTEXT:  JSON data, line 1: { "a":...
1991 SELECT jsonb '{ "a":  "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
1992 ERROR:  invalid input syntax for type json
1993 LINE 1: SELECT jsonb '{ "a":  "\ud83d\ud83d" }' -> 'a';
1994                      ^
1995 DETAIL:  Unicode high surrogate must not follow a high surrogate.
1996 CONTEXT:  JSON data, line 1: { "a":...
1997 SELECT jsonb '{ "a":  "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
1998 ERROR:  invalid input syntax for type json
1999 LINE 1: SELECT jsonb '{ "a":  "\ude04\ud83d" }' -> 'a';
2000                      ^
2001 DETAIL:  Unicode low surrogate must follow a high surrogate.
2002 CONTEXT:  JSON data, line 1: { "a":...
2003 SELECT jsonb '{ "a":  "\ud83dX" }' -> 'a'; -- orphan high surrogate
2004 ERROR:  invalid input syntax for type json
2005 LINE 1: SELECT jsonb '{ "a":  "\ud83dX" }' -> 'a';
2006                      ^
2007 DETAIL:  Unicode low surrogate must follow a high surrogate.
2008 CONTEXT:  JSON data, line 1: { "a":...
2009 SELECT jsonb '{ "a":  "\ude04X" }' -> 'a'; -- orphan low surrogate
2010 ERROR:  invalid input syntax for type json
2011 LINE 1: SELECT jsonb '{ "a":  "\ude04X" }' -> 'a';
2012                      ^
2013 DETAIL:  Unicode low surrogate must follow a high surrogate.
2014 CONTEXT:  JSON data, line 1: { "a":...
2015 -- handling of simple unicode escapes
2016 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' as correct_in_utf8;
2017 ERROR:  unsupported Unicode escape sequence
2018 LINE 1: SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' as corr...
2019                      ^
2020 DETAIL:  Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8.
2021 CONTEXT:  JSON data, line 1: { "a":...
2022 SELECT jsonb '{ "a":  "dollar \u0024 character" }' as correct_everywhere;
2023      correct_everywhere      
2024 -----------------------------
2025  {"a": "dollar $ character"}
2026 (1 row)
2027
2028 SELECT jsonb '{ "a":  "dollar \\u0024 character" }' as not_an_escape;
2029            not_an_escape           
2030 -----------------------------------
2031  {"a": "dollar \\u0024 character"}
2032 (1 row)
2033
2034 SELECT jsonb '{ "a":  "null \u0000 escape" }' as fails;
2035 ERROR:  unsupported Unicode escape sequence
2036 LINE 1: SELECT jsonb '{ "a":  "null \u0000 escape" }' as fails;
2037                      ^
2038 DETAIL:  \u0000 cannot be converted to text.
2039 CONTEXT:  JSON data, line 1: { "a":...
2040 SELECT jsonb '{ "a":  "null \\u0000 escape" }' as not_an_escape;
2041         not_an_escape         
2042 ------------------------------
2043  {"a": "null \\u0000 escape"}
2044 (1 row)
2045
2046 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
2047 ERROR:  unsupported Unicode escape sequence
2048 LINE 1: SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a'...
2049                      ^
2050 DETAIL:  Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8.
2051 CONTEXT:  JSON data, line 1: { "a":...
2052 SELECT jsonb '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
2053  correct_everywhere 
2054 --------------------
2055  dollar $ character
2056 (1 row)
2057
2058 SELECT jsonb '{ "a":  "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
2059       not_an_escape      
2060 -------------------------
2061  dollar \u0024 character
2062 (1 row)
2063
2064 SELECT jsonb '{ "a":  "null \u0000 escape" }' ->> 'a' as fails;
2065 ERROR:  unsupported Unicode escape sequence
2066 LINE 1: SELECT jsonb '{ "a":  "null \u0000 escape" }' ->> 'a' as fai...
2067                      ^
2068 DETAIL:  \u0000 cannot be converted to text.
2069 CONTEXT:  JSON data, line 1: { "a":...
2070 SELECT jsonb '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
2071    not_an_escape    
2072 --------------------
2073  null \u0000 escape
2074 (1 row)
2075
2076 -- jsonb_to_record and jsonb_to_recordset
2077 select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
2078     as x(a int, b text, d text);
2079  a |  b  | d 
2080 ---+-----+---
2081  1 | foo | 
2082 (1 row)
2083
2084 select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
2085     as x(a int, b text, c boolean);
2086  a |  b  | c 
2087 ---+-----+---
2088  1 | foo | 
2089  2 | bar | t
2090 (2 rows)
2091
2092 -- indexing
2093 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2094  count 
2095 -------
2096      1
2097 (1 row)
2098
2099 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2100  count 
2101 -------
2102     15
2103 (1 row)
2104
2105 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2106  count 
2107 -------
2108      2
2109 (1 row)
2110
2111 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2112  count 
2113 -------
2114      2
2115 (1 row)
2116
2117 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2118  count 
2119 -------
2120      2
2121 (1 row)
2122
2123 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2124  count 
2125 -------
2126    194
2127 (1 row)
2128
2129 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2130  count 
2131 -------
2132      0
2133 (1 row)
2134
2135 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
2136  count 
2137 -------
2138    337
2139 (1 row)
2140
2141 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
2142  count 
2143 -------
2144     42
2145 (1 row)
2146
2147 CREATE INDEX jidx ON testjsonb USING gin (j);
2148 SET enable_seqscan = off;
2149 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2150  count 
2151 -------
2152      1
2153 (1 row)
2154
2155 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2156  count 
2157 -------
2158     15
2159 (1 row)
2160
2161 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2162  count 
2163 -------
2164      2
2165 (1 row)
2166
2167 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2168  count 
2169 -------
2170      2
2171 (1 row)
2172
2173 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2174  count 
2175 -------
2176      2
2177 (1 row)
2178
2179 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
2180  count 
2181 -------
2182      3
2183 (1 row)
2184
2185 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
2186  count 
2187 -------
2188      3
2189 (1 row)
2190
2191 -- exercise GIN_SEARCH_MODE_ALL
2192 SELECT count(*) FROM testjsonb WHERE j @> '{}';
2193  count 
2194 -------
2195   1012
2196 (1 row)
2197
2198 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2199  count 
2200 -------
2201    194
2202 (1 row)
2203
2204 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2205  count 
2206 -------
2207      0
2208 (1 row)
2209
2210 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
2211  count 
2212 -------
2213    337
2214 (1 row)
2215
2216 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
2217  count 
2218 -------
2219     42
2220 (1 row)
2221
2222 -- array exists - array elements should behave as keys (for GIN index scans too)
2223 CREATE INDEX jidx_array ON testjsonb USING gin((j->'array'));
2224 SELECT count(*) from testjsonb  WHERE j->'array' ? 'bar';
2225  count 
2226 -------
2227      3
2228 (1 row)
2229
2230 -- type sensitive array exists - should return no rows (since "exists" only
2231 -- matches strings that are either object keys or array elements)
2232 SELECT count(*) from testjsonb  WHERE j->'array' ? '5'::text;
2233  count 
2234 -------
2235      0
2236 (1 row)
2237
2238 -- However, a raw scalar is *contained* within the array
2239 SELECT count(*) from testjsonb  WHERE j->'array' @> '5'::jsonb;
2240  count 
2241 -------
2242      1
2243 (1 row)
2244
2245 RESET enable_seqscan;
2246 SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
2247  count 
2248 -------
2249   4791
2250 (1 row)
2251
2252 SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
2253     key    | count 
2254 -----------+-------
2255  line      |   884
2256  query     |   207
2257  pos       |   203
2258  node      |   202
2259  space     |   197
2260  status    |   195
2261  public    |   194
2262  title     |   190
2263  wait      |   190
2264  org       |   189
2265  user      |   189
2266  coauthors |   188
2267  disabled  |   185
2268  indexed   |   184
2269  cleaned   |   180
2270  bad       |   179
2271  date      |   179
2272  world     |   176
2273  state     |   172
2274  subtitle  |   169
2275  auth      |   168
2276  abstract  |   161
2277  array     |     5
2278  age       |     2
2279  foo       |     2
2280  fool      |     1
2281 (26 rows)
2282
2283 -- sort/hash
2284 SELECT count(distinct j) FROM testjsonb;
2285  count 
2286 -------
2287    894
2288 (1 row)
2289
2290 SET enable_hashagg = off;
2291 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
2292  count 
2293 -------
2294    894
2295 (1 row)
2296
2297 SET enable_hashagg = on;
2298 SET enable_sort = off;
2299 SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
2300  count 
2301 -------
2302    894
2303 (1 row)
2304
2305 SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
2306  j  
2307 ----
2308  {}
2309 (1 row)
2310
2311 SET enable_sort = on;
2312 RESET enable_hashagg;
2313 RESET enable_sort;
2314 DROP INDEX jidx;
2315 DROP INDEX jidx_array;
2316 -- btree
2317 CREATE INDEX jidx ON testjsonb USING btree (j);
2318 SET enable_seqscan = off;
2319 SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
2320  count 
2321 -------
2322    884
2323 (1 row)
2324
2325 SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
2326  count 
2327 -------
2328      1
2329 (1 row)
2330
2331 --gin path opclass
2332 DROP INDEX jidx;
2333 CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
2334 SET enable_seqscan = off;
2335 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2336  count 
2337 -------
2338      1
2339 (1 row)
2340
2341 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2342  count 
2343 -------
2344     15
2345 (1 row)
2346
2347 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2348  count 
2349 -------
2350      2
2351 (1 row)
2352
2353 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2354  count 
2355 -------
2356      2
2357 (1 row)
2358
2359 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2360  count 
2361 -------
2362      2
2363 (1 row)
2364
2365 -- exercise GIN_SEARCH_MODE_ALL
2366 SELECT count(*) FROM testjsonb WHERE j @> '{}';
2367  count 
2368 -------
2369   1012
2370 (1 row)
2371
2372 RESET enable_seqscan;
2373 DROP INDEX jidx;
2374 -- nested tests
2375 SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
2376            jsonb            
2377 ----------------------------
2378  {"ff": {"a": 12, "b": 16}}
2379 (1 row)
2380
2381 SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
2382                  jsonb                 
2383 ---------------------------------------
2384  {"ff": {"a": 12, "b": 16}, "qq": 123}
2385 (1 row)
2386
2387 SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
2388                                               jsonb                                               
2389 --------------------------------------------------------------------------------------------------
2390  {"aa": ["a", "aaa"], "qq": {"a": 12, "b": 16, "c": ["c1", "c2"], "d": {"d1": "d3", "d2": "d2"}}}
2391 (1 row)
2392
2393 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
2394                                                 jsonb                                                 
2395 ------------------------------------------------------------------------------------------------------
2396  {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2"], "d": {"d1": "d1", "d2": "d2"}}}
2397 (1 row)
2398
2399 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
2400                                                           jsonb                                                          
2401 -------------------------------------------------------------------------------------------------------------------------
2402  {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2", ["c3"], {"c4": 4}], "d": {"d1": "d1", "d2": "d2"}}}
2403 (1 row)
2404
2405 SELECT '{"ff":["a","aaa"]}'::jsonb;
2406         jsonb         
2407 ----------------------
2408  {"ff": ["a", "aaa"]}
2409 (1 row)
2410
2411 SELECT
2412   '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
2413   '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
2414   ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
2415   ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
2416    '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
2417       ?column?      | ?column? | f | t | ?column? 
2418 --------------------+----------+---+---+----------
2419  {"a": 12, "b": 16} | 123      | f | t | [1, 2]
2420 (1 row)
2421
2422 -- nested containment
2423 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
2424  ?column? 
2425 ----------
2426  t
2427 (1 row)
2428
2429 SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
2430  ?column? 
2431 ----------
2432  t
2433 (1 row)
2434
2435 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
2436  ?column? 
2437 ----------
2438  f
2439 (1 row)
2440
2441 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
2442  ?column? 
2443 ----------
2444  f
2445 (1 row)
2446
2447 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
2448  ?column? 
2449 ----------
2450  t
2451 (1 row)
2452
2453 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
2454  ?column? 
2455 ----------
2456  f
2457 (1 row)
2458
2459 SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
2460  ?column? 
2461 ----------
2462  f
2463 (1 row)
2464
2465 SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
2466  ?column? 
2467 ----------
2468  t
2469 (1 row)
2470
2471 SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
2472  ?column? 
2473 ----------
2474  t
2475 (1 row)
2476
2477 SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
2478  ?column? 
2479 ----------
2480  t
2481 (1 row)
2482
2483 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
2484  ?column? 
2485 ----------
2486  t
2487 (1 row)
2488
2489 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
2490  ?column? 
2491 ----------
2492  t
2493 (1 row)
2494
2495 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
2496  ?column? 
2497 ----------
2498  f
2499 (1 row)
2500
2501 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
2502  ?column? 
2503 ----------
2504  t
2505 (1 row)
2506
2507 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
2508  ?column? 
2509 ----------
2510  t
2511 (1 row)
2512
2513 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
2514  ?column? 
2515 ----------
2516  f
2517 (1 row)
2518
2519 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
2520  ?column? 
2521 ----------
2522  t
2523 (1 row)
2524
2525 -- nested object field / array index lookup
2526 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
2527  ?column? 
2528 ----------
2529  null
2530 (1 row)
2531
2532 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
2533  ?column? 
2534 ----------
2535  1
2536 (1 row)
2537
2538 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
2539  ?column? 
2540 ----------
2541  [1, 2]
2542 (1 row)
2543
2544 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
2545  ?column? 
2546 ----------
2547  {"1": 2}
2548 (1 row)
2549
2550 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
2551    ?column?    
2552 ---------------
2553  {"1": [2, 3]}
2554 (1 row)
2555
2556 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
2557  ?column? 
2558 ----------
2559  [2, 3]
2560 (1 row)
2561
2562 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
2563  ?column? 
2564 ----------
2565  
2566 (1 row)
2567
2568 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
2569  ?column? 
2570 ----------
2571  
2572 (1 row)
2573
2574 SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
2575  ?column? 
2576 ----------
2577  "a"
2578 (1 row)
2579
2580 SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
2581  ?column? 
2582 ----------
2583  "b"
2584 (1 row)
2585
2586 SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
2587  ?column? 
2588 ----------
2589  "c"
2590 (1 row)
2591
2592 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
2593  ?column? 
2594 ----------
2595  [1, 2]
2596 (1 row)
2597
2598 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
2599  ?column? 
2600 ----------
2601  2
2602 (1 row)
2603
2604 SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
2605  ?column? 
2606 ----------
2607  null
2608 (1 row)
2609
2610 SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
2611  ?column? 
2612 ----------
2613  
2614 (1 row)
2615
2616 SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
2617  ?column? 
2618 ----------
2619  null
2620 (1 row)
2621
2622 SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
2623  ?column? 
2624 ----------
2625  "a"
2626 (1 row)
2627
2628 SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
2629  ?column? 
2630 ----------
2631  
2632 (1 row)
2633
2634 --nested path extraction
2635 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
2636  ?column? 
2637 ----------
2638  
2639 (1 row)
2640
2641 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
2642  ?column? 
2643 ----------
2644  "b"
2645 (1 row)
2646
2647 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
2648  ?column?  
2649 -----------
2650  [1, 2, 3]
2651 (1 row)
2652
2653 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
2654  ?column? 
2655 ----------
2656  1
2657 (1 row)
2658
2659 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
2660  ?column? 
2661 ----------
2662  2
2663 (1 row)
2664
2665 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
2666  ?column? 
2667 ----------
2668  3
2669 (1 row)
2670
2671 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
2672  ?column? 
2673 ----------
2674  
2675 (1 row)
2676
2677 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
2678  ?column? 
2679 ----------
2680  3
2681 (1 row)
2682
2683 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-3}';
2684  ?column? 
2685 ----------
2686  1
2687 (1 row)
2688
2689 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-4}';
2690  ?column? 
2691 ----------
2692  
2693 (1 row)
2694
2695 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
2696  ?column? 
2697 ----------
2698  0
2699 (1 row)
2700
2701 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
2702  ?column? 
2703 ----------
2704  [3, 4]
2705 (1 row)
2706
2707 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
2708    ?column?    
2709 ---------------
2710  {"5": "five"}
2711 (1 row)
2712
2713 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
2714  ?column? 
2715 ----------
2716  "five"
2717 (1 row)
2718
2719 --nested exists
2720 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
2721  ?column? 
2722 ----------
2723  t
2724 (1 row)
2725
2726 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
2727  ?column? 
2728 ----------
2729  t
2730 (1 row)
2731
2732 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
2733  ?column? 
2734 ----------
2735  t
2736 (1 row)
2737
2738 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
2739  ?column? 
2740 ----------
2741  t
2742 (1 row)
2743
2744 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
2745  ?column? 
2746 ----------
2747  t
2748 (1 row)
2749
2750 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
2751  ?column? 
2752 ----------
2753  f
2754 (1 row)
2755
2756 -- jsonb_strip_nulls
2757 select jsonb_strip_nulls(null);
2758  jsonb_strip_nulls 
2759 -------------------
2760  
2761 (1 row)
2762
2763 select jsonb_strip_nulls('1');
2764  jsonb_strip_nulls 
2765 -------------------
2766  1
2767 (1 row)
2768
2769 select jsonb_strip_nulls('"a string"');
2770  jsonb_strip_nulls 
2771 -------------------
2772  "a string"
2773 (1 row)
2774
2775 select jsonb_strip_nulls('null');
2776  jsonb_strip_nulls 
2777 -------------------
2778  null
2779 (1 row)
2780
2781 select jsonb_strip_nulls('[1,2,null,3,4]');
2782  jsonb_strip_nulls  
2783 --------------------
2784  [1, 2, null, 3, 4]
2785 (1 row)
2786
2787 select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
2788              jsonb_strip_nulls              
2789 --------------------------------------------
2790  {"a": 1, "c": [2, null, 3], "d": {"e": 4}}
2791 (1 row)
2792
2793 select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
2794     jsonb_strip_nulls     
2795 --------------------------
2796  [1, {"a": 1, "c": 2}, 3]
2797 (1 row)
2798
2799 -- an empty object is not null and should not be stripped
2800 select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
2801  jsonb_strip_nulls  
2802 --------------------
2803  {"a": {}, "d": {}}
2804 (1 row)
2805
2806 select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
2807         jsonb_pretty        
2808 ----------------------------
2809  {                         +
2810      "a": "test",          +
2811      "b": [                +
2812          1,                +
2813          2,                +
2814          3                 +
2815      ],                    +
2816      "c": "test3",         +
2817      "d": {                +
2818          "dd": "test4",    +
2819          "dd2": {          +
2820              "ddd": "test5"+
2821          }                 +
2822      }                     +
2823  }
2824 (1 row)
2825
2826 select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
2827        jsonb_pretty        
2828 ---------------------------
2829  [                        +
2830      {                    +
2831          "f1": 1,         +
2832          "f2": null       +
2833      },                   +
2834      2,                   +
2835      null,                +
2836      [                    +
2837          [                +
2838              {            +
2839                  "x": true+
2840              },           +
2841              6,           +
2842              7            +
2843          ],               +
2844          8                +
2845      ],                   +
2846      3                    +
2847  ]
2848 (1 row)
2849
2850 select jsonb_pretty('{"a":["b", "c"], "d": {"e":"f"}}');
2851    jsonb_pretty   
2852 ------------------
2853  {               +
2854      "a": [      +
2855          "b",    +
2856          "c"     +
2857      ],          +
2858      "d": {      +
2859          "e": "f"+
2860      }           +
2861  }
2862 (1 row)
2863
2864 select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
2865                            jsonb_concat                            
2866 -------------------------------------------------------------------
2867  {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"}
2868 (1 row)
2869
2870 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
2871                   ?column?                   
2872 ---------------------------------------------
2873  {"b": "g", "aa": 1, "cq": "l", "fg": false}
2874 (1 row)
2875
2876 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
2877                ?column?                
2878 ---------------------------------------
2879  {"b": 2, "aa": 1, "aq": "l", "cq": 3}
2880 (1 row)
2881
2882 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
2883            ?column?           
2884 ------------------------------
2885  {"b": 2, "aa": "l", "cq": 3}
2886 (1 row)
2887
2888 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
2889           ?column?          
2890 ----------------------------
2891  {"b": 2, "aa": 1, "cq": 3}
2892 (1 row)
2893
2894 select '["a", "b"]'::jsonb || '["c"]';
2895     ?column?     
2896 -----------------
2897  ["a", "b", "c"]
2898 (1 row)
2899
2900 select '["a", "b"]'::jsonb || '["c", "d"]';
2901        ?column?       
2902 ----------------------
2903  ["a", "b", "c", "d"]
2904 (1 row)
2905
2906 select '["c"]' || '["a", "b"]'::jsonb;
2907     ?column?     
2908 -----------------
2909  ["c", "a", "b"]
2910 (1 row)
2911
2912 select '["a", "b"]'::jsonb || '"c"';
2913     ?column?     
2914 -----------------
2915  ["a", "b", "c"]
2916 (1 row)
2917
2918 select '"c"' || '["a", "b"]'::jsonb;
2919     ?column?     
2920 -----------------
2921  ["c", "a", "b"]
2922 (1 row)
2923
2924 select '[]'::jsonb || '["a"]'::jsonb;
2925  ?column? 
2926 ----------
2927  ["a"]
2928 (1 row)
2929
2930 select '[]'::jsonb || '"a"'::jsonb;
2931  ?column? 
2932 ----------
2933  ["a"]
2934 (1 row)
2935
2936 select '"b"'::jsonb || '"a"'::jsonb;
2937   ?column?  
2938 ------------
2939  ["b", "a"]
2940 (1 row)
2941
2942 select '{}'::jsonb || '{"a":"b"}'::jsonb;
2943   ?column?  
2944 ------------
2945  {"a": "b"}
2946 (1 row)
2947
2948 select '[]'::jsonb || '{"a":"b"}'::jsonb;
2949    ?column?   
2950 --------------
2951  [{"a": "b"}]
2952 (1 row)
2953
2954 select '{"a":"b"}'::jsonb || '[]'::jsonb;
2955    ?column?   
2956 --------------
2957  [{"a": "b"}]
2958 (1 row)
2959
2960 select '"a"'::jsonb || '{"a":1}';
2961 ERROR:  invalid concatenation of jsonb objects
2962 select '{"a":1}' || '"a"'::jsonb;
2963 ERROR:  invalid concatenation of jsonb objects
2964 select '["a", "b"]'::jsonb || '{"c":1}';
2965        ?column?       
2966 ----------------------
2967  ["a", "b", {"c": 1}]
2968 (1 row)
2969
2970 select '{"c": 1}'::jsonb || '["a", "b"]';
2971        ?column?       
2972 ----------------------
2973  [{"c": 1}, "a", "b"]
2974 (1 row)
2975
2976 select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
2977               ?column?              
2978 ------------------------------------
2979  {"b": "g", "cq": "l", "fg": false}
2980 (1 row)
2981
2982 select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
2983  ?column? 
2984 ----------
2985  t
2986 (1 row)
2987
2988 select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
2989  ?column? 
2990 ----------
2991  t
2992 (1 row)
2993
2994 select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
2995  ?column? 
2996 ----------
2997  t
2998 (1 row)
2999
3000 select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
3001  ?column? 
3002 ----------
3003  t
3004 (1 row)
3005
3006 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
3007    jsonb_delete   
3008 ------------------
3009  {"b": 2, "c": 3}
3010 (1 row)
3011
3012 select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
3013    jsonb_delete   
3014 ------------------
3015  {"b": 2, "c": 3}
3016 (1 row)
3017
3018 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
3019    jsonb_delete   
3020 ------------------
3021  {"a": 1, "c": 3}
3022 (1 row)
3023
3024 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
3025    jsonb_delete   
3026 ------------------
3027  {"a": 1, "b": 2}
3028 (1 row)
3029
3030 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
3031        jsonb_delete       
3032 --------------------------
3033  {"a": 1, "b": 2, "c": 3}
3034 (1 row)
3035
3036 select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
3037      ?column?     
3038 ------------------
3039  {"b": 2, "c": 3}
3040 (1 row)
3041
3042 select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
3043      ?column?     
3044 ------------------
3045  {"b": 2, "c": 3}
3046 (1 row)
3047
3048 select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
3049      ?column?     
3050 ------------------
3051  {"a": 1, "c": 3}
3052 (1 row)
3053
3054 select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
3055      ?column?     
3056 ------------------
3057  {"a": 1, "b": 2}
3058 (1 row)
3059
3060 select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
3061          ?column?         
3062 --------------------------
3063  {"a": 1, "b": 2, "c": 3}
3064 (1 row)
3065
3066 select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
3067  ?column? 
3068 ----------
3069  t
3070 (1 row)
3071
3072 select '["a","b","c"]'::jsonb - 3;
3073     ?column?     
3074 -----------------
3075  ["a", "b", "c"]
3076 (1 row)
3077
3078 select '["a","b","c"]'::jsonb - 2;
3079   ?column?  
3080 ------------
3081  ["a", "b"]
3082 (1 row)
3083
3084 select '["a","b","c"]'::jsonb - 1;
3085   ?column?  
3086 ------------
3087  ["a", "c"]
3088 (1 row)
3089
3090 select '["a","b","c"]'::jsonb - 0;
3091   ?column?  
3092 ------------
3093  ["b", "c"]
3094 (1 row)
3095
3096 select '["a","b","c"]'::jsonb - -1;
3097   ?column?  
3098 ------------
3099  ["a", "b"]
3100 (1 row)
3101
3102 select '["a","b","c"]'::jsonb - -2;
3103   ?column?  
3104 ------------
3105  ["a", "c"]
3106 (1 row)
3107
3108 select '["a","b","c"]'::jsonb - -3;
3109   ?column?  
3110 ------------
3111  ["b", "c"]
3112 (1 row)
3113
3114 select '["a","b","c"]'::jsonb - -4;
3115     ?column?     
3116 -----------------
3117  ["a", "b", "c"]
3118 (1 row)
3119
3120 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
3121                                 jsonb_set                                 
3122 --------------------------------------------------------------------------
3123  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": [1, 2, 3]}
3124 (1 row)
3125
3126 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
3127                                   jsonb_set                                  
3128 -----------------------------------------------------------------------------
3129  {"a": 1, "b": [1, [1, 2, 3]], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3130 (1 row)
3131
3132 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
3133                                   jsonb_set                                  
3134 -----------------------------------------------------------------------------
3135  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [[1, 2, 3], 3]}, "n": null}
3136 (1 row)
3137
3138 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
3139 ERROR:  path element at the position 2 is NULL
3140 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
3141                                 jsonb_set                                
3142 -------------------------------------------------------------------------
3143  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": {"1": 2}}
3144 (1 row)
3145
3146 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
3147                                  jsonb_set                                  
3148 ----------------------------------------------------------------------------
3149  {"a": 1, "b": [1, {"1": 2}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3150 (1 row)
3151
3152 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
3153                                  jsonb_set                                  
3154 ----------------------------------------------------------------------------
3155  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [{"1": 2}, 3]}, "n": null}
3156 (1 row)
3157
3158 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
3159 ERROR:  path element at the position 2 is NULL
3160 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
3161                                 jsonb_set                                 
3162 --------------------------------------------------------------------------
3163  {"a": 1, "b": [1, "test"], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3164 (1 row)
3165
3166 select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
3167                                     jsonb_set                                    
3168 ---------------------------------------------------------------------------------
3169  {"a": 1, "b": [1, {"f": "test"}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3170 (1 row)
3171
3172 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
3173                     jsonb_delete_path                     
3174 ----------------------------------------------------------
3175  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
3176 (1 row)
3177
3178 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
3179                         jsonb_delete_path                         
3180 ------------------------------------------------------------------
3181  {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3182 (1 row)
3183
3184 select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
3185                         jsonb_delete_path                         
3186 ------------------------------------------------------------------
3187  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
3188 (1 row)
3189
3190 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
3191                          ?column?                         
3192 ----------------------------------------------------------
3193  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
3194 (1 row)
3195
3196 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
3197                              ?column?                             
3198 ------------------------------------------------------------------
3199  {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3200 (1 row)
3201
3202 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
3203 ERROR:  path element at the position 2 is not an integer
3204 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
3205                              ?column?                             
3206 ------------------------------------------------------------------
3207  {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
3208 (1 row)
3209
3210 -- empty structure and error conditions for delete and replace
3211 select '"a"'::jsonb - 'a'; -- error
3212 ERROR:  cannot delete from scalar
3213 select '{}'::jsonb - 'a';
3214  ?column? 
3215 ----------
3216  {}
3217 (1 row)
3218
3219 select '[]'::jsonb - 'a';
3220  ?column? 
3221 ----------
3222  []
3223 (1 row)
3224
3225 select '"a"'::jsonb - 1; -- error
3226 ERROR:  cannot delete from scalar
3227 select '{}'::jsonb -  1; -- error
3228 ERROR:  cannot delete from object using integer subscript
3229 select '[]'::jsonb - 1;
3230  ?column? 
3231 ----------
3232  []
3233 (1 row)
3234
3235 select '"a"'::jsonb #- '{a}'; -- error
3236 ERROR:  cannot delete path in scalar
3237 select '{}'::jsonb #- '{a}';
3238  ?column? 
3239 ----------
3240  {}
3241 (1 row)
3242
3243 select '[]'::jsonb #- '{a}';
3244  ?column? 
3245 ----------
3246  []
3247 (1 row)
3248
3249 select jsonb_set('"a"','{a}','"b"'); --error
3250 ERROR:  cannot set path in scalar
3251 select jsonb_set('{}','{a}','"b"', false);
3252  jsonb_set 
3253 -----------
3254  {}
3255 (1 row)
3256
3257 select jsonb_set('[]','{1}','"b"', false);
3258  jsonb_set 
3259 -----------
3260  []
3261 (1 row)
3262
3263 -- jsonb_set adding instead of replacing
3264 -- prepend to array
3265 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}');
3266                        jsonb_set                       
3267 -------------------------------------------------------
3268  {"a": 1, "b": [{"foo": 123}, 0, 1, 2], "c": {"d": 4}}
3269 (1 row)
3270
3271 -- append to array
3272 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
3273                        jsonb_set                       
3274 -------------------------------------------------------
3275  {"a": 1, "b": [0, 1, 2, {"foo": 123}], "c": {"d": 4}}
3276 (1 row)
3277
3278 -- check nesting levels addition
3279 select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}');
3280                               jsonb_set                              
3281 ---------------------------------------------------------------------
3282  {"a": 1, "b": [4, 5, [0, 1, 2, {"foo": 123}], 6, 7], "c": {"d": 4}}
3283 (1 row)
3284
3285 -- add new key
3286 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
3287                          jsonb_set                          
3288 ------------------------------------------------------------
3289  {"a": 1, "b": [0, 1, 2], "c": {"d": 4, "e": {"foo": 123}}}
3290 (1 row)
3291
3292 -- adding doesn't do anything if elements before last aren't present
3293 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}');
3294                 jsonb_set                
3295 -----------------------------------------
3296  {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
3297 (1 row)
3298
3299 select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
3300                 jsonb_set                
3301 -----------------------------------------
3302  {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
3303 (1 row)
3304
3305 -- add to empty object
3306 select jsonb_set('{}','{x}','{"foo":123}');
3307       jsonb_set      
3308 ---------------------
3309  {"x": {"foo": 123}}
3310 (1 row)
3311
3312 --add to empty array
3313 select jsonb_set('[]','{0}','{"foo":123}');
3314    jsonb_set    
3315 ----------------
3316  [{"foo": 123}]
3317 (1 row)
3318
3319 select jsonb_set('[]','{99}','{"foo":123}');
3320    jsonb_set    
3321 ----------------
3322  [{"foo": 123}]
3323 (1 row)
3324
3325 select jsonb_set('[]','{-99}','{"foo":123}');
3326    jsonb_set    
3327 ----------------
3328  [{"foo": 123}]
3329 (1 row)
3330
3331 select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
3332 ERROR:  path element at the position 2 is not an integer
3333 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
3334 ERROR:  path element at the position 3 is not an integer
3335 select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');
3336 ERROR:  path element at the position 3 is NULL