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

Separate quote config for headers and cells #195

Closed
kodybrand opened this issue Sep 5, 2017 · 11 comments
Closed

Separate quote config for headers and cells #195

kodybrand opened this issue Sep 5, 2017 · 11 comments

Comments

@kodybrand
Copy link

The CSV output is putting double quotes around the column titles.

var csv = json2csv({ data: myCars, fields: fields });

is producing the correct output, but with double quotes around the column titles.

"car","price","color"
"Audi",40000,"blue"
"BMW",35000,"black"
"Porsche",60000,"green"

@knownasilya
Copy link
Collaborator

This protects against cases when someone puts the delimiter into the column name.

@kodybrand
Copy link
Author

Is there a way to disable this from happening?

@knownasilya
Copy link
Collaborator

knownasilya commented Sep 6, 2017

Not at the moment. What issue is being caused by this?

@kodybrand
Copy link
Author

The software I am importing the CSV file into does not recognize the title columns when they are wrapped in quotes. I figured out a work around, but it would be useful if I could just pass in a parameter which would not wrap the titles in quotes.

@knownasilya
Copy link
Collaborator

I'd be willing to merge a PR with a feature like that, preferably with tests 👍 you can add it here https://github.com/zemirco/json2csv/blob/master/lib/json2csv.js#L169

@juanjoDiaz juanjoDiaz changed the title Quotes around ColumnTitles Separate quote config for headers and cells Feb 2, 2018
@anishxt
Copy link

anishxt commented Jun 26, 2018

I know that this is tagged as a feature request, but I'm wondering to have another(?) feature similar to this. As @kodybrand mentioned, the resultant CSV generated is:

"car","price","color"
"Audi",40000,"blue"
"BMW",35000,"black"
"Porsche",60000,"green"

Can we have an option to enclose all data fields with quotes regardless of data-type (even for integers, float, boolean) like this:

"car","price","color"
"Audi","40000","blue"
"BMW","35000","black"
"Porsche","60000","green"

@juanjoDiaz
Copy link
Collaborator

Hi @anishxt,

Would some preprocessing solve your use case?

Something like this (untested code disclaimer 🙂)
const myStringifiedData = myData.map(item => Object.keys(item).reduce((acc, key) => (acc[key] = String(item[key])), {}))

Imho, json2csv should only convert json to csv. Not change your data types.

@anishxt
Copy link

anishxt commented Jun 26, 2018

Thank you, @juanjoDiaz, for throwing that bit!

Did some adjustments to fit with my case and it worked! Thank you!! 🙂

@juanjoDiaz
Copy link
Collaborator

We should decide if we want to implement this for v5.

This is essentially a similar request as #420 .

I vote against implementing this since the CSV standard defines that the header should use the same format as normal record lines (https://tools.ietf.org/html/rfc4180#section-2).

Just because some software was broken 2 years ago is not a good enough reason to go against the standard.

@knownasilya thoughts?

@knownasilya
Copy link
Collaborator

I agree, especially since it can be done with a transform after.

@jlk34n
Copy link

jlk34n commented Feb 6, 2020

I'm not sure I agree that the ietf standard defines the header as the same format as rows; it says that "Each field may or may not [my emphasis] be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all)." (or did I miss something?)
I too have to post-process my CSVs now to accommodate a loader app that insists headers not be quoted. Adding this feature to json2csv would be a nice-to-have. =O)

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

No branches or pull requests

5 participants