You’re on an on-call rotation for a fintech event-ingestion pipeline that processes tens of millions of events per day. Downstream fraud and compliance systems deduplicate events by hashing a canonical string representation of each payload. Unfortunately, partners send payloads with inconsistent whitespace, optional null fields, and non-deterministic object key order.
Write a function canonicalize_payload(payload) that converts a JSON-like Python object into a deterministic string with the exact formatting rules below.
payload is one of:
dict[str, Any]list[Any]str, int, float, bool, or NoneNone.{k1:v1,k2:v2} with no spaces."id" and "tags" (i.e., output "id":... and "tags":...). All other keys are unquoted.[e1,e2,...] with no spaces.None -> nullTrue/False -> true/false (lowercase)str(x).Example 1
{"id": " evt_1 ", "meta": {"b": 2, "a": 1}, "tags": [" vip", "vip ", "new"], "note": None}{"id":"evt_1","meta":{a:1,b:2},"tags":["vip","vip","new"]}note, sort meta keys, normalize string whitespace, preserve list order, and quote only id and tags keys.Example 2
[{"k": "a b"}, {"k": "a b"}, None, True][{k:"a b"},{k:"a b"},null,true]"a b"; array order is preserved; None and True become null and true.None values, it must render as {}.None inside a list is not dropped; it renders as null.