Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

json to csv javascript

<html>
<head>
    <title>Demo - Covnert JSON to CSV</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>

    <script type="text/javascript">
        // JSON to CSV Converter
        function ConvertToCSV(objArray) {
            var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
            var str = '';

            for (var i = 0; i < array.length; i++) {
                var line = '';
                for (var index in array[i]) {
                    if (line != '') line += ','

                    line += array[i][index];
                }

                str += line + '
';
            }

            return str;
        }

        // Example
        $(document).ready(function () {

            // Create Object
            var items = [
                  { name: "Item 1", color: "Green", size: "X-Large" },
                  { name: "Item 2", color: "Green", size: "X-Large" },
                  { name: "Item 3", color: "Green", size: "X-Large" }];

            // Convert Object to JSON
            var jsonObject = JSON.stringify(items);

            // Display JSON
            $('#json').text(jsonObject);

            // Convert JSON to CSV & Display CSV
            $('#csv').text(ConvertToCSV(jsonObject));
        });
    </script>
</head>
<body>
    <h1>
        JSON</h1>
    <pre id="json"></pre>
    <h1>
        CSV</h1>
    <pre id="csv"></pre>
</body>
</html>
Comment

js csv to json

let csvToJson = require('convert-csv-to-json');

let json = csvToJson.getJsonFromCsv("myInputFile.csv");
for(let i=0; i<json.length;i++){
    console.log(json[i]);
}
Comment

js csv file to json file

let csvToJson = require('convert-csv-to-json');

let fileInputName = 'myInputFile.csv'; 
let fileOutputName = 'myOutputFile.json';

csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName);
Comment

PREVIOUS NEXT
Code Example
Javascript :: gym open ai 
Javascript :: save file javascript 
Javascript :: try catch js 
Javascript :: sequelize transaction util 
Javascript :: run node script pupeeter when button from form clicked 
Javascript :: sequelize one to many 
Javascript :: javascript string 
Javascript :: where to find node js logs windows logging node.js howto 
Javascript :: NodeJS 10.24.1 
Javascript :: new array from javascript 
Javascript :: clearinterval in javascript 
Javascript :: angular 8 enable routing 
Javascript :: find object in array 
Javascript :: enzyme react 
Javascript :: get all files in directory recursively nodejs 
Javascript :: simple javascript 
Javascript :: apps script get last row with data 
Javascript :: delete message plugin discord 
Javascript :: javascript sort 
Javascript :: print() in javascript 
Javascript :: localstorage api 
Javascript :: from array create two arrayjavascript 
Javascript :: javascript clone element 
Javascript :: how to find element in array angularjs 
Javascript :: javascript for in 
Javascript :: address 
Javascript :: javascript regex match sequence 
Javascript :: adding methods to objects javascript 
Javascript :: get string length javascript 
Javascript :: csurf in express 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =