如何在 Node.js 中将 CSV 转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16831250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:40:01  来源:igfitidea点击:

How to convert CSV to JSON in Node.js

javascriptjsonnode.jscsvexpress

提问by Jetson John

I am trying to convert csv file to json. I am using .

我正在尝试将 csv 文件转换为 json。我在用 。

Example CSV:

示例 CSV:

a,b,c,d
1,2,3,4
5,6,7,8
...

Desired JSON:

所需的 JSON:

{"a": 1,"b": 2,"c": 3,"d": 4},
{"a": 5,"b": 6,"c": 7,"d": 8},
...

I tried node-csv parser library.But the output is like array not like I expected.

我尝试了 node-csv 解析器库。但输出就像数组,不像我预期的那样。

I'm using Node 0.8 and express.js and would like a recommendation on how to easily accomplish this.

我正在使用 Node 0.8 和 express.js,并希望获得有关如何轻松完成此操作的建议。

回答by Keyang

Node.js csvtojsonmoduleis a comprehensive nodejs csv parser. It can be used as node.js app library / a command line tool / or browser with help of browserifyor webpack.

Node.jscsvtojson模块是一个全面的 nodejs csv 解析器。它可以用来作为Node.js的应用程序库/命令行工具/或与帮助浏览器browserifywebpack

the source code can be found at: https://github.com/Keyang/node-csvtojson

源代码可以在:https: //github.com/Keyang/node-csvtojson

It is fast with low memory consumption yet powerful to support any of parsing needs with abundant API and easy to read documentation.

它速度快,内存消耗低,但功能强大,可以通过丰富的 API 和易于阅读的文档来支持任何解析需求。

The detailed documentation can be found here

详细的文档可以在这里找到

Here are some code examples:

下面是一些代码示例:

Use it as a library in your Node.js application ([email protected] +):

将其用作 Node.js 应用程序中的库 ([email protected] +):

  1. Install it through npm
  1. 安装通过 npm

npm install --save csvtojson@latest

npm install --save csvtojson@latest

  1. Use it in your node.js app:
  1. 在您的 node.js 应用程序中使用它:
// require csvtojson
var csv = require("csvtojson");

// Convert a csv file with csvtojson
csv()
  .fromFile(csvFilePath)
  .then(function(jsonArrayObj){ //when parse finished, result will be emitted here.
     console.log(jsonArrayObj); 
   })

// Parse large csv with stream / pipe (low mem consumption)
csv()
  .fromStream(readableStream)
  .subscribe(function(jsonObj){ //single json object will be emitted for each csv line
     // parse each json asynchronousely
     return new Promise(function(resolve,reject){
         asyncStoreToDb(json,function(){resolve()})
     })
  }) 

//Use async / await
const jsonArray=await csv().fromFile(filePath);

Use it as a command-line tool:

将其用作命令行工具:

sh# npm install csvtojson
sh# ./node_modules/csvtojson/bin/csvtojson ./youCsvFile.csv

-or-

-或者-

sh# npm install -g csvtojson
sh# csvtojson ./yourCsvFile.csv

For advanced usage:

高级用法:

sh# csvtojson --help

You can find more details from the github page above.

您可以从上面的 github 页面找到更多详细信息。

回答by brnrd

You can try to use underscore.js

你可以尝试使用underscore.js

First convert the lines in arrays using the toArrayfunction :

首先使用toArray函数转换数组中的行:

var letters = _.toArray(a,b,c,d);
var numbers = _.toArray(1,2,3,4);

Then object the arrays together using the objectfunction :

然后使用对象函数将数组对象在一起:

var json = _.object(letters, numbers);

By then, the json var should contain something like :

到那时,json var 应该包含如下内容:

{"a": 1,"b": 2,"c": 3,"d": 4}

回答by Jess

Here is a solution that does not require a separate module. However, it is very crude, and does not implement much error handling. It could also use more tests, but it will get you going. If you are parsing very large files, you may want to seek an alternative. Also, see this solution from Ben Nadel.

这是一个不需要单独模块的解决方案。但是,它非常粗糙,并没有实现太多的错误处理。它也可以使用更多的测试,但它会让你继续前进。如果您正在解析非常大的文件,您可能需要寻找替代方案。另外,请参阅Ben Nadel 的解决方案

Node Module Code, csv2json.js:

节点模块代码,csv2json.js:

/*
 * Convert a CSV String to JSON
 */
