mrodrig / json-2-csv

Convert JSON to CSV *or* CSV to JSON!
https://mrodrig.github.io/json-2-csv
MIT License
420 stars 58 forks source link

[CSV -> JSON] An empty value at the end of the CSV does not write to JSON properly. #155

Closed Bakabake closed 4 years ago

Bakabake commented 4 years ago

Background Information

The issue I'm reporting is with:

I have...

Expected Behavior

If there exists an empty element as the very last entry of the CSV, an empty value should be written out.

Actual Behavior

The last field does not get written out, and the previous element has a comma appended to the end of the value. Using the csv/code below, the output will be:

[{ "data": { "uid":"my_uid," } }]

Data Sample

CSV:

data.uid,data.name
my_uid,

Code Example

// Please include a simple example to replicate the issue
let converter = require('json-2-csv');

converter.csv2jsonAsync(csv)
    .then(json => { console.log(JSON.stringify(json)) })

Additional Notes

I've noticed that consecutive empty values also cause the same issue.
Use the above test code with the following csv:

data.uid,data.name
,

The resulting JSON will look like:

[{ "data":{ "uid": "," }}]

Multiple empty rows produce proper output except for the last row:

data.uid,data.name
,
,

Results in:

[ { "data":{ "uid": "", "name": "" } },
  { "data":{ "uid": "," } }]

Also, if the last element is not empty, everything works fine. Using this csv:

data.uid,data.name,data.desc
my_uid,,
my_other_uid,,test

Produces the appropriate JSON:

[ {"data":{"uid":"my_uid",
                "name":"",
                "desc":""}},
  {"data":{"uid":"my_other_uid",
                "name":"",
                "desc":"test"}}]
mrodrig commented 4 years ago

Thanks for reporting this @Bakabake. Another good find. I have a PR out to handle that scenario and will merge it in once the tests finish running. I should have it published to NPM in a little bit as 3.7.5. Thanks again for reporting it and for all of the additional notes you provided. They were really helpful.

Bakabake commented 4 years ago

Anytime! I'm always happy to help!

dilshansdoq commented 8 months ago

Hi @mrodrig I am facing a similar issue to this. Input: sender_id,receiver_id,message,image\r\nu1,"u2","m1",\r\nu1,"u3","m2", Expected Output:

[
          { sender_id: 'u1', receiver_id: 'u2', message: 'm1', image: '' },
          { sender_id: 'u1', receiver_id: 'u3', message: 'm2', image: '' },
]

Actual Output:

[
          { sender_id: 'u1', receiver_id: 'u2', message: 'm1', image: '' },
          { sender_id: 'u1', receiver_id: 'u3', message: 'm2', image: ',' },
]

Code:

const parsed = csvParser.csv2json(data, {
  trimFieldValues: true,
  trimHeaderFields: true,
});

So the issue occurs when at the end of nth row, there is no line break(lf or crlf), and the (m-1)st column has quoted string.

Please let me know if you require a minimum reproduction code. I will create a separate issue then.