JSON
Parse JSON responses — r.json() + staged .get traversal
r.json() parses the response body and returns the corresponding Python structure: a JSON object becomes a dictionary, a JSON array becomes a list, and primitive JSON values become their Python equivalents. Invalid JSON raises requests.exceptions.JSONDecodeError.
dictionary.get("key") returns the stored object or None when the key is absent. A second argument supplies another default. Storing each nested level in a separate variable makes its type visible before continuing. A list must contain an element before index 0 can be read. The explicit loop stores the first dictionary whose method key contains GET.
json.loads(text) performs the same parsing for an existing string. json.dumps(obj, indent=2) performs the opposite operation and formats a Python object as readable JSON text.
import json
r = s.get(url=POLL_URL, verify=False, proxies=PROXIES, timeout=10)
data = r.json() # JSON body -> dict/list (raises on non-JSON body)
token = data.get('uuid') # safe key read: None if absent (no KeyError)
status = data.get('status', 'unknown') # ...with a fallback default
meta = data.get('meta', {})
host = meta.get('host')
# value buried in a list-of-objects -- guard the empty list BEFORE indexing [0]:
reqs = data.get('data', [])
if reqs:
first = reqs[0]
else:
first = {}
query = first.get('query', {})
out = query.get('data') # e.g. the exfiltrated value
# first list item matching a predicate (None if none match):
hit = None
for x in reqs:
if x.get('method') == 'GET':
hit = x
break
# parse a JSON *string*, and pretty-print any object to eyeball its shape:
obj = json.loads(r.text)
print(json.dumps(obj, indent=2))Sample JSON body the calls target
{
"uuid": "1b2c3d4e",
"status": "ok",
"data": [
{ "method": "GET", "query": { "data": "726f6f74" } }
]
}What each variable holds
token -> '1b2c3d4e'
status -> 'ok'
host -> None (no 'meta' key, default {} then .get -> None)
out -> '726f6f74' (data[0]['query']['data'])
hit -> {'method': 'GET', 'query': {...}}Find by: json, parse json, r.json, response json, api response, dict, get key, default value, keyerror, indexerror, jsondecodeerror, nested, traversal, list of objects, index guard, first match, find in list, predicate, json.loads, json.dumps, pretty print, navigate, extract field, webhook, poll, oob exfil · Source: HTB/VoidWhispers