@@ -5017,3 +5017,133 @@ select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;
50175017 12345
50185018(1 row)
50195019
5020+ -- test mapping of jsonb to SQL/JSON JSON type
5021+ select json(' { "aa": 1, "b" : 2 }');
5022+ json
5023+ -----------------------
5024+ { "aa": 1, "b" : 2 }
5025+ (1 row)
5026+
5027+ select json ' { "aa": 1, "b" : 2 }';
5028+ json
5029+ -----------------------
5030+ { "aa": 1, "b" : 2 }
5031+ (1 row)
5032+
5033+ select json text ' { "aa": 1, "b" : 2 }';
5034+ json
5035+ -----------------------
5036+ { "aa": 1, "b" : 2 }
5037+ (1 row)
5038+
5039+ create table test_json_as_json (js json, jb jsonb);
5040+ \d test_json_as_json
5041+ Table "public.test_json_as_json"
5042+ Column | Type | Collation | Nullable | Default
5043+ --------+-------+-----------+----------+---------
5044+ js | json | | |
5045+ jb | jsonb | | |
5046+
5047+ set json_as_jsonb = on;
5048+ select json(' { "aa": 1, "b" : 2 }');
5049+ jsonb
5050+ -------------------
5051+ {"b": 2, "aa": 1}
5052+ (1 row)
5053+
5054+ select json ' { "aa": 1, "b" : 2 }';
5055+ jsonb
5056+ -------------------
5057+ {"b": 2, "aa": 1}
5058+ (1 row)
5059+
5060+ select json text ' { "aa": 1, "b" : 2 }';
5061+ json
5062+ -----------------------
5063+ { "aa": 1, "b" : 2 }
5064+ (1 row)
5065+
5066+ \d test_json_as_json
5067+ Table "public.test_json_as_json"
5068+ Column | Type | Collation | Nullable | Default
5069+ --------+-----------+-----------+----------+---------
5070+ js | json text | | |
5071+ jb | json | | |
5072+
5073+ create table test_json_as_jsonb (js json, jb jsonb, jt json text);
5074+ \d test_json_as_jsonb
5075+ Table "public.test_json_as_jsonb"
5076+ Column | Type | Collation | Nullable | Default
5077+ --------+-----------+-----------+----------+---------
5078+ js | json | | |
5079+ jb | json | | |
5080+ jt | json text | | |
5081+
5082+ insert into test_json_as_jsonb values ('{ "a" : 1 }', '{ "a" : 1 }', '{ "a" : 1 }');
5083+ select * from test_json_as_jsonb;
5084+ js | jb | jt
5085+ ----------+----------+-------------
5086+ {"a": 1} | {"a": 1} | { "a" : 1 }
5087+ (1 row)
5088+
5089+ select jsonb_object_field(js, 'a') from test_json_as_jsonb;
5090+ jsonb_object_field
5091+ --------------------
5092+ 1
5093+ (1 row)
5094+
5095+ select jsonb_object_field(jb, 'a') from test_json_as_jsonb;
5096+ jsonb_object_field
5097+ --------------------
5098+ 1
5099+ (1 row)
5100+
5101+ select jsonb_object_field(jt, 'a') from test_json_as_jsonb;
5102+ ERROR: function jsonb_object_field(json text, unknown) does not exist
5103+ LINE 1: select jsonb_object_field(jt, 'a') from test_json_as_jsonb;
5104+ ^
5105+ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
5106+ select json_object_field(jt, 'a') from test_json_as_jsonb;
5107+ json_object_field
5108+ -------------------
5109+ 1
5110+ (1 row)
5111+
5112+ set json_as_jsonb = off;
5113+ \d test_json_as_jsonb
5114+ Table "public.test_json_as_jsonb"
5115+ Column | Type | Collation | Nullable | Default
5116+ --------+-------+-----------+----------+---------
5117+ js | jsonb | | |
5118+ jb | jsonb | | |
5119+ jt | json | | |
5120+
5121+ select * from test_json_as_jsonb;
5122+ js | jb | jt
5123+ ----------+----------+-------------
5124+ {"a": 1} | {"a": 1} | { "a" : 1 }
5125+ (1 row)
5126+
5127+ select jsonb_object_field(js, 'a') from test_json_as_jsonb;
5128+ jsonb_object_field
5129+ --------------------
5130+ 1
5131+ (1 row)
5132+
5133+ select jsonb_object_field(jb, 'a') from test_json_as_jsonb;
5134+ jsonb_object_field
5135+ --------------------
5136+ 1
5137+ (1 row)
5138+
5139+ select jsonb_object_field(jt, 'a') from test_json_as_jsonb;
5140+ ERROR: function jsonb_object_field(json, unknown) does not exist
5141+ LINE 1: select jsonb_object_field(jt, 'a') from test_json_as_jsonb;
5142+ ^
5143+ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
5144+ select json_object_field(jt, 'a') from test_json_as_jsonb;
5145+ json_object_field
5146+ -------------------
5147+ 1
5148+ (1 row)
5149+
0 commit comments