Skip to content

extract_json_objects

fastagency.helpers.extract_json_objects #

extract_json_objects(
    text: str, decoder: Optional[JSONDecoder] = None
) -> Iterator[str]
Source code in fastagency/helpers.py
def extract_json_objects(
    text: str, decoder: Optional[JSONDecoder] = None
) -> Iterator[str]:
    decoder = decoder or JSONDecoder()
    pos = 0
    while True:
        # print(f"matching: {text[pos:]}")
        match = text.find("{", pos)
        if match == -1:
            yield text[pos:]  # return the remaining text
            break
        yield text[pos:match].rstrip(" ")  # modification for the non-JSON parts
        try:
            result, index = decoder.raw_decode(text[match:])
            yield result
            pos = match + index
            # move past space characters if needed
            while pos < len(text) and text[pos] == " ":
                pos += 1
        except ValueError:
            yield text[match]
            pos = match + 1