I have JSON array values as given below,
{
"test_details": [
{
"test_id": "001",
"test_name": "Blood Test",
"group_name": "Group 1",
"package_details": [
{
"package_name": "master health checkup",
"package_id": "1"
},
{
"package_name": "special camp package",
"package_id": "2"
}
]
},
{
"test_id": "002",
"test_name": "haemoglobin",
"group_name": "Group 2",
"package_details": [
{
"package_name": "master health checkup",
"package_id": "1"
}
]
},
{
"test_id": "003",
"test_name": "creatine test",
"group_name": "Group 2",
"package_name": null
}
]
}
In the above JSON, I want to group the values by the key package_name, which means I want to order the test list against the package name.
I have tried several array group by functions but for a dynamic key package_name, it is not working.
My final output JSON would be:
{
"test_details": {
"master health checkup": [
{
"test_id": "001",
"test_name": "Blood Test",
"group_name": "Group 1",
"package_details": [
{
"package_name": "master health checkup",
"package_id": "1"
},
{
"package_name": "special camp package",
"package_id": "2"
}
]
},
{
"test_id": "002",
"test_name": "haemoglobin",
"group_name": "Group 2",
"package_details": [
{
"package_name": "master health checkup",
"package_id": "1"
}
]
}
],
"special camp package": [
{
"test_id": "001",
"test_name": "Blood Test",
"group_name": "Group 1",
"package_details": [
{
"package_name": "master health checkup",
"package_id": "1"
},
{
"package_name": "special camp package",
"package_id": "2"
}
]
}
]
}
}
here you can see the master health checkup have 2 tests and under and same as special camp package have 1 test. how to achieve this? how to apply array group by when the array key is dynamic?