I am receiving a rest response in following format.
{
"query_result": {
"status": "SUCCESS",
"message": "Results Found",
"results": [
{
"key": "123"
},
{
"key": "465"
}
]
}
}
From this response, I want to create a list of String from the keys. Thus looking to get a list outcome as follows:
["123", "456"]
Trying to produce the output as follows but stuck at the map step in following code block.
public Optional<List<String>> get(HttpEntity<Request> request){
ResponseEntity<Response> response = getResponse(request); // this response holds above json data
return Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
.filter(CollectionUtils::isNotEmpty)
.map(results -> results.) // Stuck here. results here is the list of results shown in above json.
}
I am not able to pull out all the key's values from here. I end up having to indicate index as follows which is not going to work since I want all keys.
.map(results -> results.get(0))
Tried considering using iterator but that would again only end up capturing the first key only.
.filter(CollectionUtils::isNotEmpty)
.map(List::iterator)
.map(Iterator::next)
.map(result -> result.getKey())
Could I please get some help with this. Thanks.