简体   繁体   中英

Change format of JSON using Javascript

Alright, so in my code I grab a JSON blob that looks like this:

[
    {
        "ts": 1431736740,
        "aggs": {
            "DNS": {
                "min": 20,
                "max": 21,
            },
            "SEND": {
                "min": 102,
                "max": 8114,
            },
            "SSL": {
                "min": 110,
                "max": 7806,
            },
            "SERVERBUSY": {
                "min": 1360,
                "max": 13709,
            }
        }
    },
    {
        "ts": 1431736680,
        "aggs": {
            "DNS": {
                "min": 22,
                "max": 24,
            },
            "SEND": {
                "min": 210,
                "max": 8251,
            },
            "SSL": {
                "min": 117,
                "max": 12488,
            },
            "SERVERBUSY": {
                "mn": 6462,
                "max": 9800,
            }
        }
    },
    {
        "ts": 1431736620,
        "aggs": {
            "DNS": {
                "min": 21,
                "max": 22,
            },
            "SEND": {
                "min": 92,
                "max": 12035,
            },
            "SSL": {
                "min": 111,
                "max": 9758,
            },
            "SERVERBUSY": {
                "min": 9855,
                "max": 14112,
            }
        }
    }
]

and I need help converting it into one that looks like this:

[
    {
        "key": "DNS",
        "values": [
            [
                0,   //The first number just goes up by one.
                20   //The second number is the value of aggs.DNS.min
            ],
            [
                1,
                22
            ],
            [
                2,
                21
            ]
        ]
    },
    {
        "key": "SEND",
        "values": [
            [
                0,
                102
            ],
            [
                1,
                210
            ],
            [
                2,
                92
            ]
        ]
    },
    {
        "key": "SSL",
        "values": [
            [
                0,
                110
            ],
            [
                1,
                117
            ],
            [
                2,
                111
            ]
        ]
    },
    {
        "key": "SERVERBUSY",
        "values": [
            [
                0,
                1360
            ],
            [
                1,
                6462
            ],
            [
                2,
                9855
            ]
        ]
    }
]

Because I'm using a library that only uses JSONs in that correct format. Is this possible? I have found a few different questions that show you can change JSON format, but I'm not sure if it can be done that dramatically. Any help is much appreciated!!!

Alright, I wanted to post what I figured out in case it helps anyone else:

var pagestatsObject = {};

var dnsValues = [];
for(var i=0; i<bardata.length; i++){
    var dnsItem = bardata[i].aggs.dns.avg;
    dnsValues.push([i, dnsItem]);
}


var connectValues = [];
for(var i=0; i<bardata.length; i++){
    var connectItem = bardata[i].aggs.con.avg;
    connectValues.push([i, connectItem]);
}


var SSLValues = [];
for(var i=0; i<bardata.length; i++){
    var SSLItem = bardata[i].aggs.ssl.avg;
    SSLValues.push([i, SSLItem]);
}


var sendValues = [];
for(var i=0; i<bardata.length; i++){
    var sendItem = bardata[i].aggs.snd.avg;
    sendValues.push([i, sendItem]);
}


var serverBusyValues = [];
for(var i=0; i<bardata.length; i++){
    var serverBusyItem = bardata[i].aggs.srvbsy.avg;
    serverBusyValues.push([i, serverBusyItem]);
}


var receiveValues = [];
for(var i=0; i<bardata.length; i++){
    var receiveItem = bardata[i].aggs.rcv.avg;
    receiveValues.push([i, receiveItem]);
}


pagestatsObject = [ 
    {"key" : "DNS", "values" : dnsValues}, 
    {"key" : "Connect", "values" : connectValues},
    {"key" : "SSL", "values" : SSLValues},
    {"key" : "Send", "values" : sendValues},
    {"key" : "Server Busy", "values" : serverBusyValues},
    {"key" : "Receive", "values" : receiveValues}
];

It's not the prettiest solution but it works for what I needed it for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM