

The csvtojson module also supports the async-await syntax. Read this guide to learn more about writing JSON to a file in a Node.js application. Open the app.js file in your favorite editor and add the following code:Īpp.js // require csvtojson module const CSVToJSON = require ( 'csvtojson' ) // convert users.csv file to JSON array CSVToJSON ( ). If the touch command is unavailable, simply create the file manually. To convert the above CSV file to JSON, create a new JavaScript file named app.js in the root directory: $ touch app.js The above command will create a node_modules directory inside your project containing all the dependencies that csvtojson requires.įinally, create a sample CSV file called users.csv with the following content in the root directory: Next, install the csvtojson module by typing the following command: $ npm install csvtojson -save This will initialize a new Node.js project and create a package.json file in the root directory.

Navigate to the folder where you want to store your project and execute the following command in the terminal: $ npm init -y Let us first create a new Node.js application and install all the required dependencies. This package is a Node.js CSV parser to convert CSV to JSON or column arrays. In order to do the CSV to JSON conversion, we'll be using the csvtojson package from NPM. We'll write a Node.js script that takes a CSV file as input and outputs its contents as a JSON array. In this article, you'll learn how to parse and convert a CSV file into a JSON array in a Node.js application. Due to simplicity and better integration with existing applications, CSV is widely used for exporting and importing large data sets. CSV (Comma-Separated Values) is a popular data exchange format that stores data in a tabular format where each row consists of one or more fields and each column represents a specific field.
