Skip to content

Instantly share code, notes, and snippets.

@Muazzeem
Created May 8, 2024 15:58
Show Gist options
  • Save Muazzeem/7f5054311eea81cf28fb0cc93dea6944 to your computer and use it in GitHub Desktop.
Save Muazzeem/7f5054311eea81cf28fb0cc93dea6944 to your computer and use it in GitHub Desktop.
def parse_json_from_text(text):
# Removing newline characters and backticks
clean_text = text.replace('\n', '').replace('`', '')
# Extracting JSON part
json_start_index = clean_text.find('{')
json_string = clean_text[json_start_index:]
# Parsing JSON
data = json.loads(json_string)
return data
## Uses
text = responses.candidates[0].content.parts[0].text
model_res = parse_json_from_text(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment