First of all thanks for the fantastic content you make! Can you please make a short video about the staff you use (like mic, cam, ...) to make your awesome videos?
Funny thing I had to come with a solution like this video at work a few weeks ago, then got problems with nulls and yes, did null checks on every step. Maybe there is a smarter way, but who knows. Example: JsonNode detailsNode = node.get("details"); if (detailNode.has("usagePublicId") && !detailNode.get("usagePublicId").isNull()) { detailAvsc.setUsagePublicId(detailNode.get("usagePublicId").asText()); }
if you are using the objectMapper, then in the setup of the test "objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);" will do the trick (not entirely sure for nulls, but at least for not mapped fields, it does), but for a custom deserializer, then no idea other than checking as above if it has the field & if it's not null, for every step.
Or @Test void parseFile() throws JsonProcessingException { var tree = objectMapper.readTree(json); var posts = Optional.ofNullable(tree.get("data")) .flatMap(n -> Optional.ofNullable(n.get("allPost"))) .flatMap(n -> Optional.ofNullable(n.get("edges"))) .filter(JsonNode::isArray) .map(Iterable::spliterator) .map(s -> StreamSupport.stream(s, false)) .stream() .flatMap(s -> s) .map(n -> n.get("node")) .map(n -> { try { return objectMapper.treeToValue(n, Post.class); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }) .toList(); assertThat(posts).isNotEmpty(); } Post.class is a simple record of a post. The magic is treeToValue. Use traversing functions to get to the actual object and then use standard Jackson deserialization to your objects.