How to convert JSON to CSV format and store in a variable How to convert JSON to CSV format and store in a variable json json

How to convert JSON to CSV format and store in a variable


A more elegant way to convert json to csv is to use the map function without any framework:

var json = json3.itemsvar fields = Object.keys(json[0])var replacer = function(key, value) { return value === null ? '' : value } var csv = json.map(function(row){  return fields.map(function(fieldName){    return JSON.stringify(row[fieldName], replacer)  }).join(',')})csv.unshift(fields.join(',')) // add header column csv = csv.join('\r\n');console.log(csv)

Output:

title,description,link,timestamp,image,embed,language,user,user_image,user_link,user_id,geo,source,favicon,type,domain,id"Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)","Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone","http://wik.io/info/US/309201303","1326439500","","","","","","","","","wikio","http://wikio.com/favicon.ico","blogs","wik.io","2388575404943858468""Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)","SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone","http://wik.io/info/US/309198933","1326439320","","","","","","","","","wikio","http://wikio.com/favicon.ico","blogs","wik.io","16209851193593872066"

Update ES6 (2016)

Use this less dense syntax and also JSON.stringify to add quotes to strings while keeping numbers unquoted:

const items = json3.itemsconst replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values hereconst header = Object.keys(items[0])const csv = [  header.join(','), // header row first  ...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))].join('\r\n')console.log(csv)


Ok I finally got this code working:

<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 + '\r\n';            }            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>

Thanks alot for all the support to all the contributors.

Praney


Very nice solution by praneybehl, but if someone wants to save the data as a csv file and using a blob method then they can refer this:

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;    var CSV = '';    //This condition will generate the Label/Header    if (ShowLabel) {        var row = "";        //This loop will extract the label from 1st index of on array        for (var index in arrData[0]) {            //Now convert each value to string and comma-seprated            row += index + ',';        }        row = row.slice(0, -1);        //append Label row with line break        CSV += row + '\r\n';    }    //1st loop is to extract each row    for (var i = 0; i < arrData.length; i++) {        var row = "";        //2nd loop will extract each column and convert it in string comma-seprated        for (var index in arrData[i]) {            row += '"' + arrData[i][index] + '",';        }        row.slice(0, row.length - 1);        //add a line break after each row        CSV += row + '\r\n';    }    if (CSV == '') {        alert("Invalid data");        return;    }    //this trick will generate a temp "a" tag    var link = document.createElement("a");    link.id = "lnkDwnldLnk";    //this part will append the anchor tag and remove it after automatic click    document.body.appendChild(link);    var csv = CSV;    blob = new Blob([csv], { type: 'text/csv' });    var csvUrl = window.webkitURL.createObjectURL(blob);    var filename =  (ReportTitle || 'UserExport') + '.csv';    $("#lnkDwnldLnk")        .attr({            'download': filename,            'href': csvUrl        });    $('#lnkDwnldLnk')[0].click();    document.body.removeChild(link);}