JsonNode Class

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.

// Assuming you have a JsonNode containing a boolean value
// Get the JsonNode as a Boolean, handling non-boolean values
var boolValue = booleanNode.asBoolean(); // May throw an exception for non-boolean values
  • isObject(): Boolean:

Checks if the JsonNode represents a JSON object and returns true if it is an object, otherwise false.

// Assuming you have a JsonNode
// Check if the JsonNode represents a JSON object
var isObject = jsonNode.isObject();
  • 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.

// Assuming you have a JsonNode
// Check if the JsonNode represents a JSON array
var isArray = jsonNode.isArray();
  • isNumber(): Boolean:

Determines if the JsonNode represents a numeric value and returns true if it's numeric, otherwise false.

// Assuming you have a JsonNode
// Check if the JsonNode represents a numeric value
var isNumber = jsonNode.isNumber();
  • isBoolean(): Boolean:

Checks if the JsonNode represents a Boolean value and returns true if it's a boolean, otherwise false.

// Assuming you have a JsonNode
// Check if the JsonNode represents a Boolean value
var isBoolean = jsonNode.isBoolean();
  • isNull(): Boolean:

Checks if the JsonNode is null and returns true if it's null, otherwise false.

// Assuming you have a JsonNode
// Check if the JsonNode is null
var isNull = jsonNode.isNull();
  • isTextual(): Boolean:

Determines if the JsonNode contains textual data and returns true if it's textual, otherwise false.

// Assuming you have a JsonNode containing textual data
// Check if the JsonNode contains textual data
var isTextual = textNode.isTextual();
  • toPrettyString(): String:

This method returns a nicely formatted, pretty-printed string representation of the JSON data contained in the JsonNode.

// Assuming you have a JsonNode
// Get a pretty-printed string representation of the JSON data
var prettyJson = jsonNode.toPrettyString();
  • toString(): String:

Returns a string representation of the JSON data contained in the JsonNode.

// Assuming you have a JsonNode
// Get a string representation of the JSON data
var jsonString = jsonNode.toString();
  • 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.

// Assuming you have a JsonNode representing a JSON object
// Check if the JsonNode has a non-null field named "fieldName"
var hasField = jsonObject.hasNonNull("fieldName");

Examples

Example: JSON Property Retrieval

// Assuming you have a JSON object retrieved from Context.getCreatedBy()
var json = Context.getStartedBy(); // getStartedBY method of Context Class

// Get a specific property from the JSON object
var propertyNode = json.get("propertyName");
if (propertyNode != null) {
    var propertyValue = propertyNode.asText();
    return ("Value of 'propertyName': " + propertyValue);
} else {
    return ("The 'propertyName' property does not exist in the JSON object.");
}

To access additional JSON utility functions, please refer to JsonUtil Class in Utilities.