简体   繁体   English

使用 psql COPY 导入带有 json 值的 csv(转义问题)

[英]Importing csv with json value with psql COPY (problem with escaping)

I am trying to import csv file to table in postgres using COPY command.我正在尝试使用 COPY 命令将 csv 文件导入 postgres 中的表。 I have problem that one column is of json data type.我有一个问题,即一列是 json 数据类型。 I tried to escape json data in csv using dollars ($$...$$) docu_4.1.2.2 .我试图使用美元 ($$...$$) docu_4.1.2.2在 csv 中转义 json 数据。 This is first line of csv:这是 csv 的第一行:

3f382d8c-bd27-4092-bd9c-8b50e24df7ec;370038757|PRIMARY_RESIDENTIAL;$${"CustomerData": "{}", "PersonModule": "{}"}$$

This is command used for import:这是用于导入的命令:

 psql -c "COPY table(id, name, details) FROM '/path/table.csv' DELIMITER ';' ENCODING 'UTF-8' CSV;"

This is error I get:这是我得到的错误:

ERROR:  invalid input syntax for type json
DETAIL:  Token "$" is invalid.
CONTEXT:  JSON data, line 1: $...
COPY table, line 1, column details: "$${CustomerData: {}, PersonModule: {}}$$"

How should I escape/import json value using COPY ?我应该如何使用COPY转义/导入 json 值? Should I give up and use something like pg_loader instead?我应该放弃并使用pg_loader东西吗? Thank you谢谢

In case of failing with importing the JSON data please give a try to the following setup - this worked for me even for quite complicated data:如果导入 JSON 数据失败,请尝试以下设置 - 即使对于非常复杂的数据,这对我也有效:

COPY "your_schema_name.yor_table_name" (your, column_names, here) 
FROM STDIN 
WITH CSV DELIMITER E'\t' QUOTE '\b' ESCAPE '\';
--here rows data
\.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM