For the complete documentation index, see llms.txt. This page is also available as Markdown.

JsonUtil

The JsonUtil class provides utility methods for working with JSON data. It includes methods for converting objects to JSON, parsing JSON strings, and manipulating JSON data.

Methods

toJson(object: any): JsonNode

Converts a JavaScript object to a JSON representation and returns a JsonNode object.

// Convert an object to JSON
var dataObject = { "name": "John", "age": 30 };
var jsonData = JsonUtil.toJson(dataObject);

parse(jsonStr: String): JsonNode

Parses a JSON string and returns a JsonNode object representing the parsed JSON data.

// Parse a JSON string
var jsonString = '{"name": "Alice", "age": 25}';
var parsedData = JsonUtil.parse(jsonString);

parseXml(xmlStr: String): JsonNode

Parses a XML string and returns a JsonNode object representing the parsed XML data.

// Parse a XML string
var xmlStr = "<messages><note id=\"501\"><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note><note id=\"502\"><to>Jani</to><from>Tove</from><heading>Re: Reminder</heading><body>I will not</body></note></messages>"
var obj = JsonUtil.parseXml(xmlStr)

var note0 = obj.get("note").get(0);
var note1 = obj.get("note").get(1);

var note1ID = note1.get("id").asInt() // 502
var note1Body = note1.get("body").asText(); // I will not

stringify(jsonNode: JsonNode): String

Converts a JsonNode object back to a JSON string.


prettyPrint(jsonNode: JsonNode): String

Formats and pretty-prints a JsonNode object as a JSON string with proper indentation and line breaks.


readPath(jsonNode: String, path: String): any

Reads a specific value from a JSON object using a JSON path and returns it.


createObjectNode(): ObjectNode

Create an empty Json Object to be set with values.


Examples

Example: JSON Read Using JSON Path

Description: This script reads a JSON property value using JSON path from a JSON string.

Use Case: Useful for extracting specific data from structured JSON objects.