I've a JSON as follows.
var test = [{
"id": "3",
"city": "seattle",
"place" : "xxx",
"usage" : "163612",
"available": "162500"
}, {
"id": "4",
"city": "washington",
"place" : "xxx",
"usage" : "52542",
"available": "86624"
}, {
"id": "3",
"city": "seattle",
"place" : "yyy",
"usage" : "163612",
"available": "962500"
},
{
"id": "5",
"city": "seattle",
"place" : "yyy",
"usage" : "562",
"available": "24252"
},
{
"id": "4",
"city": "washington",
"place" : "yyy",
"usage" : "163612",
"available": "319250"
}]
I want to group this JSON by 'id' and 'city'. The newly formed grouped JSON should be as follows.
[
{
"3": {
"seattle": [
{
"xxx": {
"usage": "163612",
"available": "162500"
}
},
{
"yyy": {
"usage": "163612",
"available": "962500"
}
}
]
}
},
{
"4": {
"washington": [
{
"xxx": {
"usage": "52542",
"available": "86624"
}
},
{
"yyy": {
"usage": "163612",
"available": "319250"
}
}
]
}
},
{
"5": {
"seattle": [
{
"xxx": {
"usage": "562",
"available": "24252"
}
}
]
}
}
]
I tried with looping and sorting and I'm unable to get the required result. Is there any way to construct this JSON.