0 / 0
JSON content model
Last updated: Jan 17, 2024
JSON content model

The JSON content model is used to access content stored in JSON format. It provides a basic API to allow callers to extract values on the assumption that they know which values are to be accessed.

Table 1. Methods for the JSON content model
Method Return types Description
getJSONAsString() String Returns the JSON content as a string.
getObjectAt(<List of cbjecta> path, JSONArtifact artifact) throws Exception Object Returns the object at the specified path. The supplied root artifact might be null, in which case the root of the content is used. The returned value can be a literal string, integer, real or boolean, or a JSON artifact (either a JSON object or a JSON array).
getChildValuesAt(<List of object> path, JSONArtifact artifact) throws Exception Hash table (key:object, value:object> Returns the child values of the specified path if the path leads to a JSON object or null otherwise. The keys in the table are strings while the associated value can be a literal string, integer, real or boolean, or a JSON artifact (either a JSON object or a JSON array).
getChildrenAt(<List of object> path path, JSONArtifact artifact) throws Exception List of objects Returns the list of objects at the specified path if the path leads to a JSON array or null otherwise. The returned values can be a literal string, integer, real or boolean, or a JSON artifact (either a JSON object or a JSON array).
reset() void Flushes any internal storage associated with this content model (for example, a cached DOM object).

Example script

If an output builder node creates output based on JSON format, you could use the following to access information about a set of books:

results = []
outputbuilder.run(results)
output = results[0]
cm = output.getContentModel("jsonContent")

bookTitle = cm.getObjectAt(["books", "ISIN123456", "title"], None)

# Alternatively, get the book object and use it as the root
# for subsequent entries
book = cm.getObjectAt(["books", "ISIN123456"], None)
bookTitle = cm.getObjectAt(["title"], book)

# Get all child values for aspecific book
bookInfo = cm.getChildValuesAt(["books", "ISIN123456"], None)

# Get the third book entry. Assumes the top-level "books" value
# contains a JSON array which can be indexed
bookInfo = cm.getObjectAt(["books", 2], None)

# Get a list of all child entries
allBooks = cm.getChildrenAt(["books"], None)
Generative AI search and answer
These answers are generated by a large language model in watsonx.ai based on content from the product documentation. Learn more