92

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

Example CSV:

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

Desired 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.

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

brnrd
  • 3,396
  • 3
  • 31
  • 41
Jetson John
  • 3,759
  • 8
  • 39
  • 53
  • http://apievangelist.com/2013/09/24/excel-and-csv-conversion-to-json-and-xml-in-javascript-that-runs-100-on-github/ and http://kinlane.github.io/csv-converter/ looks impressive. – VonC Sep 27 '13 at 09:22
  • I wrote a small blog post on a similar solution as proposed by brnrd: http://thinkingonthinking.com/scripting-a-csv-converter/ – poseid May 01 '14 at 08:37

21 Answers21

121

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

the source code can be found at: 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.

The detailed documentation can be found here

Here are some code examples:

Use it as a library in your Node.js application (csvtojson@2.0.0 +):

  1. Install it through npm

npm install --save csvtojson@latest

  1. Use it in your node.js app:
// 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.

Keyang
  • 1,828
  • 1
  • 12
  • 17
  • Code has been added. See more detailed documentation here https://github.com/Keyang/node-csvtojson – Keyang Jun 20 '13 at 10:24
  • Since version 0.3.0, csvtojson does not depend on any other lib. It will behave like a proper Stream object. – Keyang Apr 23 '14 at 21:19
  • The link to the blog is dead. –  Oct 27 '14 at 18:35
  • I don't know if this is happening only to me, but for a large CSV file this is to slow. Like 10 seconds slower than d3 – limoragni Jul 17 '15 at 14:48
  • for ppl like me who are looking out for simpler version to read from web url, csv() .fromStream(request.get('https://example.com/test.csv')) .then((jsonArray) => { console.log(util.inspect(jsonArray)); }, (err)=>{ console.log(err); }); – Vikas Putcha Dec 31 '19 at 18:06
  • what if the file is uploaded by the user? I have the file in the ' file ' variable and I want to convert it to JSON. How will I do that with csvtojson package ? – Himanshu Tariyal May 29 '21 at 16:21
  • @HimanshuTariyal add this line `const csvFilePath = ` underneath `var csv = require("csvtojson");` – Green Jun 07 '22 at 17:51
24

You can try to use underscore.js

First convert the lines in arrays using the toArray function :

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

Then object the arrays together using the object function :

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

By then, the json var should contain something like :

{"a": 1,"b": 2,"c": 3,"d": 4}
brnrd
  • 3,396
  • 3
  • 31
  • 41
21

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);});
userFog
  • 10,685
  • 1
  • 15
  • 7
13

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.

Node Module Code, 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:

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);
    });
});
Jess
  • 23,901
  • 21
  • 124
  • 145
6

If you want just a command line converter, the quickest and most clean solution for me is to use csvtojson via npx (included by default in node.js)

$ npx csvtojson ./data.csv > data.json

VanDavv
  • 836
  • 2
  • 13
  • 38
6

