Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Convert Data Between CSV And JSON With Simple JavaScript

A popular subject on the blog has always been around the conversion of one data format to another data format. For example we’ve already seen how to convert XML data to JSON data with JavaScript, but what if we wanted to work with comma separated value (CSV) data instead?

We’re going to see how to take a CSV file, parse it into JSON, make some changes, and then convert it back into a CSV file using Node.js and a few readily available packages.

Building a Node.js Application for CSV Data to JSON Data Parsing

The basis of this example is going to be simple. For this reason, we want to keep it simple by referencing a fresh Node.js project. Assuming you have Node.js installed, execute the following within a new directory:

npm init -y
npm install csvtojson --save
npm install json2csv --save

The above command will create a new project with the appropriate dependencies. We’re going to be using the csvtojson package to convert a CSV file to JSON and then we’re going to be using the json2csv package to convert JSON data to CSV data, but not necessarily write it to a file.

The next step is to create an app.js file to hold our project logic:

touch app.js

If you don’t have a touch command, go ahead and create the file manually. With the logic file in place, let’s create a sample dataset to work with. Create a file called source.csv that includes the following:

sku,title,hardware,price
12345,Hollow Knight,Nintendo Switch,15.99
35342,Dark Souls Remastered,Nintendo Switch,39.99

Our example data is simple product data with header information. If you want to use other data, feel free to change it up, but keep it simple while you’re learning.

Now open your project’s app.js file and include the following:

const CSVToJSON = require("csvtojson");
const JSONToCSV = require("json2csv").parse;
const FileSystem = require("fs");

CSVToJSON().fromFile("./source.csv").then(source => {
    source.push({
        "sku": "34890",
        "title": "Fortnite",
        "hardware": "Nintendo Switch",
        "price": "00.00"
    });
    var csv = JSONToCSV(source, { fields: ["sku", "title", "hardware", "price" ]});
    FileSystem.writeFileSync("./destination.csv", csv);
});

The above JavaScript is complete code, but we’re going to figure out what everything means. First we’re importing our dependencies, along with a file system dependency.

Using the csvtojson package, we can asynchronously load our source.csv file where the result is an array of objects. Since it is no fun just to convert and convert back, we’re going to change the data first. After we have JSON data, we’re going to add an object for another product.

When we’re ready to convert back to CSV format, we can use the json2csv package, providing our JSON array as well as the field information. The conversion back to CSV does not save it to disk, instead that is where the filesystem package comes into play. Using the CSV data, we can define an output file and write it to disk.

Conclusion

You just saw how to convert a comma separated value (CSV) file to JSON, make some manipulations, then write it back to disk as a CSV file, using simple JavaScript and a few packages. If you want to see a more realistic scenario which includes data processing from a remote API, check out my tutorial titled, Simple Data Processing with JavaScript and the HERE API.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Search

Follow Us

Subscribe

Subscribe to my newsletter for monthly tips and tricks on subjects such as mobile, web, and game development.

The Polyglot Developer
The Polyglot Developer

Support This Site

Close

Subscribe To Our Newsletter

Stay up to date on the latest in web, mobile, and game development, plus receive exclusive content by subscribing to The Polyglot Developer newsletter.

Unsubscribe at any time without hassle.