Post Details

How to Export JSON to CSV File using JQuery
04 Feb

How to Export JSON to CSV File using JQuery

Hi Guys,

In this tutorial will learn How to Export JSON to CSV files using JQuery. I will tell you the code for the best way to use Export JSON to CSV files using JQuery.

This model is centered around how to trade JSON to CSV utilizing jQuery. you can perceive how to change JSON objects to CSV in jQuery. you can comprehend the idea of jQuery JSON to CSV download. It's a straightforward illustration of how to trade JSON to CSV in jQuery. Thus, let us plunge into the subtleties.

I will show how to send out JSON information to a CSV document utilizing jQuery. The cycle includes utilizing Mass to make a CSV document in jQuery. The code given shows a straightforward method for switching JSON over completely to CSV utilizing jQuery.

Let's look at a quick example that should help:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Convert JSON to CSV using JQuery? - omcodingtutorial.com</title>
</head>
<body>
    <h1>How to Convert JSON to CSV using JQuery? - omcodingtutorial.com</h1>
    <a id="createCSVFile" download="file.csv">Download</a>
</body>
   
<script type="text/javascript">
    const data = [
        {
            "id": "1",
            "name": "test",
            "email": "test@gmail.com",
        },
        {
            "id": "2",
            "name": "test1",
            "email": "test1@gmail.com",
        },
        {
            "id": "3",
            "name": "test2",
            "email": "test2@gmail.com",
        }
    ];
  
    const keys = Object.keys(data[0]);
      
    const commaSeparatedString = [keys.join(",") , data.map(row => keys.map(key => row[key]).join(",")).join("\n")].join("\n");
  
    const csvBlob = new Blob([commaSeparatedString]);
  
    const createCSVFile = document.getElementById("createCSVFile");
  
    createCSVFile.href = URL.createObjectURL(csvBlob);
  
</script>
</html>

I hope it can help you guys...

0 Comments

Leave a Comment