# JSON Schema Drift Checker — Delivery (tsk_9918cf95e8eb) ## Overview `drift_check.py` is a single-file, standard-library-only Python 3 script. It fetches JSON from two no-auth public endpoints, flattens each document into dotted paths with type labels (recursing through nested objects at every level and one level into arrays), and reports added fields, removed fields, and changed value types. Exit codes: `0` = no drift, `1` = drift found, `2` = usage/network error. ## Script (`survival-lab/drift_check.py`, 110 lines) ```python #!/usr/bin/env python3 """Field-level JSON schema drift checker for two no-auth public API endpoints. Usage: python drift_check.py URL1 URL2 Exit code: 0 = no drift, 1 = drift found, 2 = usage/network error. """ import json import sys import urllib.request def fetch_json(url): """Fetch and decode JSON from a URL using only the standard library.""" req = urllib.request.Request(url, headers={"User-Agent": "drift-check/1.0"}) with urllib.request.urlopen(req, timeout=20) as resp: return json.load(resp) def type_name(value): """Map a decoded JSON value to a stable, readable type label.""" if value is None: return "null" if isinstance(value, bool): return "bool" if isinstance(value, int): return "int" if isinstance(value, float): return "float" if isinstance(value, str): return "str" if isinstance(value, dict): return "object" if isinstance(value, list): return "array" return type(value).__name__ def flatten(value, prefix="", out=None): """Flatten JSON into {dotted_path: type_label}. Recurses through nested objects at every level and one level into arrays, so drift inside nested objects is detected rather than only at the top. """ if out is None: out = {} if isinstance(value, dict): for key, val in value.items(): path = f"{prefix}.{key}" if prefix else str(key) out[path] = type_name(val) if isinstance(val, dict): flatten(val, path, out) elif isinstance(val, list) and val: first = val[0] out[path + "[]"] = type_name(first) if isinstance(first, dict): for k, v in first.items(): out[f"{path}[].{k}"] = type_name(v) return out def report(a, b, url1, url2): """Print a human-readable drift report; return True if any drift exists.""" added = sorted(set(b) - set(a)) removed = sorted(set(a) - set(b)) changed = sorted(p for p in set(a) & set(b) if a[p] != b[p]) print("=" * 64) print("JSON SCHEMA DRIFT REPORT") print("=" * 64) print(f"URL1: {url1}") print(f"URL2: {url2}") print(f"Common fields: {len(set(a) & set(b))} | " f"Removed: {len(removed)} | Added: {len(added)} | Type-changed: {len(changed)}") print("-" * 64) print(f"\n[ADDED] {len(added)} field(s) present only in URL2:") for p in added: print(f" + {p} : {b[p]}") print(f"\n[REMOVED] {len(removed)} field(s) present only in URL1:") for p in removed: print(f" - {p} : {a[p]}") print(f"\n[CHANGED TYPE] {len(changed)} field(s) with different types:") for p in changed: print(f" ~ {p} : {a[p]} -> {b[p]}") drift = bool(added or removed or changed) print("\n" + "=" * 64) print("RESULT: " + ("DRIFT DETECTED" if drift else "NO DRIFT")) print("=" * 64) return drift def main(): if len(sys.argv) != 3: print("Usage: python drift_check.py URL1 URL2", file=sys.stderr) return 2 url1, url2 = sys.argv[1], sys.argv[2] try: a = flatten(fetch_json(url1)) b = flatten(fetch_json(url2)) except Exception as exc: print(f"error: could not fetch/parse JSON: {exc}", file=sys.stderr) return 2 return 1 if report(a, b, url1, url2) else 0 if __name__ == "__main__": sys.exit(main()) ``` ## Verified sample run (real endpoints, real output) Command (exact): ```bash python drift_check.py "https://api.frankfurter.dev/v1/latest?base=USD&symbols=CNY" "https://open.er-api.com/v6/latest/USD" ``` Actual stdout (verbatim capture from the real run; process exit code `1`): ```text ================================================================ JSON SCHEMA DRIFT REPORT ================================================================ URL1: https://api.frankfurter.dev/v1/latest?base=USD&symbols=CNY URL2: https://open.er-api.com/v6/latest/USD Common fields: 2 | Removed: 3 | Added: 175 | Type-changed: 0 ---------------------------------------------------------------- [ADDED] 175 field(s) present only in URL2: + base_code : str + documentation : str + provider : str + rates.AED : float + rates.AFN : float + rates.ALL : float + rates.AMD : float + rates.ANG : float + rates.AOA : float + rates.ARS : float + rates.AUD : float + rates.AWG : float + rates.AZN : float + rates.BAM : float + rates.BBD : int + rates.BDT : float + rates.BGN : float + rates.BHD : float + rates.BIF : float + rates.BMD : int + rates.BND : float + rates.BOB : float + rates.BRL : float + rates.BSD : int + rates.BTN : float + rates.BWP : float + rates.BYN : float + rates.BZD : int + rates.CAD : float + rates.CDF : float + rates.CHF : float + rates.CLF : float + rates.CLP : float + rates.CNH : float + rates.COP : float + rates.CRC : float + rates.CUP : int + rates.CVE : float + rates.CZK : float + rates.DJF : float + rates.DKK : float + rates.DOP : float + rates.DZD : float + rates.EGP : float + rates.ERN : int + rates.ETB : float + rates.EUR : float + rates.FJD : float + rates.FKP : float + rates.FOK : float + rates.GBP : float + rates.GEL : float + rates.GGP : float + rates.GHS : float + rates.GIP : float + rates.GMD : float + rates.GNF : float + rates.GTQ : float + rates.GYD : float + rates.HKD : float + rates.HNL : float + rates.HRK : float + rates.HTG : float + rates.HUF : float + rates.IDR : float + rates.ILS : float + rates.IMP : float + rates.INR : float + rates.IQD : float + rates.IRR : float + rates.ISK : float + rates.JEP : float + rates.JMD : float + rates.JOD : float + rates.JPY : float + rates.KES : float + rates.KGS : float + rates.KHR : float + rates.KID : float + rates.KMF : float + rates.KRW : float + rates.KWD : float + rates.KYD : float + rates.KZT : float + rates.LAK : float + rates.LBP : int + rates.LKR : float + rates.LRD : float + rates.LSL : float + rates.LYD : float + rates.MAD : float + rates.MDL : float + rates.MGA : float + rates.MKD : float + rates.MMK : float + rates.MNT : float + rates.MOP : float + rates.MRU : float + rates.MUR : float + rates.MVR : float + rates.MWK : float + rates.MXN : float + rates.MYR : float + rates.MZN : float + rates.NAD : float + rates.NGN : float + rates.NIO : float + rates.NOK : float + rates.NPR : float + rates.NZD : float + rates.OMR : float + rates.PAB : int + rates.PEN : float + rates.PGK : float + rates.PHP : float + rates.PKR : float + rates.PLN : float + rates.PYG : float + rates.QAR : float + rates.RON : float + rates.RSD : float + rates.RUB : float + rates.RWF : float + rates.SAR : float + rates.SBD : float + rates.SCR : float + rates.SDG : float + rates.SEK : float + rates.SGD : float + rates.SHP : float + rates.SLE : float + rates.SLL : float + rates.SOS : float + rates.SRD : float + rates.SSP : float + rates.STN : float + rates.SYP : float + rates.SZL : float + rates.THB : float + rates.TJS : float + rates.TMT : float + rates.TND : float + rates.TOP : float + rates.TRY : float + rates.TTD : float + rates.TVD : float + rates.TWD : float + rates.TZS : float + rates.UAH : float + rates.UGX : float + rates.USD : int + rates.UYU : float + rates.UZS : float + rates.VES : float + rates.VND : float + rates.VUV : float + rates.WST : float + rates.XAF : float + rates.XCD : float + rates.XCG : float + rates.XDR : float + rates.XOF : float + rates.XPF : float + rates.YER : float + rates.ZAR : float + rates.ZMW : float + rates.ZWG : float + rates.ZWL : float + result : str + terms_of_use : str + time_eol_unix : int + time_last_update_unix : int + time_last_update_utc : str + time_next_update_unix : int + time_next_update_utc : str [REMOVED] 3 field(s) present only in URL1: - amount : float - base : str - date : str [CHANGED TYPE] 0 field(s) with different types: ================================================================ RESULT: DRIFT DETECTED ================================================================ ``` Both endpoints are public, require no API key and no sign-up. Frankfurter (ECB exchange-rate data) and open.er-api.com return structurally different schemas; this real pair yields 3 removed fields, 175 added fields and 0 type-changed fields, so the added/removed reporting path is exercised end to end. The changed-type path is implemented in `report()` and is triggered whenever a dotted path exists on both sides with different type labels. ## Edge cases - Network errors, HTTP errors and malformed JSON exit with code `2`, never `1`, so a transport failure cannot be mistaken for genuine drift. - JSON `int` and `float` are distinct labels, so `1` vs `1.0` is reported as a type change even though the values are numerically equal. - Arrays are inspected one level deep through their first element only; a heterogeneous array can hide drift in later elements. - Only structure (paths + type labels) is compared, not values: a rate that changes value but keeps its type is intentionally not reported as drift. - Key order is ignored because paths are collected in dicts/sets, and all lists are sorted so the report is stable and diff-friendly.