I have a simple set of JSON that needs to be reformatted since all of the key value pairs are not always output.
{
"result": [
{
"category": "Negative Notification",
"event": "open",
"result": 2
},
{
"category": "Referral",
"event": "bounce",
"result": 1
},
{
"category": "Negative Notification",
"event": "delivered",
"result": 34
},
{
"category": "Negative Notification",
"event": "processed",
"result": 34
},
{
"category": "Positive Notification",
"event": "open",
"result": 42
},
{
"category": "Referral",
"event": "delivered",
"result": 17
},
{
"category": "Positive Notification",
"event": "processed",
"result": 504
},
{
"category": "Referral",
"event": "processed",
"result": 18
},
{
"category": "Positive Notification",
"event": "delivered",
"result": 504
},
{
"category": "Negative Notification",
"event": "bounce",
"result": 16
},
{
"category": "Positive Notification",
"event": "bounce",
"result": 176
},
{
"category": "Referral",
"event": "open",
"result": 10
}
]
}
The problem with the way this is output is depending on weather the data is available or not, accessing the objects by number could create unexpected functionality. The second problem is that is has to be manipulated by javascript, and cannot be manipulated on the server side.
I would like the JSON to be reformatted so that each category is an object (there are currently three, but could be as many as five) has summarized data inside the object. For example:
{
"result": {
"Negative Notification" : [
{
"processed":34,
"delivered":34,
"bounces":16,
"opens":2
}
],
"Positive Notification" : [
{
"processed":504,
"delivered":504,
"bounces":176,
"opens":42
}
],
"Referral" : [
{
"processed":18,
"delivered":17,
"bounces":1,
"opens":10
}
]
}
}
How would I pull this off? Simply looping through and naming the objects is leading me nowhere.