小编Sid*_*Sid的帖子

将带有副本的有效json插入postgres表

有效的JSON自然可以有反斜杠字符:\.在SQL语句中插入数据时,如下所示:

sidharth=# create temp table foo(data json);
CREATE TABLE
sidharth=# insert into foo values( '{"foo":"bar", "bam": "{\"mary\": \"had a lamb\"}" }');
INSERT 0 1

sidharth=# select * from foo;

data                         
\-----------------------------------------------------

{"foo":"bar", "bam": "{\"mary\": \"had a lamb\"}" }
(1 row)
Run Code Online (Sandbox Code Playgroud)

事情很好.

但是,如果我将JSON复制到文件并运行复制命令,我得到:

sidharth=# \copy foo from './tests/foo' (format text); 


ERROR:  invalid input syntax for type json
DETAIL:  Token "mary" is invalid.
CONTEXT:  JSON data, line 1: {"foo":"bar", "bam": "{"mary...
COPY foo, line 1, column data: "{"foo":"bar", "bam": "{"mary": "had a …
Run Code Online (Sandbox Code Playgroud)

postgresql json postgresql-9.3

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

json ×1

postgresql ×1

postgresql-9.3 ×1