Extract JSON object from text, looking in various formats and locations. Args: text: The text that may contain JSON Returns: dict: Parsed JSON object if found, None otherwise
(text)
| 10 | |
| 11 | |
| 12 | def extract_json_from_text(text): |
| 13 | """ |
| 14 | Extract JSON object from text, looking in various formats and locations. |
| 15 | |
| 16 | Args: |
| 17 | text: The text that may contain JSON |
| 18 | |
| 19 | Returns: |
| 20 | dict: Parsed JSON object if found, None otherwise |
| 21 | """ |
| 22 | try: |
| 23 | # First, try to extract JSON from markdown code blocks (with or without language tag) |
| 24 | json_matches = [ |
| 25 | re.search(r'```json\s*(.*?)\s*```', text, re.DOTALL), |
| 26 | re.search(r'```\s*(\{.*?\})\s*```', text, re.DOTALL) |
| 27 | ] |
| 28 | |
| 29 | for json_match in json_matches: |
| 30 | if json_match: |
| 31 | try: |
| 32 | return json.loads(json_match.group(1)) |
| 33 | except json.JSONDecodeError: |
| 34 | continue |
| 35 | |
| 36 | # If no JSON found in code blocks, try to find JSON objects anywhere in the text |
| 37 | # Find all potential JSON objects (looking for balanced braces) |
| 38 | brace_count = 0 |
| 39 | json_start = -1 |
| 40 | for i, char in enumerate(text): |
| 41 | if char == '{': |
| 42 | if brace_count == 0: |
| 43 | json_start = i |
| 44 | brace_count += 1 |
| 45 | elif char == '}': |
| 46 | brace_count -= 1 |
| 47 | if brace_count == 0 and json_start != -1: |
| 48 | # Found a complete JSON object |
| 49 | potential_json = text[json_start:i+1] |
| 50 | try: |
| 51 | return json.loads(potential_json) |
| 52 | except json.JSONDecodeError: |
| 53 | # This wasn't valid JSON, continue looking |
| 54 | continue |
| 55 | except Exception: |
| 56 | pass |
| 57 | |
| 58 | return None |
| 59 | |
| 60 | |
| 61 | def parse_json_with_fallbacks(text, error_context=""): |
no outgoing calls