Reputation: 1079
I am using Jackson to fetch Json as following:
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 jsonNode which is an array of many Json objects. I would like to keep only Json objects which contains a certain field:value as keeping all objects with "courseLevel":"basic". How should I do that? Is ObjectMapper right way to go or any better way to filter objects in the array and only keep those with certain field/value? Any suggestion?
Upvotes: 5
Views: 5469
Reputation: 22224
ObjectMapper is meant for data binding of an object to a JSON document. You can discard or retain an object, only after it has been deserialized. Here is a suggestion for retaining only object with "field":"value"
pair present :
Iterator<JsonNode> it = rootNode.iterator();
while (it.hasNext()) {
JsonNode node = it.next();
if (node.has("field") && !node.get("field").textValue().equals("value")) {
it.remove();
}
}
Upvotes: 3