How to extract json data from a string in Python? Let's say the input is "{'key':…
- User
How to extract json data from a string in Python? Let's say the input is "{'key': 'value'}".
- Assistant
Below are a few common scenarios and the corresponding “recipes” for turning the string you have into an actual Python object.
1. The string really is valid JSON
(e.g. it uses double quotes like
{"key": "value"}).import json raw = '{"key": "value"}' data = json.loads(raw) print(data) # {'key': 'value'} print(data['key']) # value
2. The string is Python-style (single quotes)
(e.g.
"{'key': 'value'}").
This isn’t valid JSON, sojson.loads()will choke. Two quick fixes:2-a. Safest: use
ast.literal_evalimport ast raw = "{'key': 'value'}" data = ast.literal_eval(raw) # safely evaluates basic Python literals print(data) # {'key': 'value'}ast.literal_eval()only understands literals (dicts, lists, numbers, strings, booleans, None) so it can’t execute arbitrary code—making it safer thaneval().2-b. Quick-n-dirty: swap quotes then load
import json raw = "{'key': 'value'}" json_ready = raw.replace("'", '"') data = json.loads(json_ready)Only use this if you’re sure your values don’t contain legitimate single quotes themselves.
3. The JSON is embedded inside other text
Imagine:
raw = 'Error-123: response={"key": "value", "num": 42} END'Minimalistic extraction (works for simple, flat JSON blocks):
import re, json m = re.search(r'\{.*?\}', raw) # first {...} non-greedy if not m: raise ValueError("No JSON found") data = json.loads(m.group(0))For nested/bracketed JSON or multiple objects, consider a streaming parser such as
ijson, or a third-party package likejsonfinder.
4. Non-standard JSON (trailing commas, comments, etc.)
Use a lenient parser instead of the stdlib one, e.g.:
pip install json5import json5 raw = "{'key': 'value',}" # trailing comma data = json5.loads(raw)
TL;DR
• Valid JSON →
json.loads()
• Python-literal dict →ast.literal_eval()
• Dirty quick fix → replace quotes or use a tolerant parser (e.g.json5)
• JSON buried in text → regex / streaming parser to isolate, thenjson.loads()Pick the approach that matches the exact shape of your input. 👍
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.