# JSON Schema-Drift Checker — Delivery **File:** `drift_check.py` (single file, standard library only, 92 lines) ## What it does Fetches JSON from two public endpoints and reports field-level schema drift: added fields, removed fields, and changed value types. Objects are compared recursively (nested at least one level deep); arrays are compared by their element type. ## CLI ``` python3 drift_check.py URL1 URL2 ``` Prints a readable drift report to stdout. **Exit 0** = no drift, **exit 1** = drift found, exit 2 = fetch/usage error. ## Script ```python #!/usr/bin/env python3 """drift_check.py — report field-level schema drift between two JSON endpoints. Usage: python3 drift_check.py URL1 URL2 Exit 0 if no drift, 1 if drift found. Standard library only. """ import json import sys import urllib.request def fetch(url): req = urllib.request.Request(url, headers={"User-Agent": "drift-check/1.0", "Accept": "application/json"}) with urllib.request.urlopen(req, timeout=20) as r: return json.loads(r.read().decode("utf-8")) def kind(v): if v is None: return "null" if isinstance(v, bool): return "boolean" if isinstance(v, dict): return "object" if isinstance(v, list): return "array" if isinstance(v, (int, float)): return "number" return "string" def element_schema(arr): """Represent an array by the schema of its first element (recursively).""" if not arr: return "array" return "array<" + kind(arr[0]) + ">" def compare(a, b, path, out): ka, kb = kind(a), kind(b) if ka != kb: out.append(("TYPE", path, ka, kb)) return if ka == "object": for key in sorted(set(a) | set(b)): p = f"{path}.{key}" if path else key if key not in b: out.append(("REMOVED", p, kind(a[key]), "-")) elif key not in a: out.append(("ADDED", p, "-", kind(b[key]))) else: compare(a[key], b[key], p, out) elif ka == "array": ea = element_schema(a) if isinstance(a, list) else ka eb = element_schema(b) if isinstance(b, list) else kb if ea != eb: out.append(("TYPE", path + "[]", ea, eb)) def main(): if len(sys.argv) != 3: print(__doc__) return 2 u1, u2 = sys.argv[1], sys.argv[2] try: a = fetch(u1) b = fetch(u2) except Exception as exc: # noqa: BLE001 print(f"FETCH ERROR: {exc}") return 2 drifts = [] compare(a, b, "", drifts) print(f"A: {u1}") print(f"B: {u2}") if not drifts: print("NO SCHEMA DRIFT") return 0 print(f"SCHEMA DRIFT FOUND: {len(drifts)} difference(s)") for tag, path, x, y in drifts: if tag == "TYPE": print(f" [TYPE] {path}: {x} -> {y}") elif tag == "ADDED": print(f" [ADDED] {path}: only in B ({y})") else: print(f" [REMOVED] {path}: only in A ({x})") return 1 if __name__ == "__main__": sys.exit(main()) ``` ## Verified sample run Both endpoints are public GitHub REST API endpoints — no keys, no sign-up. ``` $ python3 drift_check.py https://api.github.com/users/torvalds https://api.github.com/orgs/openai A: https://api.github.com/users/torvalds B: https://api.github.com/orgs/openai SCHEMA DRIFT FOUND: 23 difference(s) [ADDED] archived_at: only in B (null) [REMOVED] bio: only in A (null) [TYPE] company: string -> null [ADDED] description: only in B (string) [REMOVED] followers_url: only in A (string) [REMOVED] following_url: only in A (string) [REMOVED] gists_url: only in A (string) [REMOVED] gravatar_id: only in A (string) [ADDED] has_organization_projects: only in B (boolean) [ADDED] has_repository_projects: only in B (boolean) [REMOVED] hireable: only in A (null) [ADDED] hooks_url: only in B (string) [ADDED] is_verified: only in B (boolean) [ADDED] issues_url: only in B (string) [TYPE] location: string -> null [ADDED] members_url: only in B (string) [REMOVED] organizations_url: only in A (string) [ADDED] public_members_url: only in B (string) [REMOVED] received_events_url: only in A (string) [REMOVED] site_admin: only in A (boolean) [REMOVED] starred_url: only in A (string) [REMOVED] subscriptions_url: only in A (string) [REMOVED] user_view_type: only in A (string) $ echo $? 1 ``` A second run confirms the "no drift" path returns exit 0: ``` $ python3 drift_check.py https://api.github.com/repos/pydantic/pydantic-ai https://api.github.com/repos/psf/requests A: https://api.github.com/repos/pydantic/pydantic-ai B: https://api.github.com/repos/psf/requests NO SCHEMA DRIFT $ echo $? 0 ``` ## Edge cases (5 bullets) - **Arrays** are compared by the type of their first element only, not element-by-element; an empty array reports `array`. - **Key order is irrelevant** — object keys are sorted, so reordering never counts as drift. - **`null` vs missing are distinct**: a field that is `null` in A and absent in B reports as one type drift or removal, not silently equal. - **Recursion depth** covers nested objects fully, but objects *inside* arrays are not descended into (only the array's element type is compared). - **Network / non-JSON responses** never crash the tool: failures print `FETCH ERROR` and exit 2, keeping stdout parseable for callers.