I have the following array:
var res = {
"status": "Success",
"data": [
{"assignedTo":"0", "createdDate":"23-07-2013", "count":"2"},
{"assignedTo":"182398", "createdDate":"01-08-2013", "count":"2"},
{"assignedTo":"182398", "createdDate":"23-07-2013", "count":"2"},
{"assignedTo":"182398", "createdDate":"24-07-2013", "count":"12"},
{"assignedTo":"182398", "createdDate":"22-07-2013", "count":"1"},
{"assignedTo":"182398", "createdDate":"30-07-2013", "count":"4"},
{"assignedTo":"182398", "createdDate":"31-07-2013", "count":"19"},
{"assignedTo":"185271", "createdDate":"24-07-2013", "count":"2"},
{"assignedTo":"185271", "createdDate":"23-07-2013", "count":"1"}
]
}
Now I want to make one json array from the above with the value of data to another json
which will be like:
[
{
key: "0",
values: [["23-07-2013", 2]]
},
{
key: "182398",
values: [["01-08-2013", 2],
["23-07-2013", 2],
["24-07-2013", 12],
["22-07-2013", 1],
["30-7-2013", 4],
["31-7-2013", 19]
},
{
key: "185271",
values: [["24-07-2013", 2],
["23-07-2013", 1]
}
]
I have tried like the following:
for (i in res.data) {
for (k in res.data[i]) {
time_val += "[" + res.data[i]['createdDate'] + ","
+ res.data[i]['count'] + "],";
cumulative_val += '{key:"' + res.data[i]['assignedTo']
+ '",values:'+time_val+'},';
}
}
Could you please guide me how to do this? Thanks in advance.
JSON.stringify. Building the JSON manually (like you do) is error prone. For example, keys must be in double quotes in JSON, and you havekey:instead of"key":in your string. If you don't want to create JSON at all, but just convert your object into an array of objects, then say so. But that has nothing to do with JSON then. Also, what's the problem with the code you have? Please explain.