如何在文本字段中的 PostgreSQL 中添加新的 JSON 对象
how to add new JSON Object in PostgreSQL in a text field
我在 PostgreSQL 中有一列具有文本数据类型(数据文本)
这一栏的数据会像
[
{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"}
]
我需要为其添加新参数,例如
{"code":"Mobile","paramValue":"9XXXXXXXXX"}
数据会像
[
{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"},
{"code":"Mobile","paramValue":"9XXXXXXXXX"}
]
我试了很多方法都没有成功
我能得到这方面的建议吗
只需使用 ||
运算符。 Documentation
考虑到您的结构始终是存储在文本列中的有效 JSON 数组。
试试这个:
create table test (id int, json_field text);
insert into test values(1,'[{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"}]');
最终代码
update test set json_field=
json_field::jsonb || '{"mobile":"kXXXXXXXX"}'
where id=1
我在 PostgreSQL 中有一列具有文本数据类型(数据文本)
这一栏的数据会像
[
{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"}
]
我需要为其添加新参数,例如
{"code":"Mobile","paramValue":"9XXXXXXXXX"}
数据会像
[
{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"},
{"code":"Mobile","paramValue":"9XXXXXXXXX"}
]
我试了很多方法都没有成功 我能得到这方面的建议吗
只需使用 ||
运算符。 Documentation
考虑到您的结构始终是存储在文本列中的有效 JSON 数组。
试试这个:
create table test (id int, json_field text);
insert into test values(1,'[{"code":"ABC","Value":"A1"},
{"code":"Name","Value":"Ramesh"},
{"code":"Age","paramValue":"24"}]');
最终代码
update test set json_field=
json_field::jsonb || '{"mobile":"kXXXXXXXX"}'
where id=1