What is the difference between ObjectNode and JsonNode in Jackson?

JsonNode is a base class that ObjectNode and ArrayNode extend. JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.

ArrayNode has specific methods for dealing with arrays such as get(index i) E.g. you cannot get an item at a specific index in a JsonNode or ObjectNode but you can in an ArrayNode.


To address OP's point about Stack Overflow answers using ObjectNode and JsonNode interchangeably: a possible source of the confusion you see is that JsonNode implements most of the functionality of both ObjectNode and ArrayNode, returning mostly false or null by default. The sub-types then override their respective methods to work correctly (e.g. indexing for ArrayNode, named access for ObjectNode). The Javadoc says:

As a general design rule, most accessors ("getters") are included in this [JsonNode] base class, to allow for traversing structure without type casts. Most mutators, however, need to be accessed through specific sub-classes (such as [ObjectNode] and [ArrayNode]). This seems sensible because proper type information is generally available when building or modifying trees, but less often when reading a tree (newly built from parsed JSON content)

"[T]o allow for traversing structure without type casts" is key, and is the reason that you can often get away with using JsonNode all the time, because as long as the methods you're calling match the sub-type of the object data actually got parsed into, they work as expected.