JsonNode
The JsonNode class is a versatile class designed to work with JSON-like data structures. It provides a set of methods to navigate, manipulate, and extract information from JSON data. This class is particularly useful for handling and processing JSON data within your applications.
Methods
get(property: String): JsonNode:
This method is used to retrieve a nested property (key) from the JSON-like data, returning a new JsonNode representing the specified property.
// Assuming you have a JsonNode representing a JSON object
// Get a nested property from the JSON object
var nestedProperty = jsonObject.get("nestedProperty");asText(): String:
It returns the content of the JsonNode as a string, assuming the content is textual.
// Assuming you have a JsonNode containing a textual value
// Get the content of the JsonNode as a string
var textValue = textNode.asText();asInt(): Integer:
This method converts the JsonNode to an integer value if it's numeric; otherwise, it may throw an error or return a default value.
// Assuming you have a JsonNode containing a numeric value
// Get the JsonNode as an integer, handling non-numeric values
var intValue = numericNode.asInt();asBoolean(): Boolean:
Converts the JsonNode to a Boolean value if it's a valid boolean value; otherwise, it may throw an error or return a default value.
isObject(): Boolean:
Checks if the JsonNode represents a JSON object and returns true if it is an object, otherwise false.
isArray(): Boolean:
Similar to isObject, this method checks if the JsonNode represents a JSON array and returns true if it is an array, otherwise false.
isNumber(): Boolean:
Determines if the JsonNode represents a numeric value and returns true if it's numeric, otherwise false.
isBoolean(): Boolean:
Checks if the JsonNode represents a Boolean value and returns true if it's a boolean, otherwise false.
isNull(): Boolean:
Checks if the JsonNode is null and returns true if it's null, otherwise false.
isTextual(): Boolean:
Determines if the JsonNode contains textual data and returns true if it's textual, otherwise false.
toPrettyString(): String:
This method returns a nicely formatted, pretty-printed string representation of the JSON data contained in the JsonNode.
toString(): String:
Returns a string representation of the JSON data contained in the JsonNode.
hasNonNull(filed: String): Boolean:
Checks if the JsonNode contains a non-null field with the specified name and returns true if it exists and is not null, otherwise false.
Examples
Example: JSON Property Retrieval
To access additional JSON utility functions, please refer to JsonUtil Class in Utilities.
Was this helpful?