-
Notifications
You must be signed in to change notification settings - Fork 272
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
Stream mode does not produce an array of JSON #321
Comments
+1 on this, but only when using it piped |
The same here. It's a little problem. import fs from 'fs';
import {Transform} from 'stream';
import csvtojson from 'csvtojson/v2';
// flag to know if it's the first chunk
let theFirstChunk = true;
// create a read stream
const rs = fs.createReadStream(src);
const csv = csvtojson({
ignoreEmpty: true,
})
.subscribe((item, index) => {
// transform of json item here what you need
});
// create a writable stream
const ws = fs.createWriteStream(dest);
// transformation stream
const transform = new Transform({
transform(chunk, encoding, callback) {
const json = JSON.parse(chunk);
let string = `,${JSON.stringify(json)}`;
if (theFirstChunk) {
string = '[' + string.replace(/^\,\s*/, '');
theFirstChunk = false;
}
callback(null, string);
},
// for the last chunk add ]
flush(callback) {
callback(null, ']');
}
});
rs.pipe(csv).pipe(transform).pipe(ws); |
Duplicate of #307 |
Hi, I think that this ticket should not be closed because the array generated is syntactically incorrect, since it contains a comma after the last row: Received result: Using v 2.0.10. |
@adams-family the following issue exists for the trailing comma issue, #333 |
I am not sure what the expected behaviour is.
My output is one JSON object per line with no wrapping array and no comma separators e.g.
{name:"Joe Bloggs",status:"alive"}
{name:"Alice Kay",status:"deceased"}
etc.
The text was updated successfully, but these errors were encountered: