Quick revision
The short answer
We parse the text into Gson's tree with JsonObject root = JsonParser.parseString(json).getAsJsonObject(), navigate to the array with root.getAsJsonObject("order").getAsJsonArray("items"), and loop over it: for (JsonElement e : items) System.out.println(e.getAsJsonObject().get("sku").getAsString()), which prints A and B. The tree API holds the whole document in memory as JsonElement objects, and older code that calls new JsonParser().parse uses an instance method that is deprecated in favour of these static methods.
What to remember
In this answer, we parsed the document with JsonParser.parseString, stepped down with getAsJsonObject("order") and getAsJsonArray("items"), iterated the JsonArray and read each sku with get("sku").getAsString(), printing A and B. We saw how missing members return Java null while null values return JsonNull, that the old new JsonParser().parse form is deprecated in favour of the static methods, and that the tree keeps the whole document in memory, so large inputs need streaming instead.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We parse the text into Gson's tree with JsonObject root = JsonParser.parseString(json).getAsJsonObject(), navigate to the array with root.getAsJsonObject("order").getAsJsonArray("items"), and loop over it: for (JsonElement e : items) System.out.println(e.getAsJsonObject().get("sku").getAsString()), which prints A and B. The tree API holds the whole document in memory as JsonElement objects, and older code that calls new JsonParser().parse uses an instance method that is deprecated in favour of these static methods.
Why this question is asked
Not every piece of JSON deserves a Java class. A health check, a one-off script, a webhook from which we need two fields, or a payload whose shape varies: in all of these, defining classes is more work than reading the values directly. Gson's tree API, its JSON object model, is the tool for that. Interviewers ask this small coding question to see whether we know the tree classes and their navigation methods, and whether we know the cost and the deprecated older form. In this answer, we will recap the tree model, write the solution step by step, handle missing values safely, and see when the tree is the wrong choice.
1A Quick Recap of Gson's Tree Model
Gson represents any JSON document as a tree of JsonElement objects. JsonElement is the base class, and it has four concrete kinds. JsonObject is a JSON object, a map from names to elements. JsonArray is a JSON array, an ordered list of elements. JsonPrimitive holds a string, number or boolean. JsonNull represents a JSON null. Each element has getAs methods to view it as a particular kind, such as getAsJsonObject() or getAsString(), and is methods to check what it is, such as isJsonObject().
- JsonObject root{ "order": ... }
- JsonObject order{ "id": 7, "items": [...] }
- JsonArray itemstwo elements
- JsonObject item{ "sku": "A" } and { "sku": "B" }
- JsonPrimitive sku"A", then "B"
5 layers, from the top down. Step through them one at a time.
Our document is an object containing an object containing an array of objects, so we will step down through exactly those kinds.
2Writing the Solution
Here is the full program, with the result of each step.
Notice the two kinds of method. On a JsonObject, getAsJsonObject("order") and getAsJsonArray("items") take a member name and return that member already cast to the expected kind. On a JsonElement, getAsJsonObject() with no argument casts the element itself. JsonArray implements Iterable<JsonElement>, so the enhanced for loop works directly. Finally, get("sku") returns a JsonElement, and getAsString() turns the primitive into a Java String. Printing the element itself would print the JSON form, with quotes, which is a common surprise.
3Handling Missing or Unexpected Values
The code above assumes the document has exactly the expected shape. With real input, an item may lack a sku, or the sku may be null, and then the calls fail in different ways.
So get() returns Java null when the member does not exist, and a JsonNull element when it exists with the value null. A careful reader checks both, or uses has("sku") first. In a script that is acceptable; in production code with many fields, the number of checks is itself a sign that a small class would be clearer.
4The Old Instance API
Older examples create a parser and call its instance method.
The static methods, parseString and parseReader, were added in Gson 2.8.6, and the instance methods were deprecated at the same time. The behaviour is the same, so modernising old code is a simple replacement. There is a small difference in strictness to be aware of: these methods parse leniently, accepting some input that is not strictly valid JSON; check your version if strict parsing matters.
5When the Tree Is the Wrong Choice
The tree API holds the whole document in memory as JsonElement objects, each with its own overhead, so a document takes several times its text size in heap. That is no problem for a webhook of a few kilobytes, but for a file of hundreds of megabytes it can exhaust memory. For large inputs we use JsonReader, Gson's streaming API, which reads one token at a time. For stable, well-known structures we use classes, which give us type checking and clearer code. The tree sits between them: ideal for small documents, varying shapes and quick extraction.
6Follow-up questions
How would we do the same with Jackson?
Jackson's tree model is JsonNode: mapper.readTree(json).path("order").path("items"), then for each node call get("sku").asText(). Its path() method returns a missing node instead of null, which makes navigation safer than Gson's get().
Can we change the tree and write it back?
Yes. JsonObject has add, addProperty and remove, and JsonArray has add and set, so we can modify the tree and write it with gson.toJson(root). This is a convenient way to patch one field in a document whose full structure we do not model.
Can we turn part of the tree into a class?
Yes, gson.fromJson(element, Item.class) converts any JsonElement into an object, so we can navigate to the interesting part with the tree API and then bind just that part to a class.
7Conclusion
In this answer, we parsed the document with JsonParser.parseString, stepped down with getAsJsonObject("order") and getAsJsonArray("items"), iterated the JsonArray and read each sku with get("sku").getAsString(), printing A and B. We saw how missing members return Java null while null values return JsonNull, that the old new JsonParser().parse form is deprecated in favour of the static methods, and that the tree keeps the whole document in memory, so large inputs need streaming instead.
Open with
I would use Gson's tree model. First, JsonObject root = JsonParser.parseString(json).getAsJsonObject(), which parses the whole text into JsonElement objects. Then I navigate: root.getAsJsonObject("order") gives the order object, and .getAsJsonArray("items") gives the array.
Then, if they want more
JsonArray is iterable, so I loop with for (JsonElement e : items) and print e.getAsJsonObject().get("sku").getAsString(), which prints A and then B. I call getAsString rather than printing the element, because printing a JsonPrimitive gives its JSON form with quotes.
For real input I would guard against missing data: get returns Java null if the member is absent and a JsonNull if it is present with value null, so I would check both or use has first.
I would also mention that older code writes new JsonParser().parse(json); those instance methods are deprecated, and the static parseString and parseReader replace them with the same behaviour.
Finally, the tree keeps the whole document in memory as JsonElement objects, which is fine for small payloads like this one, but for large files I would switch to JsonReader streaming, and for stable structures I would rather map to classes.
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 official documentation for the version you use. 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 "Without creating a Java class, read the JSON...": https://scrivio-interview-library.netlify.app/java/json-libraries/8dfc6ad778/