简体   繁体   English

如何使用Node.js将Csv转换为Json?

[英]How to convert Csv to Json using Node.js?

var csvToJson = require('convert-csv-to-json');
var fileInputName = 'Bangalore_schools.csv'; 
var fileOutputName = 'output.json';
csvToJson.generateJsonFileFromCsv(fileInputName, fileOutputName);

Csv File Link: Csv File Csv文件链接: Csv文件

But the above code i have used i am unable to fully convert it to json. 但是我使用的上述代码无法将其完全转换为json。 Please help. 请帮忙。 Output is coming like this.. i want to separate each key and its value likewise we have in json. 输出是这样的..我想像我们在json中一样分离每个键及其值。

[
 {
  "district|block|cluster|schoolid|schoolname|category|gender|medium_of_inst|address|area|pincode|landmark|identification1|busroutes|identification2|latlong": "-----------+---------+----------------------------+----------+-------------------------------------------+---------------+--------+----------------+---------------------------------------------------------------------------------------------------------------+-----------------------------------------+------------------+-----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+-------------------------------------------+-----------------------------------------------------------------------------------------------------+------------------------------------------"
 },
 {
  "district|block|cluster|schoolid|schoolname|category|gender|medium_of_inst|address|area|pincode|landmark|identification1|busroutes|identification2|latlong": " bangalore|south-1|vajara halli|32868|GKHPS ALAHALLI|Upper Primary|co-ed|kannada|avalahalli south range- 1|bangalore|560062|masjid||||POINT(77.5766623020172 12.8893003120716)"
 }, 

The default delimiter convert-csv-to-json uses is a semicolon ( ; ). 默认的分隔符convert-csv-to-json使用的是分号( ; )。 If you want to use a different delimiter, like | 如果要使用其他定界符,例如| the file you shared uses, you need to define it explicitly. 共享使用的文件,您需要明确定义它。 Eg: 例如:

csvToJson.fieldDelimiter('|').generateJsonFileFromCsv(fileInputName, fileOutputName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM