5

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?

1 Answer 1

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.