Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CSV delimiter other than comma #535

Closed
ghost opened this issue Mar 28, 2016 · 4 comments
Closed

Support CSV delimiter other than comma #535

ghost opened this issue Mar 28, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Mar 28, 2016

I would like to populate a table that has a JSONB field and some non-JSONB fields using CSV format. I run into problems when the JSONB value in the CSV file has more than one key-value pair because I need to separate pairs with a comma.

Example line from CSV file:
foo,"{""a"": 1, ""b"": "x"}"

Is there a way to provide a parameter like in PostgreSQL's COPY command ("copy ... with delimiter '|'...")?

@SebAlbert
Copy link

It may be only in your posting of an example here, but I think the quotation marks surrounding your x inside the JSONB are not properly escaped.

On basically that very same note, I think first and foremost any CSV importing feature should implement RFC-compliant escaping (I don't know if PostgREST does it already), so your commas inside the quotation marks surrounding the entire JSONB would be properly ignored.

Nevertheless, I don't object against the choice of arbitrary delimiters for both separation of columns and quoting; I would in fact also appreciate it for CSV output from PostgREST to be speicifiable, maybe through some Prefer header, unless possible in the Accept header.

@begriffs
Copy link
Member

@SebAlbert could you provide me some small examples to test RFC compliance? I'll turn them into tests for the test suite.

@rrantzau the ability to do the true COPY command depends on nikita-volkov/hasql#1. Also thinking about your specific example, maybe the x has too few quotes, what about trying this: foo,"{""a"": 1, ""b"": ""x""}"

@ghost
Copy link
Author

ghost commented Mar 28, 2016

Thank you for the replies, it was my mistake!

The following example does work:

psql -U postgres -c "drop table if exists foo.testj cascade"
psql -U postgres -c "create table foo.testj (a int, b text, j jsonb)"
echo 'a,b,j
1,b1,"{""x"": 10, ""y"": ""y1""}"' > ./testj.csv
cat testj.csv
curl "http://localhost:3000/testj" -H "Content-type: text/csv; charset=utf-8" --data-binary @testj.csv
curl "http://localhost:3000/testj?a,b,j" -H "Content-type: text/csv; charset=utf-8"
[{"a":1,"b":"b1","j":{"x": 10, "y": "y1"}}]
psql -U postgres -c "select * from foo.testj"
 a | b  |          j           
---+----+----------------------
 1 | b1 | {"x": 10, "y": "y1"}
(1 row)

Great!

@ghost ghost closed this as completed Mar 28, 2016
@SebAlbert
Copy link

Just some comments, they may or may not be of use: You may want to use the Accept header instead of the Content-type header in order to actually GET csv returned from the server. And you may want to use ?select=a,b,j instead of ?a,b,j

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants