psql insert json with double quotes inside strings

psql insert json with double quotes inside strings

我正在尝试通过 psqlexiftool 生成的 JSON 插入到 postgresql 中,这似乎是有效的。似乎以某种方式转义单引号和转义双引号无法正常工作。我不知道如何正确转义 json。似乎 psql 没有正确处理单引号转义,因为它将 \" 引导到 psql 而不是查询。

鉴于此 table

create table test (exif jsonb);

这些工作:

test=> insert into test values ('{"a": 1, "b": "2"}');
INSERT 0 1
test=> insert into test values ('{"a": 1, "b": "2\""}');
INSERT 0 1
test=> select * from test;
     exif
----------------------
{"a": 1, "b": "2"}
{"a": 1, "b": "2\""}

但这些不会

test=> insert into test values ('{"a": 1, "b": "1\' 2\""}');
Invalid command \""}');. Try \? for help.

test=> select '{"a": 1, "b": "1' 2\""}';
Invalid command \""}';. Try \? for help.

test=> select E'{"a": 1, "b": "1' 2\""}';
Invalid command \""}';. Try \? for help.

test=> select '{"a": 1, "b": "1\' 2\""}';
Invalid command \""}';. Try \? for help.

有什么建议吗?

在转义单引号的数据库命令中,您需要将其加倍:

test=> insert into test values ('{"a": 1, "b": "1'' 2\""}');

这是正确转义单引号的方法:

test=> select '{"a": 1, "b": "1'' 2\""}';