替换 postgresql jsonb 数组对象中的撇号 ( ' )
Replace apostrophe ( ' ) in postgresql jsonb array object
请找到下面的 jsonb 对象,我需要在其中替换指令密钥中的所有撇号。
[{
"instruction": "Don't need to book car for M'lore location",
"reservationNo": "TT00098272"
},
{
"instruction": "Please book a car for mumbai location",
"reservationNo": "TT00098273"
}
]
预期结果:值被替换为撇号
[{
"instruction": "Dont need to book car for Mlore location",
"reservationNo": "TT00098272"
},
{
"instruction": "Please book a car for mumbai location",
"reservationNo": "TT00098273"
}
]
您可以在投射到 text
后执行 regexp_replace()
:
postgres=# create table j (field jsonb);
CREATE TABLE
postgres=# insert into j values ('[{
postgres'# "instruction": "Don''t need to book car for M''lore location",
postgres'# "reservationNo": "TT00098272"
postgres'# },
postgres'# {
postgres'# "instruction": "Please book a car for mumbai location",
postgres'# "reservationNo": "TT00098273"
postgres'# }
postgres'# ]'::jsonb);
INSERT 0 1
postgres=# select regexp_replace(field::text,'''','','g')::jsonb from j;
regexp_replace
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"instruction": "Dont need to book car for Mlore location", "reservationNo": "TT00098272"}, {"instruction": "Please book a car for mumbai location", "reservationNo": "TT00098273"}]
(1 row)
请找到下面的 jsonb 对象,我需要在其中替换指令密钥中的所有撇号。
[{
"instruction": "Don't need to book car for M'lore location",
"reservationNo": "TT00098272"
},
{
"instruction": "Please book a car for mumbai location",
"reservationNo": "TT00098273"
}
]
预期结果:值被替换为撇号
[{
"instruction": "Dont need to book car for Mlore location",
"reservationNo": "TT00098272"
},
{
"instruction": "Please book a car for mumbai location",
"reservationNo": "TT00098273"
}
]
您可以在投射到 text
后执行 regexp_replace()
:
postgres=# create table j (field jsonb);
CREATE TABLE
postgres=# insert into j values ('[{
postgres'# "instruction": "Don''t need to book car for M''lore location",
postgres'# "reservationNo": "TT00098272"
postgres'# },
postgres'# {
postgres'# "instruction": "Please book a car for mumbai location",
postgres'# "reservationNo": "TT00098273"
postgres'# }
postgres'# ]'::jsonb);
INSERT 0 1
postgres=# select regexp_replace(field::text,'''','','g')::jsonb from j;
regexp_replace
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"instruction": "Dont need to book car for Mlore location", "reservationNo": "TT00098272"}, {"instruction": "Please book a car for mumbai location", "reservationNo": "TT00098273"}]
(1 row)