Using Jackson and JsonNode, how to filter Json objects with a specific "field": "value",

I use Jackson to extract Json as follows:

WSRequest request = WS.url("https://www.someurl.com");
Promise<WSResponse> promise = request.get();
Promise<JsonNode> jsonPromise = promise.map(s -> {return s.asJson();});
JsonNode jsonNode = jsonPromise.get(1000);

So far so good. Now I have a jsonNode, which is an array of many Json objects. I would like to save only Json objects that contain a specific field: value as saving all objects using "courseLevel": "basic". How can I do it? Is ObjectMapper the right way or some better way to filter objects in an array and save only those who have a specific field / value? Any suggestion?

+4
source share
1 answer

ObjectMapper JSON. , . "field":"value":

Iterator<JsonNode> it = rootNode.iterator();
while (it.hasNext()) {
    JsonNode node = it.next();
    if (node.has("field") && !node.get("field").textValue().equals("value")) {
        it.remove();
    }
}
0

Source: https://habr.com/ru/post/1606499/


All Articles