jsonb字段中的PostgreSQL重命名属性

jsonb字段中的PostgreSQL重命名属性

问题描述:

在PostgreSQL 9.5中,是否可以重命名jsonb字段中的属性?

In postgresql 9.5, is there a way to rename an attribute in a jsonb field?

例如:

{ "nme" : "test" }

应重命名为

{ "name" : "test"}

UPDATE中,使用删除(-)和并置(||)运算符,例如:

create table example(id int primary key, js jsonb);
insert into example values
    (1, '{"nme": "test"}'),
    (2, '{"nme": "second test"}');

update example
set js = js - 'nme' || jsonb_build_object('name', js->'nme')
where js ? 'nme'
returning *;

 id |           js            
----+-------------------------
  1 | {"name": "test"}
  2 | {"name": "second test"}
(2 rows)