Home » Convert CSV Data to JSON using JavaScript with Example

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.

Read Also:  How to get nth-child Selector using Jquery

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:  AngularJS GET Data in Ajax example using $http

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Curl GET And POST Method Calls – PHP Example
  2. Read Also:  Get last inserted ID laravel 5.7 Examples
  3. Read Also:  Simple PHP Convert Object To An Array Examples
  4. Read Also:  Vue JS Axios Post Request using Laravel Example
  5. Read Also:  Simple C Programming Structure Examples
  6. Read Also:  Mifflin-St Jeor Equation BMR Calculator
  7. Read Also:  Jquery .next() and .prev() functions example
  8. Read Also:  Fresher PHP WEB Developer Salary Range Level
  9. Read Also:  Simple PHP Convert Object To An Array Examples
  10. Read Also:  AngularJS GET Data in Ajax example using $http

Leave a Comment

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