I have a requirement where I need to extract an element/attribute from an JSON object based on a particular key. I am super confused here how to fetch.
{ "metadata":{
"lastSyncTime":"2000-11-21T16:07:53",
"dataFromDB":true
},
"allocationReports":[ {
"allocatedUserCount":100,
"healthGoalName":"Eat Healthier",
"segments": [
{segmentValue: "40-49", allocatedUserCount: 2},
{segmentValue: "30-39", allocatedUserCount: 13},
{segmentValue: "20-29", allocatedUserCount: 23}
]
},
{
"allocatedUserCount":130,
"healthGoalName":"Feel Happier",
"segments": [
{segmentValue: "40-49", allocatedUserCount: 20},
{segmentValue: "30-39", allocatedUserCount: 3},
{segmentValue: "20-29", allocatedUserCount: 53}
]
},
{
"allocatedUserCount":150,
"healthGoalName":"Quit Smoking",
"segments": [
{segmentValue: "40-49", allocatedUserCount: 19},
{segmentValue: "30-39", allocatedUserCount: 1},
{segmentValue: "20-29", allocatedUserCount: 22},
{segmentValue: "10-19", allocatedUserCount: 47}
]
}
],
"overall":{
"usersWithGoalCount":0,
"registeredCount":500,
"eligibleCount":280
}
}
How can we extract the segments array based on healthgoalName. For example I have written a method which takes 'healthGoalName'(Let's say Quit Smoking) as argument. This method should return -
"segments": [
{segmentValue: "40-49", allocatedUserCount: 19},
{segmentValue: "30-39", allocatedUserCount: 1},
{segmentValue: "20-29", allocatedUserCount: 22},
{segmentValue: "10-19", allocatedUserCount: 47}
]
Thanks in advance.