Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Read this JSON with Jackson's tree model and print the email of the second user, safely handling missing fields: {"users": [{"email": "a@x.com"}, {"name": "b"}]}

We read the document with mapper.readTree(json) and walk it with path(): root.path("users").path(1).path("email"). path() never returns null; for anything absent it returns a MissingNode, so the chain cannot throw, and we finish with email.isMissingNode() or email.asText("none") to handle the absent case. get() is the stricter alternative that returns null, at("/users/1/email") does the same walk with a JSON Pointer, and ObjectNode and ArrayNode let us build JSON the same way.

Why this question is asked

Not every JSON document deserves a Java class. When we only need one value from a large or irregular payload, such as a webhook, a third-party response or a test fixture, the tree model is the quickest tool. The trap is that real payloads have missing fields, and a naive chain of calls throws a NullPointerException on the first gap. Interviewers ask this small coding question to see whether we know the difference between get() and path() and how Jackson represents absence. In this answer, we will recap the tree model, write the safe lookup, compare it with the unsafe one, use a JSON Pointer, and build a document with ObjectNode.

1A Quick Recap of the Tree Model

readTree parses JSON into a tree of JsonNode objects, one node per JSON value. An ObjectNode holds named children, an ArrayNode holds indexed children, and value nodes such as TextNode, IntNode and BooleanNode hold single values. A NullNode represents an explicit JSON null. Finally, MissingNode is a special node that represents something that is not there at all; it never appears in a parsed document, but navigation methods return it when we ask for a child that does not exist.

Java
String json = "{\"users\": [{\"email\": \"a@x.com\"}, {\"name\": \"b\"}]}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(json);
root.getNodeType(); // OBJECT
root.path("users").size(); // 2
root.path("users").path(0).path("email"); // "a@x.com" (a TextNode)
The nodes in our document
PathNode typeValue
(root)ObjectNode{"users": [...]}
usersArrayNode2 elements
users[0].emailTextNode"a@x.com"
users[1].nameTextNode"b"
users[1].emailMissingNode (on lookup)not present

Our JSON has two users. The first has an email; the second, which is the one we were asked about, only has a name. So the correct output for the question is that there is no email.

2The Safe Lookup With path()

path() is the navigation method designed for this case. On an object node, path("name") returns the child or a MissingNode. On an array node, path(index) returns the element or a MissingNode. And calling path() on a MissingNode returns a MissingNode again, so a long chain simply carries the absence through to the end instead of failing in the middle.

Java
JsonNode email = root.path("users").path(1).path("email");
System.out.println(email.isMissingNode()); // true
System.out.println(email.asText("none")); // none
System.out.println(root.path("users").path(0).path("email").asText("none")); // a@x.com
System.out.println(root.path("customers").path(5).path("email").asText("none")); // none

asText("none") returns the text value when there is one and the default for missing or null nodes, so it is a compact way to finish the chain. Notice the last line: even the users array itself can be missing and the code still prints none. In Jackson 3 several text-related JsonNode methods were renamed towards String, for example asText towards asString, so check the method name for your version; the path() idea is unchanged.

3Why get() Throws in the Same Chain

get() is the other navigation method. It returns the child, or null when there is none. That is useful when we want to distinguish absent from present explicitly, but it makes chains unsafe.

Java
JsonNode e1 = root.get("users").get(1).get("email");
System.out.println(e1); // null: users[1] has no email
String text = root.get("users").get(1).get("email").asText();
// NullPointerException: get("email") returned null, and we called asText() on it
root.get("customers").get(0);
// NullPointerException: get("customers") already returned null

So the rule is simple: use path() when walking into data that may be incomplete, and use get() when absence is a programming error that should fail loudly. Both methods treat an explicit JSON null differently from a missing field: get() and path() both return a NullNode for "email": null, and isNull() is true for it, while isMissingNode() is false.

4Using a JSON Pointer With at()

A JSON Pointer is a standard string syntax, defined in RFC 6901, for addressing a value inside a JSON document. Each segment after a slash is a field name or an array index. JsonNode.at() takes such a pointer and behaves like a chain of path() calls: it returns a MissingNode for anything that is not there.

Java
JsonNode viaPointer = root.at("/users/1/email");
System.out.println(viaPointer.isMissingNode()); // true
System.out.println(root.at("/users/0/email").asText()); // a@x.com

Pointers are handy when the path comes from configuration, for example a mapping file that says which field to extract from each partner's webhook. If a field name contains a slash or a tilde, the pointer escapes them as ~1 and ~0.

5Building JSON With ObjectNode and ArrayNode

The tree model works in the other direction too. mapper.createObjectNode() gives us an empty object; put() adds values, putArray() and putObject() add nested containers and return them so we can keep filling them.

Java
ObjectNode out = mapper.createObjectNode();
ArrayNode users = out.putArray("users");
users.addObject().put("email", "a@x.com");
users.addObject().put("name", "b");
out.put("count", 2);
System.out.println(mapper.writeValueAsString(out));
// {"users":[{"email":"a@x.com"},{"name":"b"}],"count":2}

This is a good way to produce small, irregular documents in tests or adapters. For regular API responses, typed DTOs are clearer and checked by the compiler.

6Follow-up questions

When would you use the tree model instead of binding to a class?

When I need a few values from a large or irregular document, when the shape varies between partners, when I must preserve fields I do not understand, or in tests. For a regular payload I use every day, a record is clearer and lets the compiler check names and types.

How do you tell a missing field from a field that is null?

path() returns a MissingNode for a missing field and a NullNode for an explicit null. So isMissingNode() is true only for the first and isNull() only for the second. This difference matters for PATCH requests, where an explicit null may mean clear the value while a missing field means leave it alone.

Is the tree model expensive?

It holds the whole document in memory as node objects, which costs several times the size of the text. For documents of a few kilobytes or megabytes that is fine. For very large inputs, the streaming JsonParser reads token by token instead.

7Conclusion

In this answer, we read the document with readTree and found the second user's email safely with root.path("users").path(1).path("email"), which returns a MissingNode rather than null. We finished the chain with isMissingNode() or asText("none"), saw why get() throws in the same chain, used the JSON Pointer /users/1/email with at(), and built JSON with ObjectNode and ArrayNode.

What to say in the interviewAbout 80 seconds aloud

Open with

I would parse it with mapper.readTree and then navigate with path rather than get: root.path users, then path 1 for the second element, then path email. The point of path is that it never returns null.

Then, if they want more

When something is absent it returns a MissingNode, and calling path on a MissingNode gives another MissingNode, so the chain carries the absence to the end without throwing.

Here the second user has only a name, so the result is a MissingNode, and I would finish with isMissingNode to branch, or asText with a default such as none to print it. In Jackson 3 the text methods were renamed towards asString, so I would check the name there.

If I used get instead, get email returns null and calling asText on it throws a NullPointerException, so I use get only when absence should be a loud error. The same lookup can be written as root.at with the JSON Pointer slash users slash 1 slash email, which is nice when the path comes from configuration.

I would also mention that an explicit null gives a NullNode, not a MissingNode, which matters for PATCH semantics, and that to build JSON I use createObjectNode, putArray and put.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Read this JSON with Jackson's tree model and print the email of the second user, safely...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/5682724312/