How to Convert Json to CSV Using Javascript?

Jun 07, 2024 | jQuery Javascript


Hello Dev,

This example demonstrates how to export JSON to CSV using jQuery. You'll learn how to convert a JSON object to CSV using JavaScript, focusing on the concept of jQuery JSON to CSV download. It's a straightforward example illustrating how to export JSON data to CSV in JavaScript. Let's delve into the specifics.

I'll guide you through exporting JSON data to a CSV file using jQuery. The process utilizes Blob to generate a CSV file in JavaScript. The code presented offers a simple method to convert JSON to CSV using JavaScript.

index.html Read Also: How to Use Dynamic Carousel Slider in Laravel?
<!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 Javascript? -- ItErrorSolution.com</title>
</head>
<body>
    <h1>How to Convert Json to CSV Using Javascript? -- ItErrorSolution.com</h1>
    <a id="createCSVFile" download="file.csv">Download</a>
</body>
   
<script type="text/javascript">
    const data = [
        {
            "id": "1",
            "name": "Admin",
            "email": "admin@gmail.com",
        },
        {
            "id": "2",
            "name": "User",
            "email": "user@gmail.com",
        },
        {
            "id": "3",
            "name": "Viewr",
            "email": "viewr@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>

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



Tags :
#jQuery
#Javascript
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"