Node
The Node class represents a specific location in JSON data and allows navigation relative to that location. It provides a way to traverse and explore component data with lazy evaluation, meaning data is only accessed when explicitly requested through methods like get_value(), exists(), or iteration.
Class Methods
from_component_json
@classmethod
from_component_json(cls, data)Creates a Node instance from a JSON object. Note that a Node instance created from the component JSON only will not contain the deltas. While this can be useful for testing most policies, it will not represent a realistic component's data when testing Check.get_all.
data (dict): A dictionary containing component metadata
Returns: A new
Nodeinstance
Example:
component_json = {
"readme": {
"lines": 50,
"missing": False
}
}
component_data = Node.from_component_json(component_json)from_component_json_file
Creates a Node instance from a JSON file. Note that a Node instance created from the component JSON only will not contain the deltas. While this can be useful for testing most policies, it will not represent a realistic component's data when testing Check.get_all.
file_path (str): Path to a JSON file containing component metadata
Returns: A new
Nodeinstance
Example:
from_bundle_json
Creates a Node instance from a bundle JSON object.
data (dict): A dictionary containing bundle data
Returns: A new
Nodeinstance
from_bundle_file
Creates a Node instance from a bundle JSON file.
file_path (str): Path to a JSON file containing bundle data
Returns: A new
Nodeinstance
Data Access Methods
get_value
Gets the raw value at the given path relative to this node.
path (str): JSON path relative to this node (default: "." for this node's value)
Returns: The raw value (dict, list, string, number, boolean, etc.)
Raises:
ValueErrorif the path is invalid or doesn't exist,NoDataErrorif data is not available yet
Example:
get_value_or_default
Gets the raw value at the given path relative to this node. If the value is missing for any reason, it returns the specified default instead.
path (str): JSON path relative to this node (default: "." for this node's value)
default: The value to return if there is no value at this path
Example:
get_node
Gets a Node at the given path relative to this node. Uses lazy evaluation - data is not accessed until value is needed.
path (str): JSON path relative to this node
Returns: A new Node instance at the specified path
Raises:
ValueErrorif the path syntax is invalid
exists
Checks if a path exists relative to this node.
Missing path behavior:
Raises
NoDataErrorbefore collectors finished (results inpendingstatus).Returns
Falseafter collectors finished.
Example:
Iteration Methods
Iterating over Node
Makes Node iterable. For dictionaries, yields keys. For arrays, yields Node objects.
For dict-like data: Yields string keys
For array-like data: Yields Node objects for each array element
Raises:
ValueErrorif the node doesn't point to a dict or array,NoDataErrorif data is not available yet
Example:
items
Get key-value pairs when this Node points to a dict-like structure.
Returns: Iterator of (key, Node) tuples for dict-like data
Raises:
ValueErrorif the node doesn't point to a dictionary,NoDataErrorif data is not available yet
Example:
Last updated
