defjsonify_string(line:str)->str:line_parts:list[str]=[]forresultinextract_json_objects(line):ifisinstance(result,dict):# got a JSON objline_parts.append(f"\n```\n{json.dumps(result,indent=4)}\n```\n")else:# got text/non-JSON-objline_parts.append(result)# (don't make that a list comprehension, quite un-readable)return"".join(line_parts)