SoFunction
Updated on 2025-03-02

The easiest way to implement JS to convert JS to csv

After working for a long time, you will always encounter various data processing tasks, such as synchronizing data and initializing some data. The most popular interactive data format is JSON, but if the JSON data obtained in the server is provided to business personnel for viewing, it may be very inconvenient. At this time, it is converted into a CSV file and can be read and written by Excel tools. In addition, after json converts to CSV, it will be easy to enter the database. This is also very useful. Of course, these software functions have been developed, but most of the codes on the Internet are relatively complex, and there are many online conversion tools. However, considering data security, try not to upload data to any irrelevant websites. You should know that if sensitive data leaks may be very unfavorable to you. Here is a relatively simple implementation method for everyone to use:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
  <title>JSON to CSV</title>
  
  <script type="text/javascript">
  var json3 = { "d": "[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName\":\"Fred Frankly\"},{\"Id\":1,\"UserName\":\"Zachary Zupers\"}]" }

  //var winners = '{}';
  var winnerObject = (json3);
  
  downloadJSON2CSV(winnerObject);

  function downloadJSON2CSV(objArray)
  {
    var array = typeof objArray != 'object' ? (objArray) : objArray;

    var str = '';

    for (var i = 0; i < ; i++) {
      var line = '';

      for (var index in array[i]) {
        line += array[i][index] + ',';
      }

      // Add double quotes      // for (var index in array[i]) {
      //  line += '"' + array[i][index] + '",';
      // }

      (0,-1); 

      str += line + '\r';
    }
    ( "data:text/csv;charset=utf-8," + str)
  }

  </script>

</head>
<body>
  <h1>This page onvert json to csv...</h1>
</body>
</html>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.