Skip to content

Instantly share code, notes, and snippets.

@arose13
Created January 23, 2025 16:53
Show Gist options
  • Save arose13/2eaa5bc5d773e07966f5e24d08f654cc to your computer and use it in GitHub Desktop.
Save arose13/2eaa5bc5d773e07966f5e24d08f654cc to your computer and use it in GitHub Desktop.
import re
def extract_json(text):
"""
Extracts the first JSON object from a given string.
Args:
text (str): The input string containing JSON.
Returns:
str: The extracted JSON as a string, or None if no JSON is found.
"""
regex = r"\{(?:[^{}]|\"(?:\\.|[^\"\\])*\"|(?R))*\}"
match = re.search(regex, text)
return match.group(0) if match else None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment