Whenever we deal with JSON we need to parse it at a specific level. What if I don't have a location to parse to parse specific value, how we will do that?.
e. g if have below JSON and we want to parse it to get values of analytics Id
e. g if have below JSON and we want to parse it to get values of analytics Id
{
"buildInfo": {
"application": "RepCatalog",
"version": "1.0.0",
"buildDate": "Thu Oct 13 17:01:48 IST 2016"
},
"status": "success",
"data": {"analytics": [ { "status": "success",
"catalogRep": {
"analyticsId": 48961,
"ReportType": "REPORT",
"title": "REPORT1476371357505",
"rights": [ {
"availability": "PUBLIC",
"isDefault": true
}],
"initialCreation": "10/13/2016 20:39:11",
"lastModified": "10/13/2016 20:39:11",
"lastAccessed": "10/13/2016 20:39:11",
"status": "OPEN"
},
"cascadeCount": 0
}]},
"time": 6674
}
I struggle alot the and find the working solution around it
I have written following function to get all values of analyticId
def extractRepIds (def tree, def ids = []) {
switch (tree) {
case Map:
tree.each { k, v ->
if (k == "AnalyticsId") { ids << v }
extractRepIds(v, ids)
}
return ids
case Collection:
tree.each { e -> extractRepIds(e, ids) }
return ids
default :
return ids
}
}
Now I have to call that function for values
def extractRepIdsFromJson(def jsonString) {
def tree = new JsonSlurper().parseText(jsonString)
extractRepIds(tree)
}

