可以使用查询从现有的 jsonb 列中更新新创建的 json 格式列,并按所需顺序选择键。下面是一个示例表、数据集和更新查询。
create table crayons (
id serial,
color_json json,
color_jsonb jsonb,
primary key (id))
ugautil=> \d crayons
Table "public.crayons"
Column | Type | Collation | Nullable | Default
-------------+---------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('crayons_id_seq'::regclass)
color_json | json | | |
color_jsonb | jsonb | | |
Indexes:
"crayons_pkey" PRIMARY KEY, btree (id)
color_json 列将保留与原始数据中相同的文本
文件。 color_jsonb 列不会保留键顺序,
重复的键、额外的空格等。
前5行数据:
ugautil=> select color_json from crayons order by id limit 5;
-----------------------------------------------------------
{"hex":"#EFDECD","name":"Almond","rgb":"(239, 222, 205)"}
{"hex":"#CD9575","name":"Antique Brass","rgb":"(205, 149, 117)"}
{"hex":"#FDD9B5","name":"Apricot","rgb":"(253, 217, 181)"}
{"hex":"#78DBE2","name":"Aquamarine","rgb":"(120, 219, 226)"}
{"hex":"#87A96B","name":"Asparagus","rgb":"(135, 169, 107)"}
当我们从 jsonb 列中选择相同的数据时,请注意它改变了键顺序。
名称在 rgb 之后
ugautil=> select color_jsonb from crayons order by id limit 5;
-------------------------------------------------------------
{"hex": "#EFDECD", "rgb": "(239, 222, 205)", "name": "Almond"}
{"hex": "#CD9575", "rgb": "(205, 149, 117)", "name": "Antique Brass"}
{"hex": "#FDD9B5", "rgb": "(253, 217, 181)", "name": "Apricot"}
{"hex": "#78DBE2", "rgb": "(120, 219, 226)", "name": "Aquamarine"}
{"hex": "#87A96B", "rgb": "(135, 169, 107)", "name": "Asparagus"}
我们可以使用 to_json() 函数将 color_jsonb 列转换回 json 格式,但它没有
返回原始键顺序。
ugautil=> select to_json(color_jsonb) from crayons limit 5;
----------------------------------------------------------------
{"hex": "#EFDECD", "rgb": "(239, 222, 205)", "name": "Almond"}
{"hex": "#CD9575", "rgb": "(205, 149, 117)", "name": "Antique Brass"}
{"hex": "#FDD9B5", "rgb": "(253, 217, 181)", "name": "Apricot"}
{"hex": "#78DBE2", "rgb": "(120, 219, 226)", "name": "Aquamarine"}
{"hex": "#87A96B", "rgb": "(135, 169, 107)", "name": "Asparagus"}
但是我们可以挑选出单独的键并对其进行格式化。
ugautil=> select format('{"hex": %s,"name": %s, "rgb": %s}',
color_jsonb->'hex',color_jsonb->'name', color_jsonb->'rgb')
from crayons limit 5;
----------------------------------------------------------------
{"hex": "#EFDECD","name": "Almond", "rgb": "(239, 222, 205)"}
{"hex": "#CD9575","name": "Antique Brass", "rgb": "(205, 149, 117)"}
{"hex": "#FDD9B5","name": "Apricot", "rgb": "(253, 217, 181)"}
{"hex": "#78DBE2","name": "Aquamarine", "rgb": "(120, 219, 226)"}
{"hex": "#87A96B","name": "Asparagus", "rgb": "(135, 169, 107)"}
更改表以创建一个新列来保存您想要的 json 类型
ugautil=> alter table crayons add column color_json2 json;
使用从 jsonb 中选择的查询更新表
列,并按您想要的顺序格式化键,并使用它
更新新的 color_json2 列。
with subquery as (
select id,
format('{"hex": %s,"name": %s, "rgb": %s}',
color_jsonb->'hex',color_jsonb->'name',
color_jsonb->'rgb') as "color_json2_fmt"
from crayons
)
update crayons
set color_json2 = subquery.color_json2_fmt::json
from subquery
where crayons.id = subquery.id;
ugautil=> select color_json2 from crayons limit 5;
color_json2
---------------------------------------------------------------
{"hex": "#EFDECD","name": "Almond", "rgb": "(239, 222, 205)"}
{"hex": "#CD9575","name": "Antique Brass", "rgb": "(205, 149, 117)"}
{"hex": "#FDD9B5","name": "Apricot", "rgb": "(253, 217, 181)"}
{"hex": "#78DBE2","name": "Aquamarine", "rgb": "(120, 219, 226)"}
{"hex": "#87A96B","name": "Asparagus", "rgb": "(135, 169, 107)"}
Explanation of JSONB introduced by PostgreSQL
updating table rows in postgres using subquery