Try it yourself with our free Json To Csv tool — runs entirely in your browser, no signup needed.

How to Convert CSV to JSON in TypeScript

How to Convert CSV to JSON in TypeScript

Converting CSV (Comma Separated Values) data to JSON (JavaScript Object Notation) is a common task in data processing and analysis. CSV is a widely used format for tabular data, while JSON is a popular format for data exchange and storage. In this article, we will explore how to convert CSV to JSON in TypeScript, a superset of JavaScript that adds optional static typing and other features.

Quick Example

Here is a minimal example of how to convert a CSV string to JSON in TypeScript:

import * as fs from 'fs';
import * as csv from 'csv-parser';

const csvString = 'Name,Age,Country\nJohn,25,USA\nAlice,30,UK';

const json = csvString
  .split('\n')
  .slice(1)
  .map((row) => row.split(','))
  .map((row) => ({ Name: row[0], Age: parseInt(row[1]), Country: row[2] }));

console.log(json);
// Output: [{ Name: 'John', Age: 25, Country: 'USA' }, { Name: 'Alice', Age: 30, Country: 'UK' }]

This code assumes you have the csv-parser package installed. You can install it by running npm install csv-parser or yarn add csv-parser.

Step-by-Step Breakdown

Let's break down the code step by step:

  • We import the fs module, which provides functions for interacting with the file system, and the csv module, which provides functions for parsing CSV data.
  • We define a CSV string with three columns: Name, Age, and Country.
  • We split the CSV string into an array of rows using the \n character as the delimiter.
  • We slice the array to exclude the header row (which contains the column names).
  • We map over the array of rows and split each row into an array of values using the , character as the delimiter.
  • We map over the array of values and create an object with three properties: Name, Age, and Country. We use the parseInt function to convert the Age value to a number.
  • Finally, we log the resulting JSON array to the console.

Handling Edge Cases

Here are some common edge cases to consider when converting CSV to JSON:

Empty/Null Input

If the input CSV string is empty or null, the code will throw an error. To handle this case, you can add a simple check:

if (!csvString) {
  console.log([]);
  return;
}

Invalid Input

If the input CSV string is invalid (e.g. it contains malformed rows or columns), the code may throw an error or produce incorrect results. To handle this case, you can use a try-catch block:

try {
  // parsing code here
} catch (error) {
  console.error('Error parsing CSV:', error);
  console.log([]);
}

Large Input

If the input CSV string is very large, the code may consume a lot of memory or take a long time to execute. To handle this case, you can use a streaming approach:

import * as fs from 'fs';
import * as csv from 'csv-parser';

const csvStream = fs.createReadStream('large.csv');
const jsonStream = csvStream.pipe(csv());

jsonStream.on('data', (row) => {
  console.log(row);
});

Unicode/Special Characters

If the input CSV string contains Unicode or special characters, the code may produce incorrect results. To handle this case, you can use a library like csv-parser which supports Unicode and special characters.

Common Mistakes

Here are some common mistakes developers make when converting CSV to JSON:

Mistake 1: Not Handling Edge Cases

// wrong code
const json = csvString.split('\n').map((row) => row.split(','));

This code does not handle edge cases like empty or null input, invalid input, or large input.

Mistake 2: Not Using a CSV Parser

// wrong code
const json = csvString.split('\n').map((row) => row.split(','));

This code does not use a CSV parser, which can lead to incorrect results or errors when dealing with complex CSV data.

Mistake 3: Not Using a Streaming Approach

// wrong code
const json = fs.readFileSync('large.csv', 'utf8').split('\n').map((row) => row.split(','));

This code reads the entire CSV file into memory, which can be inefficient for large files.

Performance Tips

Here are some performance tips for converting CSV to JSON in TypeScript:

Tip 1: Use a Streaming Approach

Use a streaming approach to process large CSV files, as shown in the example above.

Tip 2: Use a CSV Parser

Use a CSV parser like csv-parser to handle complex CSV data and avoid errors.

Tip 3: Use Buffer Instead of String

Use the Buffer type instead of String to improve performance when dealing with large CSV files.

FAQ

Q: What is the best way to handle errors when converting CSV to JSON?

A: Use a try-catch block to catch errors and log them to the console.

Q: How can I handle large CSV files?

A: Use a streaming approach to process large CSV files.

Q: What is the best way to handle Unicode and special characters?

A: Use a library like csv-parser which supports Unicode and special characters.

Q: Can I use this code to convert CSV to JSON in the browser?

A: No, this code is designed for Node.js and uses Node.js-specific APIs.

Q: How can I improve the performance of this code?

A: Use a streaming approach, use a CSV parser, and use Buffer instead of String.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp