Convert CSV Data to JSON using JavaScript with Example

Today, We want to share with you csv to json javascript.In this post we will show you convert csv to json javascript online, hear for csv to json download we will give you demo and example for implement.In this post, we will learn about Convert CSV File Into JSON Using PHP with an example.

csv to json javascript

here in this Example You learn All About Example of python csv to json, csv to nested json, csv to json javascript npm with demo and full source code.

Step 1: Create CSV Convert Form HTML

Example: Convert CSV to JSON using JavaScript

Step 2: Convert CSV Data to JSON

$( document ).ready(function() {
	$("#convertCSV").click(function() {
		var productList = $("#productList").val();
		var memberJson = CSVToJSON(productList);
		$("#memberJson").val(memberJson);
	});
});

function CSVToJSON

function CSVToJSON(productList) {
    var data = CSVToArray(productList);
    var objData = [];
    for (var i = 1; i < data.length; i++) {
        objData[i - 1] = {};
        for (var k = 0; k < data[0].length && k < data[i].length; k++) {
            var key = data[0][k];
            objData[i - 1][key] = data[i][k]
        }
    }
    var memberJson = JSON.stringify(objData);
    memberJson = memberJson.replace(/},/g, "},\r\n");
    return memberJson;
}

function CSVToArray

function CSVToArray(productList, separator) {
    separator = (separator || ",");
     var pattern = new RegExp((
    "(\\" + separator + "|\\r?\\n|\\r|^)" +
    "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
    "([^\"\\" + separator + "\\r\\n]*))"), "gi");
    var data = [[]];
    var matches = null;
    while (matches = pattern.exec(productList)) {
        var matchedSeparator = matches[1];
        if (matchedSeparator.length && (matchedSeparator != separator)) {
            data.push([]);
        }
        if (matches[2]) {
            var matchedSeparator = matches[2].replace(
            new RegExp("\"\"", "g"), "\"");
        } else {
            var matchedSeparator = matches[3];
        }
        data[data.length - 1].push(matchedSeparator);
    }
    return (data);
}

I hope you get an idea about csv to javascript object.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment