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

<div class="container">
	<h2>Example: Convert CSV to JSON using JavaScript</h2>	
	<div class="form-group">
		<label for="productList">Paste CSV Data:</label>
		<textarea id="productList" class="form-control" rows="6"></textarea>
	</div>
	<div class="form-group">
		<label for="memberJson">Member JSON Data Output:</label>
		<textarea id="memberJson" class="form-control" rows="6"></textarea>
	</div>    
    <button id="convertCSV" class="btn btn-success">Convert to JSON</button>    
</div>

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.

Read Also:  Laravel Get Current DateTime Handling in PHP with Carbon

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *