I was trying to find the summation of deceased count from JSON object extract from the following JSON response
https://api.covid19india.org/state_district_wise.json
Snippet from above is json
"Maharashtra": {
"districtData": {
"Ahmednagar": {
"notes": "",
"active": 4206,
"confirmed": 275516,
"migratedother": 1,
"deceased": 5565,
"recovered": 265744,
"delta": {
"confirmed": 559,
"deceased": 4,
"recovered": 0
}
},
I want to find the sum of the deceased from the state of Maharashtra. I know we can use collect on JSONArray but here is JSONObject. I tried following but to no avail
int value=jsonPath.get("Maharashtra.districtData.collect{it.deceased}.sum()");
There are 2
deceasedkeys in the object, I assume that you only want thedeceasedin the outer object. The simplest solution I get so far.In case, you don't want to use Java8 stream.
I use JsonPath (com.jayway.jsonpath.JsonPath)
pom.xml
Update
I come up with another solution, that using mapping POJO.
POJO