parse_json
langroid/parsing/parse_json.py
is_valid_json(json_str)
¶
Check if the input string is a valid JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_str |
str
|
The input string to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the input string is a valid JSON, False otherwise. |
Source code in langroid/parsing/parse_json.py
flatten(nested_list)
¶
Flatten a nested list into a single list of strings
Source code in langroid/parsing/parse_json.py
get_json_candidates(s)
¶
Get top-level JSON candidates, i.e. strings between curly braces.
Source code in langroid/parsing/parse_json.py
try_repair_json_yaml(s)
¶
Attempt to load as json, and if it fails, try repairing the JSON. If that fails, replace any with space as a last resort. NOTE - replacing with space will result in format loss, which may matter in generated code (e.g. python, toml, etc)
Source code in langroid/parsing/parse_json.py
extract_top_level_json(s)
¶
Extract all top-level JSON-formatted substrings from a given string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
The input string to search for JSON substrings. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: A list of top-level JSON-formatted substrings. |
Source code in langroid/parsing/parse_json.py
top_level_json_field(s, f)
¶
Extract the value of a field f from a top-level JSON object. If there are multiple, just return the first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
The input string to search for JSON substrings. |
required |
f |
str
|
The field to extract from the JSON object. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
Any
|
The value of the field f in the top-level JSON object, if any. Otherwise, return an empty string. |