Created
October 23, 2018 07:39
-
-
Save Cvetomird91/054c009e8737bc43c8ba243a59b4cc50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.node.ArrayNode; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Arrays; | |
public class JacksonFilterNodes { | |
public static void main(String[] args) throws IOException { | |
//read the JSON file into POJOs | |
ObjectMapper objectMapper = new ObjectMapper(); | |
File file = new File("./data.json"); | |
JsonNode[] payments = objectMapper.readValue(file, JsonNode[].class); | |
//convert the property representing the transactions into an ArrayList | |
List<JsonNode> paymentsList = new ArrayList<JsonNode>(Arrays.asList(payments)); | |
//Filter the transactions whose status isn't received | |
paymentsList.removeIf((JsonNode p) -> p.get("status").asText() == null || !p.get("status").asText().equals("Received")); | |
for (JsonNode p : paymentsList) { | |
System.out.println(p.get("status").asText()); | |
} | |
String jsonAry = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(paymentsList); | |
System.out.println(jsonAry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment