Skip to content Skip to sidebar Skip to footer

How To Change Json Format For Load Data To Highchart

My JSON looks like this: [ { '1332879360000.0': 300, '1332797760000.0': 353, '1332799320000.0': 358, '1332879780000.0': 302, '1332800160

Solution 1:

json1 = [{"1332879360000.0": 300.0, "1332797760000.0": 353.0, "1332799320000.0": 358.0, "1332879780000.0": 302.0, "1332800160000.0": 359.0, "1332880200000.0": 299.0, "1332880620000.0": 298.0, "1332881040000.0": 301.0, "1332881460000.0": 402.0, "1332880020000.0": 330.0, "1332882300000.0": 466.0, "1332796620000.0": 519.0, "1332800520000.0": 447.0, "1332797460000.0": 359.0, "1332801000000.0": 442.0}];

hc = []

for(key in json1[0])
{
    hc.push([key,json1[0][key]])
}

json2 = JSON.stringify(hc)

json2 now contains your data the way you want it =)

Solution 2:

your can create a javascript array of arrays and place the values as follows:

var json = ... your json array// construct your java-script arrayvar value = newArray(json.length);

// populate javascript arrayfor (var n = 0; n < json.length; n++ ) {
   var obj = json[n];
   value[n] = newArray(2);
   for(var key in obj){
        value[0] = key;
        value[1] = obj[key];
   }
}

Solution 3:

You need to iterate through your JSON input and construct an Array dynamically. Then just use JSON.stringify() to get the desired output JSON.

var jsonIn ="[\r\n"+"    {\r\n"+"        \"1332879360000.0\": 300,\r\n"+"        \"1332797760000.0\": 353,\r\n"+"        \"1332799320000.0\": 358,\r\n"+"        \"1332879780000.0\": 302,\r\n"+"        \"1332800160000.0\": 359,\r\n"+"        \"1332880200000.0\": 299,\r\n"+"        \"1332880620000.0\": 298,\r\n"+"        \"1332881040000.0\": 301,\r\n"+"        \"1332881460000.0\": 402,\r\n"+"        \"1332880020000.0\": 330,\r\n"+"        \"1332882300000.0\": 466,\r\n"+"        \"1332796620000.0\": 519,\r\n"+"        \"1332800520000.0\": 447,\r\n"+"        \"1332797460000.0\": 359,\r\n"+"        \"1332801000000.0\": 442\r\n"+"    }\r\n"+"]";

var jsonArr = [];       
var jsonObj =JSON.parse( jsonIn )[0];

for( var key in jsonObj ) {
    jsonArr.push( [parseInt( key ), jsonObj[key]] );
}

var jsonOut =JSON.stringify( jsonArr ); 
console.log( jsonOut );

Output:

[[1332879360000,300],[1332797760000,353],[1332799320000,358],[1332879780000,302],[1332800160000,359],[1332880200000,299],[1332880620000,298],[1332881040000,301],[1332881460000,402],[1332880020000,330],[1332882300000,466],[1332796620000,519],[1332800520000,447],[1332797460000,359],[1332801000000,442]]

Solution 4:

Try this out:- http://jsfiddle.net/adiioo7/tcu8t/2/

JS:-

var oldFormat = [{
    "1332879360000.0": 300,
        "1332797760000.0": 353,
        "1332799320000.0": 358,
        "1332879780000.0": 302,
        "1332800160000.0": 359,
        "1332880200000.0": 299,
        "1332880620000.0": 298,
        "1332881040000.0": 301,
        "1332881460000.0": 402,
        "1332880020000.0": 330,
        "1332882300000.0": 466,
        "1332796620000.0": 519,
        "1332800520000.0": 447,
        "1332797460000.0": 359,
        "1332801000000.0": 442
}];

var newFormat = [];

jQuery(function ($) {
    $.each(oldFormat[0], function (i, item) {
        newFormat.push([parseInt(i), item]);
    });

    console.log(newFormat);
});

Post a Comment for "How To Change Json Format For Load Data To Highchart"