json_path
json_path
¶
Lightweight JSON dot-path extractor for instrument output parsing.
Used by the instrument plugin system (PluginCliBackend) to extract result text, error messages, and token counts from CLI instrument output. This is NOT full JSONPath — it's a minimal extractor covering the patterns observed across all researched CLI instruments.
Supported syntax
key — top-level key key.subkey — nested access key[0] — array index key. — wildcard: iterate all values, return first match key..subkey — wildcard with nested access
Examples:
extract_json_path(data, "result") → data["result"] extract_json_path(data, "error.message") → data["error"]["message"] extract_json_path(data, "items[0]") → data["items"][0] extract_json_path(data, "models.*.tokens") → first data["models"][k]["tokens"]
Functions¶
extract_json_path
¶
Extract a value from nested data using a dot-path.
Returns the first matching value, or None if the path doesn't resolve. Wildcards (*) iterate all values in a dict and return the first match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Parsed JSON data (typically a dict). |
required |
path
|
str
|
Dot-separated path string, e.g. "error.message", "models.*.tokens". |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The extracted value, or None if not found. |
Source code in src/marianne/utils/json_path.py
extract_json_path_all
¶
Extract ALL matching values from nested data using a dot-path.
Like extract_json_path but collects all wildcard matches instead of returning only the first. Useful for aggregating token counts across multiple models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Parsed JSON data (typically a dict). |
required |
path
|
str
|
Dot-separated path string with wildcards. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of all matching values (may be empty). |