问题
If I have the following json file
C:\PostgresStage>type test2.json
{"key":"Hello \"World\""}
And I try to load it into a json or text column using the COPY command I end up with invalid JSON because copy appears to strip the escape characters from the file as can be seen below
postgres=# \d+ T1
Table "public.t1"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+------+-----------+----------+---------+----------+--------------+-------------
data | text | | | | extended | |
postgres=# delete from T1;
DELETE 1
postgres=# copy t1 from 'c:\PostgresStage\test2.json';
COPY 1
postgres=# select * from T1;
data
-------------------------
{"key":"Hello "World""}
(1 row)
postgres=# select data::jsonb from T1;
ERROR: invalid input syntax for type json
DETAIL: Token "World" is invalid.
CONTEXT: JSON data, line 1: {"key":"Hello "World...
postgres=#
Is there anyway to prevent this..
Also if I have 'pretty' JSON such as this
C:\PostgresStage>type test2.json
{
"key":"Hello \"World\""
}
It gets loaded as 3 rows. Is there anyway to use copy to load the entire file as one row.
Finally is there anyway to use COPY to pass a parameter to a function rather than load a row into a table
回答1:
For JSON documents that are not "pretty printed", eg the entire document is a single line in the file, a hack is to use
copy t1 from 'c:\PostgresStage\test2.json' csv quote e'\x01' delimiter e'\x02';
Thanks to http://adpgtech.blogspot.com/2014/09/importing-json-data.html
来源:https://stackoverflow.com/questions/52334512/postgres-copy-with-command-strips-escapes-from-json-file-also-loads-each-line-a