exports.convert = function(csvString) {
    var json = [];
    var csvArray = csvString.split("\n");

    // Remove the column names from csvArray into csvColumns.
    // Also replace single quote with double quote (JSON needs double).
    var csvColumns = JSON
            .parse("[" + csvArray.shift().replace(/'/g, '"') + "]");

    csvArray.forEach(function(csvRowString) {

        var csvRow = csvRowString.split(",");

        // Here we work on a single row.
        // Create an object with all of the csvColumns as keys.
        jsonRow = new Object();
        for ( var colNum = 0; colNum < csvRow.length; colNum++) {
            // Remove beginning and ending quotes since stringify will add them.
            var colData = csvRow[colNum].replace(/^['"]|['"]$/g, "");
            jsonRow[csvColumns[colNum]] = colData;
        }
        json.push(jsonRow);
    });

    return JSON.stringify(json);
};

Jasmine Test, csv2jsonSpec.js:

茉莉花测试,csv2jsonSpec.js:

var csv2json = require('csv2json.js');

var CSV_STRING = "'col1','col2','col3'\n'1','2','3'\n'4','5','6'";
var JSON_STRING = '[{"col1":"1","col2":"2","col3":"3"},{"col1":"4","col2":"5","col3":"6"}]';

/* jasmine specs for csv2json */
describe('csv2json', function() {

    it('should convert a csv string to a json string.', function() {
        expect(csv2json.convert(CSV_STRING)).toEqual(
                JSON_STRING);
    });
});

回答by userFog

Had to do something similar, hope this helps.

不得不做类似的事情,希望这会有所帮助。

// Node packages for file system
var fs = require('fs');
var path = require('path');


var filePath = path.join(__dirname, 'PATH_TO_CSV');
// Read CSV
var f = fs.readFileSync(filePath, {encoding: 'utf-8'}, 
    function(err){console.log(err);});

// Split on row
f = f.split("\n");

// Get first row for column headers
headers = f.shift().split(",");

var json = [];    
f.forEach(function(d){
    // Loop through each row
    tmp = {}
    row = d.split(",")
    for(var i = 0; i < headers.length; i++){
        tmp[headers[i]] = row[i];
    }
    // Add object to list
    json.push(tmp);
});

var outPath = path.join(__dirname, 'PATH_TO_JSON');
// Convert object to string, write json to file
fs.writeFileSync(outPath, JSON.stringify(json), 'utf8', 
    function(err){console.log(err);});

回答by theseadroid

Using lodash:

使用lodash

function csvToJson(csv) {
  const content = csv.split('\n');
  const header = content[0].split(',');
  return _.tail(content).map((row) => {
    return _.zipObject(header, row.split(','));
  });
}

回答by slobodan.blazeski

I haven't tried csv package https://npmjs.org/package/csvbut according to documentation it looks quality implementation http://www.adaltas.com/projects/node-csv/

我还没有尝试过 csv 包https://npmjs.org/package/csv但根据文档它看起来质量实现http://www.adaltas.com/projects/node-csv/

回答by xverges

I started with node-csvtojson, but it brought too many dependencies for my linking.

我从node-csvtojson开始,但它为我的链接带来了太多的依赖。

Building on your question and the answer by brnd, I used node-csvand underscore.js.

基于您的问题和brnd回答,我使用了node-csvunderscore.js

var attribs;
var json:
csv()
    .from.string(csvString)
    .transform(function(row) {
        if (!attribs) {
            attribs = row;
            return null;
        }
        return row;
     })
    .to.array(function(rows) {
        json = _.map(rows, function(row) {
            return _.object(attribs, row);
        });
     });

回答by Divyanshu Pandey

I have a very simple solution to just print json from csv on console using csvtojson module.

我有一个非常简单的解决方案,可以使用 csvtojson 模块在控制台上从 csv 打印 json。

// require csvtojson
var csv = require("csvtojson");

const csvFilePath='customer-data.csv' //file path of csv
csv()
.fromFile(csvFilePath)``
.then((jsonObj)=>{
    console.log(jsonObj);
})

回答by Abdennour TOUMI

Node-ETLpackage is enough for all BI processing.

Node-ETL包足以进行所有 BI 处理。

npm install node-etl; 

Then :

然后 :

var ETL=require('node-etl');
var output=ETL.extract('./data.csv',{
              headers:["a","b","c","d"],
              ignore:(line,index)=>index!==0, //ignore first line
 });

回答by francis

Using ES6

使用 ES6

const toJSON = csv => {
    const lines = csv.split('\n')
    const result = []
    const headers = lines[0].split(',')

    lines.map(l => {
        const obj = {}
        const line = l.split(',')

        headers.map((h, i) => {
            obj[h] = line[i]
        })

        result.push(obj)
    })

    return JSON.stringify(result)
}

const csv = `name,email,age
francis,[email protected],33
matty,[email protected],29`

const data = toJSON(csv)

console.log(data)

Output

输出

// [{"name":"name","email":"email","age":"age"},{"name":"francis","email":"[email protected]","age":"33"},{"name":"matty","email":"[email protected]","age":"29"}]