diff --git a/fieldExtraction/src/utils/string_utils.py b/fieldExtraction/src/utils/string_utils.py index 2e2aac1..272cae0 100644 --- a/fieldExtraction/src/utils/string_utils.py +++ b/fieldExtraction/src/utils/string_utils.py @@ -268,11 +268,13 @@ def universal_json_load(string_dict: str): ValueError: If no valid JSON object is found in the input string. In addition, the traceback as well as the offending string are printed. + Note: if more than one json-like (or list-like) is found in the input string, this function will + take the last one positionally. """ # Try to match dictionary or list of dictionaries first - dict_match = re.search(r"\{.*\}|\[\s*?\{.*\}\s*?\]", string_dict, re.DOTALL) - if dict_match: - matched_str = dict_match.group() + dict_matches = re.findall(r"\{.*\}|\[\s*?\{.*\}\s*?\]", string_dict, re.DOTALL) + if dict_matches: + matched_str = dict_matches[-1] # Take the last match in the string try: return json.loads(matched_str) except json.JSONDecodeError as e: @@ -282,9 +284,9 @@ def universal_json_load(string_dict: str): raise ValueError(f"JSON parsing failed: {e.msg} at position {e.pos}") from e # If no dict pattern found, try to match list of strings - list_match = re.search(r"\[(.*?)\]", string_dict, re.DOTALL) - if list_match: - matched_str = list_match.group() + list_matches = re.findall(r"\[(.*?)\]", string_dict, re.DOTALL) + if list_matches: + matched_str = f"[{list_matches[-1]}]" # Take the last match in the string try: # Clean up the string list before parsing cleaned_str = re.sub(r'(?