extract_json_items#

extract_json_items(text: str | list) list[source]#

Extracts and parses all JSON objects or arrays from code blocks in the input text.

Parameters:
textstr

The input text containing JSON enclosed within triple backticks with json syntax highlighting.

Returns:
list

A list containing all parsed JSON objects or arrays found in the code blocks. Returns an empty list if no valid JSON is found or parsing fails.

Note

If a JSON code block is invalid, that specific block will be skipped but the function will continue processing other valid JSON code blocks.

Warning

Ensure that the input text contains properly formatted JSON in code blocks. The repair_json package is used to help support noncompliant JSON.

Examples

>>> text = '''
Some intro text.
```json
{
    "key1": "value1",
    "key2": "value2"
}
```
More text between code blocks.
```json
{
    "key3": "value3",
    "key4": "value4"
}
```
'''
>>> extract_json_items(text)
[{'key1': 'value1', 'key2': 'value2'}, {'key3': 'value3', 'key4': 'value4'}]