Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()
(value: str)
| 55 | return None |
| 56 | |
| 57 | def parse_value(value: str): |
| 58 | """Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()'""" |
| 59 | value = value.strip() |
| 60 | |
| 61 | if value == "null": |
| 62 | return None |
| 63 | elif value == "true": |
| 64 | return True |
| 65 | elif value == "false": |
| 66 | return False |
| 67 | else: |
| 68 | # Try to convert to int or float |
| 69 | try: |
| 70 | if '.' in value: # If there's a dot, it might be a float |
| 71 | return float(value) |
| 72 | else: |
| 73 | return int(value) |
| 74 | except ValueError: |
| 75 | # If conversion fails, return the value as-is (likely a string) |
| 76 | return value.strip('"') # Remove surrounding quotes if they exist |
| 77 | |
| 78 | def extract_values_from_json(json_string, keys=["reasoning", "answer", "data"], allow_no_quotes=False): |
| 79 | """Extract key values from a non-standard or malformed JSON string, handling nested objects.""" |
no outgoing calls
no test coverage detected