Skip to content

ndjson and json array format #284

@Keyang

Description

@Keyang

Thank you for your package.
I just started using it today.
I have a question about my probable misunderstanding of use stream.

Here are my 2 different experiments with the file:

------ test.csv
nom,insee,pop
Marseille,13055,800000
Paris,75000,25000000

1st test:
const csv=require('csvtojson')
csv({includeColumns:/(nom|insee)/, colParser:{"insee":"string"},
checkType:true})
.fromFile("test.csv")
.then(jsonObj => fs.writeFile("result.json", JSON.stringify(jsonObj),
(err) => {if (err) return console.log(err);}))

------ result.json
[{"nom":"Marseille","insee":"13055"},{"nom":"Paris","insee":"75000"}]

Correct answer as expected.

but

2nd test:
const csv=require('csvtojson')
const fs = require('fs');
fs.createReadStream("test.csv")
.pipe(csv({includeColumns:/(nom|insee)/,
colParser:{"insee":"string"}, checkType:true}))
.pipe(fs.createWriteStream("result.json"));

------ result.json
{"nom":"Marseille","insee":"13055"}
{"nom":"Paris","insee":"75000"}

Not quite correct
(missing comma between objects and overall sqare brackets)
I don't see where I am wrong in second piece of code.

Thank you for your assistance.
Best regards
Robert.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions