简体   繁体   中英

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. I have problem that one column is of json data type. I tried to escape json data in csv using dollars ($$...$$) docu_4.1.2.2 . This is first line of 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 ? Should I give up and use something like pg_loader instead? 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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