Skip to content
GreenSnippets

Fetch JSON with the standard library

A small GET helper using urllib with a timeout, status check, size limit and JSON decoding errors.

Python
import json
import urllib.request


def get_json(url, timeout=10, max_bytes=5_000_000, headers=None):
    request = urllib.request.Request(
        url, headers={"Accept": "application/json", "User-Agent": "example-client/1.0", **(headers or {})}
    )
    with urllib.request.urlopen(request, timeout=timeout) as response:
        raw = response.read(max_bytes + 1)
    if len(raw) > max_bytes:
        raise ValueError("response too large")
    return json.loads(raw)

Example / expected output

Output
get_json("https://api.github.com/repos/python/cpython")["full_name"]  # 'python/cpython'

How it works

No third-party package needed for simple GET requests. Always set a timeout (the default is none, so a stalled server blocks forever), send an explicit User-Agent and cap how much you read so a huge response cannot exhaust memory. For anything complex, requests or httpx are friendlier.