Sling Academy
Home/Node.js/Node.js: How to Convert CSV to JSON

Node.js: How to Convert CSV to JSON

Last updated: December 30, 2023

Overview

Learn how to effortlessly transform CSV data into JSON format using Node.js, utilizing modern JavaScript syntax and libraries to streamline the process.

Before diving into the code examples, ensure you have Node.js installed. Basic knowledge of npm, asynchronous programming in JavaScript, and handling filesystems with Node.js is also recommended.

Basic Conversion

Understand the simplest method for CSV to JSON conversion using core Node.js modules.

const fs = require('fs');
const { parse } = require('csv-parse/sync');

const csvData = fs.readFileSync('data.csv', 'utf8');
const jsonData = parse(csvData, { columns: true, skip_empty_lines: true });
console.log(jsonData);

Streaming Data

A guide to converting large CSV files into JSON format using Node.js streams, to handle data efficiently and effectively.

const fs = require('fs');
const { parse } = require('csv-parse');
const { Transform } = require('stream');

const inputStream = fs.createReadStream('largeData.csv', 'utf8');
const outputStream = fs.createWriteStream('output.json');
const jsonTransform = new Transform({
  writableObjectMode: true,
  transform(chunk, encoding, callback) {
    // Transform CSV to JSON here
    this.push(JSON.stringify(chunk));
    callback();
  },
});

inputStream
  .pipe(parse({ columns: true }))
  .pipe(jsonTransform)
  .pipe(outputStream);

Using Third-Party Libraries

Explore using popular npm packages for an efficient and powerful CSV to JSON conversion process.

import fs from 'fs';
import csvtojson from 'csvtojson';

const readStream = fs.createReadStream('data.csv');
const writeStream = fs.createWriteStream('data.json');

readStream.pipe(csvtojson()).pipe(writeStream);

Advanced Parsing

Delve into advanced parsing techniques, including custom value transformations and error handling for robust CSV to JSON conversion.

import { pipeline } from 'stream';
import through2 from 'through2';
import csvtojson from 'csvtojson';

pipeline(
  fs.createReadStream('data.csv'),
  csvtojson().on('error', handleError),
  through2.obj(function (chunk, enc, callback) {
    // Perform any data transformation here
    callback(null, transformChunk(chunk));
  }),
  fs.createWriteStream('data.json'),
  (err) => {
    if (err) console.log('Pipeline failed.', err);
    else console.log('Pipeline succeeded.');
  }
);

function transformChunk(chunk) {
  // Define transformations here
  return chunk;
}

function handleError(error) {
  // error handling process
}

Asynchronous Conversion

Discover how to leverage async/await for converting CSV files to JSON asynchronously, enhancing performance and coding experience.

import fsPromises from 'fs/promises';
import parseAsync from 'csv-parse/lib/es5/sync';

async function convertCsvToJson(filePath) {
  const csvData = await fsPromises.readFile(filePath, 'utf8');
  return parseAsync(csvData, {
    columns: true,
    skip_empty_lines: true,
  });
}

convertCsvToJson('data.csv')
  .then((jsonData) => console.log(jsonData))
  .catch((error) => console.error(error));

Conclusion

In this tutorial, we explored a range of techniques for converting CSV files to JSON in Node.js, using both native modules and third-party libraries. Ensure that you select the method best suited to the size of your dataset and the complexity of your data transformation needs.

Next Article: Node.js: How to convert images to byte arrays

Previous Article: Node.js: How to programmatically run Git commands

Series: Node.js Intermediate Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates