Parse JSON string into JSON with both json and demjson
(string)
| 820 | |
| 821 | |
| 822 | def parse_json(string) -> dict: |
| 823 | """Parse JSON string into JSON with both json and demjson""" |
| 824 | result = None |
| 825 | try: |
| 826 | result = json_loads(string) |
| 827 | if not isinstance(result, dict): |
| 828 | raise ValueError(f"JSON from string input ({string}) is not a dictionary (type {type(result)}): {result}") |
| 829 | return result |
| 830 | except Exception as e: |
| 831 | print(f"Error parsing json with json package, falling back to demjson: {e}") |
| 832 | |
| 833 | try: |
| 834 | result = demjson.decode(string) |
| 835 | if not isinstance(result, dict): |
| 836 | raise ValueError(f"JSON from string input ({string}) is not a dictionary (type {type(result)}): {result}") |
| 837 | return result |
| 838 | except demjson.JSONDecodeError as e: |
| 839 | print(f"Error parsing json with demjson package (fatal): {e}") |
| 840 | raise e |
| 841 | |
| 842 | |
| 843 | def validate_function_response(function_response_string: any, return_char_limit: int, strict: bool = False, truncate: bool = True) -> str: |
no test coverage detected