Using 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,francis@gmail.com,33
matty,mm@gmail.com,29`

const data = toJSON(csv)

console.log(data)

Output

// [{"name":"name","email":"email","age":"age"},{"name":"francis","email":"francis@gmail.com","age":"33"},{"name":"matty","email":"mm@gmail.com","age":"29"}]
francis
  • 3,852
  • 1
  • 28
  • 30
5

Using lodash:

function csvToJson(csv) {
  const content = csv.split('\n');
  const header = content[0].split(',');
  return _.tail(content).map((row) => {
    return _.zipObject(header, row.split(','));
  });
}
theseadroid
  • 471
  • 5
  • 19
  • 1
    A tweak: I added .map(function(str) { return _.trim(str, '"') to remove any double-quotes from the headers and data items, ie `const header = content[0].split(',').map(function(str) { return _.trim(str, '"'); });` and `return _.zipObject(header, row.split(',').map(function(str) { return _.trim(str, '"'); }));` – CharlesA Feb 02 '20 at 01:04
3

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

slobodan.blazeski
  • 1,040
  • 9
  • 20
3

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

Building on your question and the answer by brnd, I used node-csv and underscore.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);
        });
     });
Community
  • 1
  • 1
xverges
  • 4,608
  • 1
  • 39
  • 60
  • async and underscore was too much for you? – Spencer Oct 12 '15 at 18:32
  • 3
    @Spencer, at the time that I posted, the dependencies were different: https://github.com/Keyang/node-csvtojson/blob/288d1cca1b9f2864acd688d13217c68f3cef68f8/package.json Pulling in express for a csv conversion felt unnatural – xverges Oct 12 '15 at 20:25
  • Oh, yeah that is a crazy dependency. My bad. – Spencer Oct 12 '15 at 20:42
3

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

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

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

Node-ETL package is enough for all BI processing.

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
 });
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • The link to this library is dead - perhaps it was moved to somewhere else on Github (or forked?). Please update link. – Max Feb 13 '19 at 19:54
  • Thank you @RohitParte . This is one of my first modules in NodeJs. While some features work fine, it is missing a lot of features. I become extremely busy with other things (Reliability Engineering, DevOps, .. so on). – Abdennour TOUMI Jun 01 '19 at 10:01
1

I have used csvtojson library for converting csv string to json array. It has variety of function which can help you to convert to JSON.
It also supports reading from file and file streaming.

Be careful while parsing the csv which can contain the comma(,) or any other delimiter . For removing the delimiter please see my answer here.

Supermacy
  • 1,429
  • 15
  • 24
1

Step 1:

Install node module: npm install csvtojson --save

Step 2:

var Converter = require("csvtojson").Converter;

var converter = new Converter({});

converter.fromFile("./path-to-your-file.csv",function(err,result){

    if(err){
        console.log("Error");
        console.log(err);  
    } 
    var data = result;

    //to check json
    console.log(data);
});
Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51
1

npm install csvjson --save
In you Node JS File

const csvjson = require('csvjson');
convertCSVToJSON(*.csv);

convertCSVToJSON = (file) => {
  const convertedObj = csvjson.toObject(file);
}
0

Me and my buddy created a web service to handle this kind of thing.

Check out Modifly.co for instructions on how to transform CSV to JSON with a single RESTful call.

marty
  • 486
  • 5
  • 18
0

Use csv parser library, I'm explaining in more details how to use it here .

var csv = require('csv');
csv.parse(csvText, {columns: true}, function(err, data){
    console.log(JSON.stringify(data, null, 2));
});
Community
  • 1
  • 1
ganqqwerty
  • 1,894
  • 2
  • 23
  • 36
0

csvtojson module is a comprehensive nodejs csv parser to convert csv to json or column arrays. It can be used as node.js library / command line tool / or in browser. Below are some features:

/** csv file
a,b,c
1,2,3
4,5,6
*/
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
    console.log(jsonObj);
    /**
     * [
     *  {a:"1", b:"2", c:"3"},
     *  {a:"4", b:"5". c:"6"}
     * ]
     */ 
})
 
// Async / await usage
const jsonArray=await csv().fromFile(csvFilePath);
MD SHAYON
  • 7,001
  • 45
  • 38
0

I converted a large (315 MB) csv file to json by installing the csvtojson module and then using the below code:

const fs = require('fs')
const Converter = require('csvtojson').Converter
const csvConverter = new Converter({
    constructResult:false,
    downstreamFormat:"array",
})

csvConverter.subscribe=function(json,row,index){
    json["rowIndex"]=index
};

const readStream = fs.createReadStream('./data.csv') // my csv file
const writeStream = fs.createWriteStream('./data.json') // my new json file

readStream.pipe(csvConverter).pipe(writeStream)

The resulting json file is in the desired format:

[
{"a": 1,"b": 2,"c": 3,"d": 4},
{"a": 5,"b": 6,"c": 7,"d": 8},
]
Green
  • 507
  • 10
  • 20
0

Once figured out how to csv data into two dimention array:

[['header1','header2'],['data1','data2']]

Convert to json is simply map and reduce:

const keys = input[0]
const jsonOutput = input.slice(1)
  .map(arr2 => keys.reduce((accumulator, element, index) => {
    return { ...accumulator,
      [element]: arr2[index]
    };
  }, {}))

Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17
Andy
  • 39
  • 4
0

In my case JSON.stringify didn't help as the files where too big. This solved my needs:

let csvFile = fs.readFileSync(
  csvFilePath,
  { encoding: "utf-8" },
  function (err) {
    console.log(err);
  }
);
csvFile = csvFile.split("\n");

let strFile = "export default [";
csvFile.forEach(function (d) {
  let row = d.split(",");
  strFile += `[${row}],`;
});
strFile += "]";
domSurgeon
  • 91
  • 1
  • 3
0

2023 Answer - Works for CSV files with linebreaks inside fields, and with separators (commas) inside fields aswell

I decided to write my own function for the following reasons:

  • csvtojson hasn't been updated since June 7th 2021, and contains calls to substr, which is deprecated and might fail on constrained environments.
  • plain JS functions available in the answers wouldn't work against my dataset, because it contains both commas inside fields and linebrakes inside fields, which are both supported by RFC 4180
  • Update: I realized my dataset also contained double quotes inside a field, which is valid if they are escaped with another double quote, so I updated my answer to account for it aswell. Example of valid double quotes: "Name ""Nickname"" LastName"
import fs from "fs";

function readCSV(filepath, separator = ",") {
  /** Reads a csv file, taking into consideration linebreaks inside of fields, and double quotes or no quotes.
   * Converts it into a json object
   */
  const fp = new URL(filepath, import.meta.url);
  const file = fs.readFileSync(fp, { encoding: "utf-8" });

  // Figure out how many cells there are by counting the first line. 
  // ATTENTION: If your header contains commas or a linebreak, this will fail.
  const firstLineBreak = file.indexOf("\n");
  const rowsNum = file.slice(0, firstLineBreak).split(",").length;

  // Construct a regex based on how many headers there are
  const singleCellRegex = `(?:(?:"([\\s\\S]*?)")|((?:(?:[^"${separator}\\n])|(?:""))+))`;
  let regexText = "";

  for (let i = 0; i < rowsNum; i++) {
    regexText += "," + singleCellRegex;
  }

  const regex = new RegExp(regexText.slice(1), "g");
  const results = file.matchAll(regex);

  const rowsArr = [];
  for (const row of results) {
    const newRow = [];

    for (let i = 0; i < rowsNum; i++) {
      const rowValue = row[2 * i + 1] ?? row[2 * i + 2];
      newRow.push(rowValue.replaceAll('""', '"')); // Remove double double quotes
    }

    rowsArr.push(newRow);
  }

  const headers = rowsArr[0];
  const rows = rowsArr.slice(1);

  return rows.map((row) =>
    row.reduce((jsonRow, field, idx) => {
      jsonRow[headers[idx]] = field;
      return jsonRow;
    }, {})
  );
}
Gabriel d'Agosto
  • 416
  • 3
  • 12