I am trying to get the value of a key from the JsonArray. In result, I am getting the JsonObject, in the below format
{
"docs": [
{
"_index": "esindex",
"_type": "esmessage",
"_id": "4",
"_version": 1,
"found": true,
"_source": {
"account_number": 4,
"balance": 27658
}
}
]
}
I need to find the id
of all those documents havig "found": true
. The format of JsonArray produced is
[
{
"_index": "esindex",
"_type": "esmessage",
"_id": "4",
"_version": 1,
"found": true,
"_source": {
"account_number": 4,
"balance": 27658
}
}
]
I am able to get the list of all the ids using the below java code
List<Integer> documents = new ArrayList<>();
JsonObject result = esClient.execute(builder.build()).getJsonObject();
JsonArray jsonArray = result.getAsJsonObject().get("docs").getAsJsonArray();
while (i < request.getIds().size()) {
JsonPrimitive checkIfExists = jsonArray.get(i).getAsJsonObject().getAsJsonPrimitive("found");
if (checkIfExists.getAsString().equals("true"))
documents.add(result.getAsJsonObject().get("docs").getAsJsonArray().get(i).getAsJsonObject().getAsJsonPrimitive("_id").getAsInt());
i++;
}
Can anyone please help me, to parse the JsonArray result using Java Stream API