zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.71k stars 365 forks source link

How to remove double quotes only in fields #316

Closed kitlung closed 6 years ago

kitlung commented 6 years ago

Using version 4.2,

var fields = ['CardNumber', 'BatchID', 'ValueNumber', 'Value', 'ExpDate', 'ExpDateDisplay']

csvObj = { 'CardNumber' : "...", 'BatchID' : "...", 'ValueNumber' : "...",, 'Value' : "...",, 'ExpDate' : "...",, 'ExpDateDisplay' : "..." } csvArr.push(csvObj) var csv = json2csv({ data: csvArr, fields: fields}) console.log(csv)

Result: "CardNumber","BatchID","ValueNumber","Value","ExpDate","ExpDateDisplay" "...", "...", "...", "...", "...", "..." "...", "...", "...", "...", "...", "..."

However, I want to remove those double quote in fields. Are there any possible solutions?

juanjoDiaz commented 6 years ago

That's done in Example 6 of the readme.

Just use the quote option quote: ''.

kitlung commented 6 years ago

@juanjoDiaz This will remove all quote. The result in Example 2 is what I want. But I get an error TypeError: Json2csvParser is not a constructor when following the example.

juanjoDiaz commented 6 years ago

Are you really using version 4.X? That last error looks like you are using 3.X but calling as if it was 4.X. and var csv = json2csv({ data: csvArr, fields: fields}) is definitely 3.X API.

Can you post a running code sample, please? Then I can help you with that error :)

Regarding removing the quotes from the headers but not from anything else. It was requested on #195 but is not implemented.

You can do it with some post processing though:

var csv = '"acb"\n"fhk"'
const rows = csv.split('\n');
const header = rows.shift().replace(/"/g, '');
rows.unshift(header);
csv = rows.join('\n');
kitlung commented 6 years ago

It works, thank you. I have checked the package.json and package-lock.json, the version is 4.2.0