在 Postgres 中修改 JSONB 字段的数据类型
Modify datatype of a field in JSONB in Postgres
我有一个 JSONB 列,其中 JSON 看起来像这样:
{"id" : "3", "username" : "abcdef"}
有没有办法将 JSON 更新为 :
{"id" : 3, "username" : "abcdef"}
select json_build_object('id',CAST(j->>'id' AS NUMERIC),'username',j->>'username')::jsonb from (
select '{"id" : "3", "username" : "abcdef"}' :: jsonb AS j
)t
使用 concatenate operator || 更新 jsonb 列,例如:
create table example(id int, js jsonb);
insert into example values
(1, '{"id": "3", "username": "abcdef"}');
update example
set js = js || jsonb_build_object('id', (js->>'id')::int)
returning *;
id | js
----+---------------------------------
1 | {"id": 3, "username": "abcdef"}
(1 row)
我有一个 JSONB 列,其中 JSON 看起来像这样:
{"id" : "3", "username" : "abcdef"}
有没有办法将 JSON 更新为 :
{"id" : 3, "username" : "abcdef"}
select json_build_object('id',CAST(j->>'id' AS NUMERIC),'username',j->>'username')::jsonb from (
select '{"id" : "3", "username" : "abcdef"}' :: jsonb AS j
)t
使用 concatenate operator || 更新 jsonb 列,例如:
create table example(id int, js jsonb);
insert into example values
(1, '{"id": "3", "username": "abcdef"}');
update example
set js = js || jsonb_build_object('id', (js->>'id')::int)
returning *;
id | js
----+---------------------------------
1 | {"id": 3, "username": "abcdef"}
(1 row)