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

Stringified Objects are double quoted and non Stringified objects are output as [object Object] #58

Open
nrodriguez opened this issue Dec 3, 2020 · 3 comments · May be fixed by #59
Open
Labels

Comments

@nrodriguez
Copy link

nrodriguez commented Dec 3, 2020

If I want my csv to simply output a column with a stringified object, it will end up with double quotes

Desired
{"can_debit_product_attributes":{"experience_type":"ecommerce","is_comp":false}}

Actual output

“{”“can_debit_product_attributes”“:{“”experience_type”“:”“ecommerce”“,”“is_comp”“:false}}”

If I don't stringify my objects then it's output as

[object Object]

Anyway around this?

I did see we check if it has a quote already and then replace it with a double quote here: https://github.com/ryu1kn/csv-writer/blob/master/src/lib/field-stringifier.ts#L27

@ryu1kn
Copy link
Owner

ryu1kn commented Dec 4, 2020

Hi @nrodriguez , thanks for the PR. It works like that as RFC 4180 says this:

  1. If double-quotes are used to enclose fields, then a double-quote
    appearing inside a field must be escaped by preceding it with
    another double quote. For example:

    "aaa","b""bb","ccc

https://tools.ietf.org/html/rfc4180#section-2

If you open "{""can_debit_...}" in excel or something you should see what you expect (i.e. {"can_debit_...})

@ryu1kn ryu1kn added the question label Dec 4, 2020
@nrodriguez
Copy link
Author

Thanks for the response @ryu1kn!

I was intending to use the exported csv in a script which was generating some sql so obviously it would then try to put it into the database with the double set of quotes. Would it be worth adding some kind of switch that allows us to export the csv with what we give it assuming we might not be opening it in excel or something?

@mgr34
Copy link

mgr34 commented Jan 6, 2021

@nrodriguez passing through as some googling brought me here for my own problem with csv-writer. However, I think @ryu1kn is correct that this is appropriate behavior per the RFC 4180 spec. But more importantly, I had a similar use case scenario as you and found it working. I wanted to dump my csv's into some sql (mariadb) and come back to the information later and JSON.parse the field. I am able to do just that without issue.

What tripped me up is I was looking at the output of the csv directly and trying to cut and paste that field into node didn't work for JSON.parse. But assuming you import SQL with fields delimited by the proper field delimiter it will only treat the outside set the surrounding pair of quotes as the delimiter and the other questions as quotes and any field delimiters inside the quotes will be ignored.

Note: I am using maria as an upstream source to SOLR. Both return the field in a way that JSON.parse works without issue.

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

Successfully merging a pull request may close this issue.

3 participants