JsonNode class sealed

One value of a JSON document held as a tree: an object, array, string, number, boolean or null, with typed accessors for navigating it.

Use it when JSON has no fixed shape worth a class: a webhook payload where two fields matter, a document to patch before passing it on, or a property whose content varies. ObjectMapper.readTree parses text into one; a property or a value typed JsonNode (or a subtype) is read as a tree too, and a node is written back as its JSON. Compared with the plain maps and lists of jsonDecode, it adds safe paths (path, at) and conversions (asInt, stringValue, ...). Nodes are equal when their JSON is.

// Read a few fields of a document without a model class.
final root = mapper.readTree('{"book": {"title": "Dune", "tags": ["sf"]}}');
root.get('book')?.get('title')?.asString();   // Dune
root.path('book').path('price').asInt(10);    // 10 (missing → default)
root.at('/book/tags/0').stringValue();        // sf
(root as ObjectNode).put('read', true);
mapper.treeToValue<Shelf>(root);              // into a class

Spring (Java):

JsonNode root = mapper.readTree(json);
root.get("book").get("title").asString();
root.at("/book/tags/0").stringValue();

Constructors

JsonNode.from(Object? json)
Wraps decoded JSON json (what jsonDecode returns) in nodes; a JsonNode is returned as it is. Map keys become strings.
factory

Properties

hashCode → int
The hash code for this object.
no setteroverride
isArray → bool
Whether this is an ArrayNode.
no setter
isBoolean → bool
Whether this is a BooleanNode.
no setter
isContainer → bool
Whether this is an object or an array.
no setter
isEmpty → bool
Whether the object or array has no children (true for other nodes).
no setter
isIntegralNumber → bool
Whether this is a number without a fraction.
no setter
isMissingNode → bool
Whether this is the MissingNode.
no setter
isNull → bool
Whether this is the NullNode.
no setter
isNumber → bool
Whether this is a NumericNode.
no setter
isObject → bool
Whether this is an ObjectNode.
no setter
isString → bool
Whether this is a StringNode.
no setter
isValueNode → bool
Whether this is a string, number, boolean or null.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
size → int
The number of properties of an object or elements of an array; 0 for other nodes.
no setter

Methods

asBoolean([bool? defaultValue]) → bool
The value as a bool: a boolean, "true" / "false", a number (not 0), false for null and a missing node; anything else gives defaultValue, or throws a JsonNodeException without one.
asDouble([double? defaultValue]) → double
The value as a double, converted like asInt (a string may be any number, "1.5").
asInt([int? defaultValue]) → int
The value as an int (Jackson 3): a finite number (truncated), a string of an integer ("42"), a boolean (1 / 0), 0 for null and a missing node. Anything else gives defaultValue, or throws a JsonNodeException without one; null and a missing node give defaultValue when there is one.
asString([String? defaultValue]) → String
The value as a string (Jackson 3): the text of a string, number or boolean, '' for null and a missing node. An object or array gives defaultValue, or throws a JsonNodeException without one; null and a missing node give defaultValue when there is one.
at(String pointer) → JsonNode
Returns the node at the JSON Pointer pointer (/book/tags/0; ~1 stands for /, ~0 for ~), or a MissingNode when there is none. An empty pointer gives this node; one not starting with / throws an ArgumentError.
booleanValue() → bool
Returns the value of a BooleanNode; throws a JsonNodeException for other nodes.
deepCopy() → JsonNode
Returns a copy that shares no objects or arrays with this node, so it can be changed independently.
doubleValue() → double
Returns the value of a NumericNode as a double; throws a JsonNodeException for other nodes.
get(Object nameOrIndex) → JsonNode?
The property nameOrIndex of an object, or the element at index nameOrIndex of an array; null when there is none.
has(Object nameOrIndex) → bool
Whether get finds a node for nameOrIndex.
intValue() → int
The value of a NumericNode as an int, when it is one exactly; throws a JsonNodeException for a fraction (1.5), NaN, infinity and other nodes.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
numberValue() → num
Returns the value of a NumericNode; throws a JsonNodeException for other nodes.
path(Object nameOrIndex) → JsonNode
Like get, but a MissingNode instead of null, so paths can be chained: root.path('a').path('b').
properties() → Iterable<MapEntry<String, JsonNode>>
The properties of an object as name/node entries, in order; empty for other nodes.
propertyNames() → Iterable<String>
The property names of an object, in order; empty for other nodes.
stringValue() → String
Returns the text of a StringNode; throws a JsonNodeException for other nodes. Use asString to convert instead.
toJson() → Object?
Returns this node as plain decoded JSON: null, a bool, number, string, list or map, as jsonEncode takes it (MissingNode gives null).
toPrettyString() → String
This node as JSON text indented by two spaces, for people to read.
toString() → String
This node as compact JSON text.
override
values() → Iterable<JsonNode>
The elements of an array or the property values of an object; empty for other nodes.

Operators

operator ==(Object other) → bool
The equality operator.
override