2 SELECT '""'::jsonb; -- OK.
8 SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed
9 ERROR: invalid input syntax for type json
10 LINE 1: SELECT $$''$$::jsonb;
12 DETAIL: Token "'" is invalid.
13 CONTEXT: JSON data, line 1: '...
14 SELECT '"abc"'::jsonb; -- OK
20 SELECT '"abc'::jsonb; -- ERROR, quotes not closed
21 ERROR: invalid input syntax for type json
22 LINE 1: SELECT '"abc'::jsonb;
24 DETAIL: Token ""abc" is invalid.
25 CONTEXT: JSON data, line 1: "abc
27 def"'::jsonb; -- ERROR, unescaped newline in string constant
28 ERROR: invalid input syntax for type json
31 DETAIL: Character with value 0x0a must be escaped.
32 CONTEXT: JSON data, line 1: "abc
33 SELECT '"\n\"\\"'::jsonb; -- OK, legal escapes
39 SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape
40 ERROR: invalid input syntax for type json
41 LINE 1: SELECT '"\v"'::jsonb;
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;
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;
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;
61 DETAIL: "\u" must be followed by four hexadecimal digits.
62 CONTEXT: JSON data, line 1: "\u000g...
63 SELECT '"\u0045"'::jsonb; -- OK, legal escape
69 SELECT '"\u0000"'::jsonb; -- ERROR, we don't support U+0000
70 ERROR: unsupported Unicode escape sequence
71 LINE 1: SELECT '"\u0000"'::jsonb;
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
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);
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: ...
84 SELECT '1'::jsonb; -- OK
90 SELECT '0'::jsonb; -- OK
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;
100 DETAIL: Token "01" is invalid.
101 CONTEXT: JSON data, line 1: 01
102 SELECT '0.1'::jsonb; -- OK
108 SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8
110 ---------------------
114 SELECT '1e100'::jsonb; -- OK
116 -------------------------------------------------------------------------------------------------------
117 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
120 SELECT '1.3e100'::jsonb; -- OK
122 -------------------------------------------------------------------------------------------------------
123 13000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
126 SELECT '1f2'::jsonb; -- ERROR
127 ERROR: invalid input syntax for type json
128 LINE 1: SELECT '1f2'::jsonb;
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;
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;
142 DETAIL: Token "1.3ex100" is invalid.
143 CONTEXT: JSON data, line 1: 1.3ex100
145 SELECT '[]'::jsonb; -- OK
151 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb; -- OK
153 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
154 [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
157 SELECT '[1,2]'::jsonb; -- OK
163 SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
164 ERROR: invalid input syntax for type json
165 LINE 1: SELECT '[1,2,]'::jsonb;
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;
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;
179 DETAIL: The input string ended unexpectedly.
180 CONTEXT: JSON data, line 1: [1,[2]
182 SELECT '{}'::jsonb; -- OK
188 SELECT '{"abc"}'::jsonb; -- ERROR, no value
189 ERROR: invalid input syntax for type json
190 LINE 1: SELECT '{"abc"}'::jsonb;
192 DETAIL: Expected ":", but found "}".
193 CONTEXT: JSON data, line 1: {"abc"}
194 SELECT '{"abc":1}'::jsonb; -- OK
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;
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;
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;
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;
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
226 --------------------------------------------------------------------
227 {"abc": 1, "def": 2, "ghi": [3, 4], "hij": {"klm": 5, "nop": [6]}}
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;
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;
240 DETAIL: Expected string, but found "3".
241 CONTEXT: JSON data, line 1: {"abc":1,3...
242 -- Miscellaneous stuff.
243 SELECT 'true'::jsonb; -- OK
249 SELECT 'false'::jsonb; -- OK
255 SELECT 'null'::jsonb; -- OK
261 SELECT ' true '::jsonb; -- OK, even with extra whitespace
267 SELECT 'true false'::jsonb; -- ERROR, too many values
268 ERROR: invalid input syntax for type json
269 LINE 1: SELECT 'true false'::jsonb;
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;
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;
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;
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;
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;
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]}']);
306 --------------------------
307 [{"a": 1},{"b": [2, 3]}]
310 -- to_jsonb, timestamps
311 select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
313 ------------------------------
314 "2014-05-28T12:22:35.614298"
318 SET LOCAL TIME ZONE 10.5;
319 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
321 ------------------------------------
322 "2014-05-29T02:52:35.614298+10:30"
325 SET LOCAL TIME ZONE -8;
326 select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
328 ------------------------------------
329 "2014-05-28T08:22:35.614298-08:00"
333 select to_jsonb(date '2014-05-28');
339 select to_jsonb(date 'Infinity');
345 select to_jsonb(timestamp 'Infinity');
351 select to_jsonb(timestamptz 'Infinity');
358 CREATE TEMP TABLE rows AS
359 SELECT x, 'txt' || x as y
360 FROM generate_series(1,3) AS x;
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;
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]}]}]
375 -----------------------------------------------------------------------
376 [{"x": 1, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
379 -- jsonb extraction functions
380 CREATE TEMP TABLE test_jsonb (
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';
394 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
400 SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
406 SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
412 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
418 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
424 SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
430 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
436 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
442 SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
448 SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
454 SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
460 SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
466 SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
472 SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
478 SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
484 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
490 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
496 SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
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';
518 SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
524 SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
530 SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
536 SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
543 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
549 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
555 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
561 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
567 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
573 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
579 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
585 select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
591 select '{"a": "c", "b": null}'::jsonb -> 'b';
597 select '"foo"'::jsonb -> 1;
603 select '"foo"'::jsonb -> 'z';
609 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
615 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
621 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
627 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
633 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
639 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
645 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
651 select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
657 select '{"a": "c", "b": null}'::jsonb ->> 'b';
663 select '"foo"'::jsonb ->> 1;
669 select '"foo"'::jsonb ->> 'z';
675 -- equality and inequality
676 SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
682 SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
688 SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
694 SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
701 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
707 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
713 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
719 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
725 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
731 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
737 SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
743 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
749 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
755 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
761 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
767 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
773 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
779 SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
785 SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
791 SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
797 SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
803 SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
809 SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
815 SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
821 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
827 SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
833 SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
839 SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
845 SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
851 SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
857 SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
863 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
869 SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
875 SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
881 SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
887 SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
893 SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
899 SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
905 -- Raw scalar may contain another raw scalar, array may contain a raw scalar
906 SELECT '[5]'::jsonb @> '[5]';
912 SELECT '5'::jsonb @> '5';
918 SELECT '[5]'::jsonb @> '5';
924 -- But a raw scalar cannot contain an array
925 SELECT '5'::jsonb @> '[5]';
931 -- In general, one thing should always contain itself. Test array containment:
932 SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
938 SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
944 -- array containment string matching confusion bug
945 SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
952 SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
958 SELECT jsonb_array_length('[]');
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
969 SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
977 SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
979 ------------------------------------------------------
981 (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
987 SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
997 SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
999 -----+------------------------------------
1001 a | {"1": "first", "b": "c", "c": "b"}
1007 SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
1009 --------------------
1016 SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
1018 ------------------------------------------------------
1020 (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
1026 SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
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;
1038 -----+------------------------------------
1040 a | {"1": "first", "b": "c", "c": "b"}
1047 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
1053 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
1059 SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
1065 SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
1071 SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
1077 SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
1083 SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
1089 SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
1095 -- array exists - array elements should behave as keys
1096 SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
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;
1110 -- However, a raw scalar is *contained* within the array
1111 SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
1117 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
1123 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
1129 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
1135 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
1141 SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
1147 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
1153 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
1159 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
1165 SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
1171 SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
1177 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
1183 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
1189 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
1195 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
1201 SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
1207 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
1213 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
1219 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
1225 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
1231 SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
1237 SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
1244 SELECT jsonb_typeof('{}') AS object;
1250 SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
1256 SELECT jsonb_typeof('[]') AS array;
1262 SELECT jsonb_typeof('["a", 1]') AS array;
1268 SELECT jsonb_typeof('null') AS "null";
1274 SELECT jsonb_typeof('1') AS number;
1280 SELECT jsonb_typeof('-1') AS number;
1286 SELECT jsonb_typeof('1.0') AS number;
1292 SELECT jsonb_typeof('1e2') AS number;
1298 SELECT jsonb_typeof('-1.0') AS number;
1304 SELECT jsonb_typeof('true') AS boolean;
1310 SELECT jsonb_typeof('false') AS boolean;
1316 SELECT jsonb_typeof('"hello"') AS string;
1322 SELECT jsonb_typeof('"true"') AS string;
1328 SELECT jsonb_typeof('"1.0"') AS string;
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]}');
1337 -------------------------------------------------------------------------
1338 ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1, 2, 3]}]
1341 SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
1343 -------------------------------------------------------------------------
1344 {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1, 2, 3]}}
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)));
1352 ------------------------------------------------------------------------------------------------
1353 {"a": {"b": false, "c": 99}, "d": {"e": [9, 8, 7], "f": {"name": "pg_class", "relkind": "r"}}}
1356 -- empty objects/arrays
1357 SELECT jsonb_build_array();
1363 SELECT jsonb_build_object();
1365 --------------------
1369 -- make sure keys are quoted
1370 SELECT jsonb_build_object(1,2);
1372 --------------------
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)))
1392 -------------------------------------------------------------------------------------------------------------------------------------------------------------
1393 {"turbines": {"847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"}}}
1398 SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
1400 ---------------------------------------------------
1401 {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1404 -- same but with two dimensions
1405 SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
1407 ---------------------------------------------------
1408 {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
1412 SELECT jsonb_object('{a,b,c}');
1413 ERROR: array must have even number of elements
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"}');
1426 --------------------------------------------------
1427 {"a": "1", "b": "2", "c": "3", "d e f": "a b c"}
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
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"}');
1444 -------------------------------------------------
1445 {"": "3", "a": "1", "b": "2", "d e f": "a b c"}
1448 -- extract_path, extract_path_as_text
1449 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1451 --------------------
1455 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1457 --------------------
1461 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1463 --------------------
1467 SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1469 --------------------
1473 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
1474 jsonb_extract_path_text
1475 -------------------------
1479 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
1480 jsonb_extract_path_text
1481 -------------------------
1485 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
1486 jsonb_extract_path_text
1487 -------------------------
1491 SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
1492 jsonb_extract_path_text
1493 -------------------------
1497 -- extract_path nulls
1498 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
1504 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
1510 SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
1516 SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
1522 -- extract_path operators
1523 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
1529 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
1535 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
1541 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
1547 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
1553 SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
1559 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
1565 SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
1571 -- corner cases for same
1572 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
1574 ----------------------------
1575 {"a": {"b": {"c": "foo"}}}
1578 select '[1,2,3]'::jsonb #> '{}';
1584 select '"foo"'::jsonb #> '{}';
1590 select '42'::jsonb #> '{}';
1596 select 'null'::jsonb #> '{}';
1602 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
1604 ---------------------
1608 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
1614 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
1620 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
1626 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
1632 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
1638 select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
1644 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
1650 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
1656 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
1662 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
1668 select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
1674 select '"foo"'::jsonb #> array['z'];
1680 select '42'::jsonb #> array['f2'];
1686 select '42'::jsonb #> array['0'];
1692 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
1694 ----------------------------
1695 {"a": {"b": {"c": "foo"}}}
1698 select '[1,2,3]'::jsonb #>> '{}';
1704 select '"foo"'::jsonb #>> '{}';
1710 select '42'::jsonb #>> '{}';
1716 select 'null'::jsonb #>> '{}';
1722 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
1724 ---------------------
1728 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
1734 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
1740 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
1746 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
1752 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
1758 select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
1764 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
1770 select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
1776 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
1782 select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
1788 select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
1794 select '"foo"'::jsonb #>> array['z'];
1800 select '42'::jsonb #>> array['f2'];
1806 select '42'::jsonb #>> array['0'];
1813 SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
1814 jsonb_array_elements
1815 ----------------------------
1820 {"f1": 1, "f2": [7, 8, 9]}
1824 SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
1826 ----------------------------
1831 {"f1": 1, "f2": [7, 8, 9]}
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 ----------------------------
1842 {"f1": 1, "f2": [7, 8, 9]}
1847 SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
1849 ----------------------------
1854 {"f1": 1, "f2": [7, 8, 9]}
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;
1867 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
1869 --------+---+--------------------------
1870 blurfl | 3 | Mon Dec 31 15:30:56 2012
1873 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
1879 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
1881 --------+---+--------------------------
1882 blurfl | 3 | Mon Dec 31 15:30:56 2012
1885 SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
1887 -------------------+---+---
1888 [100, 200, false] | |
1891 SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
1893 -------------------+---+--------------------------
1894 [100, 200, false] | 3 | Mon Dec 31 15:30:56 2012
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;
1902 --------+---+--------------------------
1904 | 3 | Fri Jan 20 10:42:53 2012
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;
1909 --------+----+--------------------------
1911 def | 3 | Fri Jan 20 10:42:53 2012
1914 SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
1916 --------+---+--------------------------
1918 | 3 | Fri Jan 20 10:42:53 2012
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;
1923 --------+----+--------------------------
1925 def | 3 | Fri Jan 20 10:42:53 2012
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;
1930 -----------------+----+--------------------------
1931 [100, 200, 300] | 99 |
1932 {"z": true} | 3 | Fri Jan 20 10:42:53 2012
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;
1939 --------+---+--------------------------
1941 | 3 | Fri Jan 20 10:42:53 2012
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;
1946 --------+----+--------------------------
1948 def | 3 | Fri Jan 20 10:42:53 2012
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;
1953 -----------------+----+--------------------------
1954 [100, 200, 300] | 99 |
1955 {"z": true} | 3 | Fri Jan 20 10:42:53 2012
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...
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';
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';
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';
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';
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...
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;
1998 -----------------------------
1999 {"a": "dollar $ character"}
2002 SELECT jsonb '{ "a": "dollar \\u0024 character" }' as not_an_escape;
2004 -----------------------------------
2005 {"a": "dollar \\u0024 character"}
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;
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;
2016 ------------------------------
2017 {"a": "null \\u0000 escape"}
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'...
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;
2028 --------------------
2032 SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
2034 -------------------------
2035 dollar \u0024 character
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...
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;
2046 --------------------
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);
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);
2067 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2073 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2079 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2085 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2091 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2097 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2103 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2109 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
2115 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
2121 CREATE INDEX jidx ON testjsonb USING gin (j);
2122 SET enable_seqscan = off;
2123 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
2129 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2135 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2141 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2147 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2153 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
2159 SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
2165 -- excercise GIN_SEARCH_MODE_ALL
2166 SELECT count(*) FROM testjsonb WHERE j @> '{}';
2172 SELECT count(*) FROM testjsonb WHERE j ? 'public';
2178 SELECT count(*) FROM testjsonb WHERE j ? 'bar';
2184 SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
2190 SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
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';
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;
2212 -- However, a raw scalar is *contained* within the array
2213 SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
2219 RESET enable_seqscan;
2220 SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
2226 SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
2258 SELECT count(distinct j) FROM testjsonb;
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;
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;
2279 SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
2285 SET enable_sort = on;
2286 RESET enable_hashagg;
2289 DROP INDEX jidx_array;
2291 CREATE INDEX jidx ON testjsonb USING btree (j);
2292 SET enable_seqscan = off;
2293 SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
2299 SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
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}';
2315 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
2321 SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
2327 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
2333 SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
2339 -- excercise GIN_SEARCH_MODE_ALL
2340 SELECT count(*) FROM testjsonb WHERE j @> '{}';
2346 RESET enable_seqscan;
2349 SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
2351 ----------------------------
2352 {"ff": {"a": 12, "b": 16}}
2355 SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
2357 ---------------------------------------
2358 {"ff": {"a": 12, "b": 16}, "qq": 123}
2361 SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
2363 --------------------------------------------------------------------------------------------------
2364 {"aa": ["a", "aaa"], "qq": {"a": 12, "b": 16, "c": ["c1", "c2"], "d": {"d1": "d3", "d2": "d2"}}}
2367 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
2369 ------------------------------------------------------------------------------------------------------
2370 {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2"], "d": {"d1": "d1", "d2": "d2"}}}
2373 SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
2375 -------------------------------------------------------------------------------------------------------------------------
2376 {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2", ["c3"], {"c4": 4}], "d": {"d1": "d1", "d2": "d2"}}}
2379 SELECT '{"ff":["a","aaa"]}'::jsonb;
2381 ----------------------
2382 {"ff": ["a", "aaa"]}
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]
2396 -- nested containment
2397 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
2403 SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
2409 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
2415 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
2421 SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
2427 SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
2433 SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
2439 SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
2445 SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
2451 SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
2457 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
2463 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
2469 SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
2475 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
2481 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
2487 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
2493 SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
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';
2506 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
2512 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
2518 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
2524 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
2530 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
2536 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
2542 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
2548 SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
2554 SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
2560 SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
2566 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
2572 SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
2578 SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
2584 SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
2590 SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
2596 --nested path extraction
2597 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
2603 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
2609 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
2615 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
2621 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
2627 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
2633 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
2639 SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
2645 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
2651 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
2657 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
2663 SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
2670 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
2676 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
2682 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
2688 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
2694 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
2700 SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
2706 -- jsonb_strip_nulls
2707 select jsonb_strip_nulls(null);
2713 select jsonb_strip_nulls('1');
2719 select jsonb_strip_nulls('"a string"');
2725 select jsonb_strip_nulls('null');
2731 select jsonb_strip_nulls('[1,2,null,3,4]');
2733 --------------------
2737 select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
2739 --------------------------------------------
2740 {"a": 1, "c": [2, null, 3], "d": {"e": 4}}
2743 select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
2745 --------------------------
2746 [1, {"a": 1, "c": 2}, 3]
2749 -- an empty object is not null and should not be stripped
2750 select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
2752 --------------------
2756 select jsonb_indent('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
2758 ----------------------------
2776 select jsonb_indent('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
2778 ---------------------------
2800 select jsonb_indent('{"a":["b", "c"], "d": {"e":"f"}}');
2814 select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
2816 -------------------------------------------------------------------
2817 {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"}
2820 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
2822 ---------------------------------------------
2823 {"b": "g", "aa": 1, "cq": "l", "fg": false}
2826 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
2828 ---------------------------------------
2829 {"b": 2, "aa": 1, "aq": "l", "cq": 3}
2832 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
2834 ------------------------------
2835 {"b": 2, "aa": "l", "cq": 3}
2838 select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
2840 ----------------------------
2841 {"b": 2, "aa": 1, "cq": 3}
2844 select '["a", "b"]'::jsonb || '["c"]';
2850 select '["a", "b"]'::jsonb || '["c", "d"]';
2852 ----------------------
2853 ["a", "b", "c", "d"]
2856 select '["c"]' || '["a", "b"]'::jsonb;
2862 select '["a", "b"]'::jsonb || '"c"';
2868 select '"c"' || '["a", "b"]'::jsonb;
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}';
2880 ----------------------
2881 ["a", "b", {"c": 1}]
2884 select '{"c": 1}'::jsonb || '["a", "b"]';
2886 ----------------------
2887 [{"c": 1}, "a", "b"]
2890 select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
2892 ------------------------------------
2893 {"b": "g", "cq": "l", "fg": false}
2896 select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
2902 select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
2908 select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
2914 select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
2920 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
2926 select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
2932 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
2938 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
2944 select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
2946 --------------------------
2947 {"a": 1, "b": 2, "c": 3}
2950 select '{"a":1 , "b":2, "c":3}'::jsonb - 'a'::text;
2956 select '{"a":null , "b":2, "c":3}'::jsonb - 'a'::text;
2962 select '{"a":1 , "b":2, "c":3}'::jsonb - 'b'::text;
2968 select '{"a":1 , "b":2, "c":3}'::jsonb - 'c'::text;
2974 select '{"a":1 , "b":2, "c":3}'::jsonb - 'd'::text;
2976 --------------------------
2977 {"a": 1, "b": 2, "c": 3}
2980 select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b'::text) = pg_column_size('{"a":1, "b":2}'::jsonb);
2986 select '["a","b","c"]'::jsonb - 3;
2992 select '["a","b","c"]'::jsonb - 2;
2998 select '["a","b","c"]'::jsonb - 1;
3004 select '["a","b","c"]'::jsonb - 0;
3010 select '["a","b","c"]'::jsonb - -1;
3016 select '["a","b","c"]'::jsonb - -2;
3022 select '["a","b","c"]'::jsonb - -3;
3028 select '["a","b","c"]'::jsonb - -4;
3034 select '{"a":1, "b":2, "c":3}'::jsonb - 3;
3036 --------------------------
3037 {"a": 1, "b": 2, "c": 3}
3040 select '{"a":1, "b":2, "c":3}'::jsonb - 2;
3046 select '{"a":1, "b":2, "c":3}'::jsonb - 1;
3052 select '{"a":1, "b":2, "c":3}'::jsonb - 0;
3058 select '{"a":1, "b":2, "c":3}'::jsonb - -1;
3064 select '{"a":1, "b":2, "c":3}'::jsonb - -2;
3070 select '{"a":1, "b":2, "c":3}'::jsonb - -3;
3076 select '{"a":1, "b":2, "c":3}'::jsonb - -4;
3078 --------------------------
3079 {"a": 1, "b": 2, "c": 3}
3082 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
3084 --------------------------------------------------------------------------
3085 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": [1, 2, 3]}
3088 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
3090 -----------------------------------------------------------------------------
3091 {"a": 1, "b": [1, [1, 2, 3]], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
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]');
3096 -----------------------------------------------------------------------------
3097 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [[1, 2, 3], 3]}, "n": null}
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]');
3102 ---------------------------------------------------------------------
3103 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3106 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
3108 -------------------------------------------------------------------------
3109 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": {"1": 2}}
3112 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
3114 ----------------------------------------------------------------------------
3115 {"a": 1, "b": [1, {"1": 2}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3118 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
3120 ----------------------------------------------------------------------------
3121 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [{"1": 2}, 3]}, "n": null}
3124 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
3126 ---------------------------------------------------------------------
3127 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3130 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
3132 --------------------------------------------------------------------------
3133 {"a": 1, "b": [1, "test"], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3136 select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
3138 ---------------------------------------------------------------------------------
3139 {"a": 1, "b": [1, {"f": "test"}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3142 select jsonb_delete('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}'::text[]);
3144 ----------------------------------------------------------
3145 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
3148 select jsonb_delete('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}'::text[]);
3150 ------------------------------------------------------------------
3151 {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3154 select jsonb_delete('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}'::text[]);
3156 ------------------------------------------------------------------
3157 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
3160 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb - '{n}'::text[];
3162 ----------------------------------------------------------
3163 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
3166 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb - '{b,-1}'::text[];
3168 ------------------------------------------------------------------
3169 {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
3172 select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb - '{d,1,0}'::text[];
3174 ------------------------------------------------------------------
3175 {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}