- CSVtoJSON
- Prerequisites
- Install npm convert-csv-to-json package
- Install
- Usage
- Generate JSON file
- Generate Array of Object in JSON format
- Generate Object with sub array
- Define field delimiter
- Support Quoted Fields
- Index header
- Empty rows
- Format property value by type
- Encoding
- Development
- License
- Buy me a Coffee
- How to Convert CSV to JSON in JavaScript
- Install csvtojson
- Convert CSV file to JSON with fromFile()
- Convert CSV string to JSON with fromString()
- Customize conversion of CSV to JSON
- Convert CSV to JSON array of rows
- Native conversion of CSV to JSON
- Every Crazy Thing JavaScript Does
- Convert CSV to JSON in JavaScript
- Convert CSV to JSON in JavaScript
- Related Article — JavaScript CSV
- Related Article — JavaScript JSON
CSVtoJSON
Converts csv files to JSON files with Node.js.
first_name | last_name | gender | age | zip | registered | |
---|---|---|---|---|---|---|
Constantin | Langsdon | clangsdon0@hc360.com | Male | 96 | 123 | true |
Norah | Raison | nraison1@wired.com | Female | 32 | false |
first_name;last_name;email;gender;age;zip;registered Constantin;Langsdon;clangsdon0@hc360.com;Male;96;123;true Norah;Raison;nraison1@wired.com;Female;32;;false
[ < "first_name": "Constantin", "last_name": "Langsdon", "email": "clangsdon0@hc360.com", "gender": "Male", "age": "96", "zip": "123", "registered": "true" >, < "first_name": "Norah", "last_name": "Raison", "email": "nraison1@wired.com", "gender": "Female", "age": "32", "zip": "", "registered": "false" > ]
Prerequisites
Install npm convert-csv-to-json package
Install
Install package in your package.json
$ npm install convert-csv-to-json --save
Install package on your machine
$ npm install -g convert-csv-to-json
Usage
Generate JSON file
let csvToJson = require('convert-csv-to-json'); let fileInputName = 'myInputFile.csv'; let fileOutputName = 'myOutputFile.json'; csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName);
Generate Array of Object in JSON format
let csvToJson = require('convert-csv-to-json'); let json = csvToJson.getJsonFromCsv("myInputFile.csv"); for(let i=0; ijson.length;i++) console.log(json[i]); >
Generate Object with sub array
firstName;lastName;email;gender;age;birth;sons Constantin;Langsdon;clangsdon0@hc360.com;Male;96;10.02.1965;*diego,marek,dries*
Given the above CSV example, to generate a JSON Object with properties that contains sub Array, like the property sons with the values diego,marek,dries you have to call the function parseSubArray(delimiter, separator) . To generate the JSON Object with sub array from the above CSV example:
csvToJson.parseSubArray('*',',').getJsonFromCsv('myInputFile.csv');
[ < "firstName": "Constantin", "lastName": "Langsdon", "email": "clangsdon0@hc360.com", "gender": "Male", "age": "96", "birth": "10.02.1965", "sons": ["diego","marek","dries"] > ]
Define field delimiter
A field delimiter is needed to split the parsed values. As default the field delimiter is the semicolon (;), this means that during the parsing when a semicolon (;) is matched a new JSON entry is created. In case your CSV file has defined another field delimiter you have to call the function fieldDelimiter(myDelimiter) and pass it as parameter the field delimiter.
E.g. if your field delimiter is the comma , then:
csvToJson.fieldDelimiter(',').getJsonFromCsv(fileInputName);
Support Quoted Fields
To be able to parse correctly fields wrapped in quote, like the last_name in the first row in the following example:
first_name | last_name | |
---|---|---|
Constantin | «Langsdon,Nandson,Gangson» | clangsdon0@hc360.com |
you need to activate the support quoted fields feature:
csvToJson.supportQuotedField(true).getJsonFromCsv(fileInputName);
[ < "firstName": "Constantin", "lastName": "Langsdon,Nandson,Gangson", "email": "clangsdon0@hc360.com" > ]
Index header
If the header is not on the first line you can define the header index like:
csvToJson.indexHeader(3).getJsonFromCsv(fileInputName);
Empty rows
Empty rows are ignored and not parsed.
Format property value by type
If you want that a number will be printed as a Number type, and values true or false is printed as a boolean Type, use:
csvToJson.formatValueByType().getJsonFromCsv(fileInputName);
[ < "first_name": "Constantin", "last_name": "Langsdon", "email": "clangsdon0@hc360.com", "gender": "Male", "age": 96, "zip": 123, "registered": true >, < "first_name": "Norah", "last_name": "Raison", "email": "nraison1@wired.com", "gender": "Female", "age": 32, "zip": "", "registered": false > ]
Number
The property age is printed as
Boolean
The property registered is printed as
Encoding
You can read and decode files with the following encoding:
csvToJson.utf8Encoding().getJsonFromCsv(fileInputName);
csvToJson.ucs2Encoding().getJsonFromCsv(fileInputName);
csvToJson.utf16leEncoding().getJsonFromCsv(fileInputName);
csvToJson.latin1Encoding().getJsonFromCsv(fileInputName);
csvToJson.asciiEncoding().getJsonFromCsv(fileInputName);
csvToJson.base64Encoding().getJsonFromCsv(fileInputName);
csvToJson.hexEncoding().getJsonFromCsv(fileInputName);
Development
License
CSVtoJSON is licensed under the GNU General Public License v3.0 License.
Buy me a Coffee
Just if you want to support this repository:
How to Convert CSV to JSON in JavaScript
You can use the csvtojson library to quickly convert CSV to JSON in JavaScript:
index.js Copied!
import csvToJson from 'csvtojson'; const csvFilePath = 'data.csv'; const json = await csvToJson().fromFile(csvFilePath); const jsonString = JSON.stringify(json, null, 2) console.log(jsonString);
For a data.csv file like this:
data.csv Copied!
color,maxSpeed,age "red",120,2 "blue",100,3 "green",130,2
This will be the resulting JSON:
JSON Copied!
We use csvtojson to convert the CSV into a JSON object, and use the JSON.stringify() method to convert the object into a well-formatted JSON string.
Install csvtojson
Before using csvtojson , you’ll need to install it in our project. You can do this with the NPM or Yarn CLI.
Shell Copied!
npm i csvtojson # Yarn yarn add csvtojson
After installation, you’ll be able to import it into a JavaScript module, like this:
JavaScript Copied!
import csvToJson from 'csvtojson'; // CommonJS const csvToJson = require('csvtojson');
Convert CSV file to JSON with fromFile()
We call the default export function of the csvtojson module to create the object that will convert the CSV. This object has a bunch of methods, each related in some way to the conversion of CSV to JSON, and fromFile() is one of them.
It accepts the name of the CSV file to convert, and returns a Promise , as the conversion is an asynchronous process. The Promise will resolve with the resulting JSON string.
Convert CSV string to JSON with fromString()
To convert from a CSV data string directly, instead of a file, you can use the also asynchronous fromString() method of the converter object instead:
index.js Copied!
import csvToJson from 'csvtojson'; const csv = `"First Name","Last Name","Age" "Russell","Castillo",23 "Christy","Harper",35 "Eleanor","Mark",26`; const json = await csvToJson().fromString(csv); const jsonString = JSON.stringify(json, null, 2); console.log(jsonString);
JSON Copied!
Customize conversion of CSV to JSON
The default export function of csvtojson accepts an object used to specify options to customize the conversion process.
One such option is header , an array used to specify the headers in the CSV data.
index.js Copied!
import csvToJson from 'csvtojson'; const csv = `"First Name","Last Name","Age" "Russell","Castillo",23 "Christy","Harper",35 "Eleanor","Mark",26`; const json = await csvToJson(< headers: ['firstName', 'lastName', 'age'], >).fromString(csv); console.log(json);
JSON Copied!
Another is delimeter , used to indicate the character that separates the columns:
index.js Copied!
import csvToJson from 'csvtojson'; const csv = `"First Name"|"Last Name"|"Age" "Russell"|"Castillo"|23 "Christy"|"Harper"|35 "Eleanor"|"Mark"|26`; const json = await csvToJson(< headers: ['firstName', 'lastName', 'age'], delimiter: '|', >).fromString(csv); const jsonString = JSON.stringify(json, null, 2); console.log(jsonString);
JSON Copied!
We also have ignoreColumns , an option that instructs the converter to ignore certain columns, using a regular expression.
index.js Copied!
import csvToJson from 'csvtojson'; const csv = `"First Name"|"Last Name"|"Age" "Russell"|"Castillo"|23 "Christy"|"Harper"|35 "Eleanor"|"Mark"|26`; const json = await csvToJson(< headers: ['firstName', 'lastName', 'age'], delimiter: '|', ignoreColumns: /lastName/, >).fromString(csv); const jsonString = JSON.stringify(json, null, 2); console.log(jsonString);
JSON Copied!
Convert CSV to JSON array of rows
By setting the output option to ‘csv’ , we can produce a list of arrays, where each array represents a row, containing the value of all columns of that row.
index.js Copied!
import csvToJson from 'csvtojson'; const csv = `color,maxSpeed,age "red",120,2 "blue",100,3 "green",130,2`; const json = await csvToJson(< output: 'csv', >).fromString(csv); const jsonString = JSON.stringify(json, null, 2); console.log(jsonString);
JSON Copied!
[ [ "red", "120", "2" ], [ "blue", "100", "3" ], [ "green", "130", "2" ] ]
Native conversion of CSV to JSON
It’s also possible to convert CSV to JSON without using any third-party libraries.
index.js Copied!
function csvToJson(csv) < // \n or \r\n depending on the EOL sequence const lines = csv.split('\n'); const delimeter = ','; const result = []; const headers = lines[0].split(delimeter); for (const line of lines) < const obj = <>; const row = line.split(delimeter); for (let i = 0; i < headers.length; i++) < const header = headers[i]; obj[header] = row[i]; >result.push(obj); > // Prettify output return result; > const csv = `color,maxSpeed,age red,120,2 blue,100,3 green,130,2`; const json = csvToJson(csv); const jsonString = JSON.stringify(json, null, 2); console.log(jsonString);
You can modify the code above to allow for varying and more complex CSV data.
JSON Copied!
Every Crazy Thing JavaScript Does
A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
Convert CSV to JSON in JavaScript
A CSV is a comma-separated value file with a .csv extension that allows data to be stored in a tabular format. Today’s article will teach how to convert data from a CSV file to JavaScript Object Notation (JSON) without using a third-party npm package.
The main difference from the normal conversion is that commas can separate the values of each line, and as we know, the different columns are also separated by commas.
Convert CSV to JSON in JavaScript
- Read the CSV file with the default fs npm package. (It’s an optional step, you can directly provide the data in array format)
- Convert the data to a string and split it into an array.
- Create a separate array of headers.
- For each remaining data row, do the following:
4.1. Create an empty object to store resultant values from the current row.
4.2. Declare a string currentArrayString as the current array value to change the delimiter and store the generated string in a new string string .
4.3. If we encounter double-quotes ( » ), we keep the commas; otherwise, we replace them with a pipe | .
4.4. Continue adding the characters we’re looping through to strings.
4.5. Divide the string using the | and store the values in a property array.
4.6. If the value for each header contains multiple data separated by commas, we store it as an array; otherwise, the value is saved directly.
4.7. Add the generated object to our result array. - Convert the resulting array to JSON, print the data, or store it in a JSON file.
The following code shows the implementation of the mentioned above in JavaScript.
const fs = require("fs"); csv = fs.readFileSync("username.csv") const array = csv.toString().split("\n"); /* Store the converted result into an array */ const csvToJsonResult = []; /* Store the CSV column headers into seprate variable */ const headers = array[0].split(", ") /* Iterate over the remaning data rows */ for (let i = 1; i array.length - 1; i++) /* Empty object to store result in key value pair */ const jsonObject = <> /* Store the current array element */ const currentArrayString = array[i] let string = '' let quoteFlag = 0 for (let character of currentArrayString) if (character === '"' && quoteFlag === 0) quoteFlag = 1 > else if (character === '"' && quoteFlag == 1) quoteFlag = 0 if (character === ', ' && quoteFlag === 0) character = '|' if (character !== '"') string += character > let jsonProperties = string.split("|") for (let j in headers) if (jsonProperties[j].includes(", ")) jsonObject[headers[j]] = jsonProperties[j] .split(", ").map(item => item.trim()) > else jsonObject[headers[j]] = jsonProperties[j] > /* Push the genearted JSON object to resultant array */ csvToJsonResult.push(jsonObject) > /* Convert the final array to JSON */ const json = JSON.stringify(csvToJsonResult); console.log(json)
The output of the above code will vary depending on the input data provided.
[ "name": "John", "quote": "Hello World" >, "name": "Alma", "quote": "Have a nice day" > ]
Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.