0

I have this valid JSON string and it can have many rows. I have just posted a fragment.

var jsonStr = ["{\"r1\":768,\"r2\":1,\"r3\":\"System Admin\",\"r4\":\"2013-06-08T05:51:23.000Z\",\"r5\":1,\"r6\":\"System Admin\",\"r7\":false}"]

I need to send the above to CSV. Follwed this Post

CSV gets exported with r1,r2,r3.. but i dont need the keys r1, r3 , r3.. etc . I just need the values . I followed the same example.

Any pointers ?

var jsonStr = ["{\"r1\":768,\"r2\":1,\"r3\":\"System Admin\",\"r4\":\"2013-06-08T05:51:23.000Z\",\"r5\":1,\"r6\":\"System Admin\",\"r7\":false}"]

DownloadJSON2CSV(jsonStr);

function DownloadJSON2CSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

    var str = '';
    for (var i = 0; i < array.length; i++) {
        var line = '';
        var tmp = 0;
        var nx = 1;

        for (var index in array[i]) {
            line += array[i][index];
            if (tmp == 0) {
                nx = array[0][3];
            }
            tmp++;
        }
        line.slice(0, line.Length - 1);
        str += line + '\r\n';

    }
    window.open("data:text/csv;charset=utf-8," + escape(str))
}

Here's the Fiddle : http://jsfiddle.net/ufjUY/

Community
  • 1
  • 1
  • 1
    There is no parsing of your string happening in your code. – Amberlamps Jul 19 '13 at 11:51
  • The code in the Post looks fine. What is the final value before the `window.open` statement ? – mohkhan Jul 19 '13 at 11:54
  • You need to include the brackets inside of the quotes: `var jsonStr = "[{\"r1\":768,\"r2\":1,\"r3\":\"System Admin\",\"r4\":\"2013-06-08T05:51:23.000Z\",\"r5\":1,\"r6\":\"System Admin\",\"r7\":false}]"` – Amberlamps Jul 19 '13 at 11:55
  • @JohnsonEddy: This is actually your solution! – Amberlamps Jul 19 '13 at 11:58
  • @Amberlamps, I did as you said. says invalid JSON string. – Johnson Eddy Jul 19 '13 at 11:59
  • http://jsfiddle.net/ufjUY/5/ Just made some edits in the fiddle. – Quicksilver Jul 19 '13 at 12:00
  • I agree with Amberlamps. You either want an array of stringified JSON objects in which case you need the for loop around JSON.parse or you want a single JSON string that contains an array of objects in which case you need to move the brackets [] inside the string. Look at this JSFiddle http://jsfiddle.net/ufjUY/7/ – Chris B Jul 19 '13 at 12:03
  • @JohnsonEddy: Have a look at Chris B´s edit please! – Amberlamps Jul 19 '13 at 12:06
  • @Amberlamps, I don't have the control over JSON data. Chris's fiddle shows all of them in a single cell and by the way the JSON data is modified. – Johnson Eddy Jul 19 '13 at 12:09

2 Answers2

0

Ok, since you don't have control over the JSON data I'm going to assume that you are given an Array of stringified JSON objects.

If this is the case have a look at this JSFiddle (http://jsfiddle.net/ufjUY/10/)

I'm not sure how you need the output formatted, so for the time being I separated them by commas in the output.

I also stripped out all of the unnecessary code found in the fiddle to simplify it.

var jsonStr = ["{\"r1\":768,\"r2\":1,\"r3\":\"System Admin\",\"r4\":\"2013-06-08T05:51:23.000Z\",\"r5\":1,\"r6\":\"System Admin\",\"r7\":false}"]

DownloadJSON2CSV(jsonStr);

function DownloadJSON2CSV(objArray)
{
    var str = '';
    for(var i = 0; i < objArray.length; i++)
    {
        var array = typeof objArray[i] != 'object' ? JSON.parse(objArray[i]) : objArray[i]; 
        var line = '';

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

        line.slice(0,line.Length-1); 
        str += line + '\r\n';
    }

    window.open( "data:text/csv;charset=utf-8," + escape(str))        
}

Hope this helps.

Chris B
  • 733
  • 3
  • 10
0

You can try the following:

var jsonStr = "[{\"r1\":768,\"r2\":1,\"r3\":\"System Admin\",\"r4\":\"2013-06-08T05:51:23.000Z\",\"r5\":1,\"r6\":\"System Admin\",\"r7\":false}]";

DownloadJSON2CSV(jsonStr);

function DownloadJSON2CSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        var tmp = 0;
        var nx=1;

        for (var index in array[i]) {
            switch( index ) {
                case "r1": 
                case "r2": 
                case "r5": line += array[i][index]; break;
                case "r7": line += (array[i][index]==false) ? -1 : 0; break;
                case "r3":
                case "r4":
                case "r6": line += '"'+array[i][index]+'"';
            }
            line += ",";
        }
        str += line + '\r\n';
    }
//        window.open( "data:text/csv;charset=utf-8," + escape(str))
//        document.getElementById('comments').innerHTML = nx;
}