This content originally appeared on remy sharp's b:log and was authored by remy sharp's b:log
The problem: CSV files generally quoted using "
and delimited using ,
- both of which feature heavily in JSON strings.
So importing a CSV with JSON doesn't fly.
One solution (it took me hours and hours to solve) is to use an alternative byte for quote and delimiter.
For example, quote using \x1
and delimiter using \x2
. Inspecting the bytes for the source data should look a little like this if done right:
Now to import requires careful command-line-fu:
$ cat data.csv | psql postgres://localhost:5432/my_db -c "\copy quiz (id,title,description,\"maxPoints\",source,attribution,lessons) FROM STDIN csv QUOTE E'\x1' DELIMITER E'\x2'"
A few key points:
- The mixed case column needs quoting (and I'm escaping the quotes)
- I'm using hex representation of the byte values
1
and2
, so that's\xDIGIT
- For the postgres import to understand the escaped digit, I must use
E
before the string
Now I'm able to import JSON in a CSV file.
Originally published on Remy Sharp's b:log
This content originally appeared on remy sharp's b:log and was authored by remy sharp's b:log

remy sharp's b:log | Sciencx (2021-03-09T12:53:55+00:00) Postgres CSV import with JSON [til/data]. Retrieved from https://www.scien.cx/2021/03/09/postgres-csv-import-with-json-til-data/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.