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 :: javascript console log 
Javascript :: javascipt async 
Javascript :: online password generator 
Javascript :: document.getelementsbyname 
Javascript :: typescript jsdoc interface 
Javascript :: express formidable 
Javascript :: ajax returning html instead of json 
Javascript :: eslint ignore javascript 
Javascript :: document.ready javascript 
Javascript :: React timeago 
Javascript :: storybook global decorator 
Javascript :: do while in js 
Javascript :: Simplest Promise Example 
Javascript :: return the first matching object from an array 
Javascript :: button click 
Javascript :: display component in popup angular 8 
Javascript :: Passing a state as a prop in react 
Javascript :: javascript function hoisting 
Javascript :: can i use splice in string of javascript 
Javascript :: javascript reversing an array 
Javascript :: window parent frame 
Javascript :: change direction in material ui 
Javascript :: double exclamation mark js 
Javascript :: adding more than one class react 
Javascript :: Format javascript date with date.js library 
Javascript :: SyntaxError: Unexpected token F in JSON at position 0 
Javascript :: addeventlistener 
Javascript :: splice method js 
Javascript :: prisma database example 
Javascript :: javascript loading animation on button click 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =