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