How to diff two daily JSON snapshots in Python (added, removed, changed) and the 3 traps that make a diff lie
I keep a small daily archive of public data: a German federal tender RSS feed, two DeFiLlama endpoints, BaFin feeds and the MCP Registry, one gzipped JSON file per source per day. Collecting is the easy half. The useful question is always "what changed since yesterday?", and a naive answer to that is wrong more often than I expected.
This post is the script I now use for it, real output from my own archive, and three traps that made early versions of the diff report changes that weren't real.
Disclosure: I run small data pipelines and AI-agent automations; the archive and the numbers below are mine. The script is in a public repo, linked at the end. Nothing here is investment advice; the crypto numbers are only example data.
What the script does
snapshot_diff.py takes two files (plain or .json.gz, a bare list or {"data": [...]}), matches records by a key you choose, and reports:
| Output | Meaning |
|---|---|
| added / removed | keys present in only one of the two days |
| changed (other fields) | same key, but any non-ignored field differs |
| biggest absolute / relative changes | ranked by one numeric field (--num) |
| exit code | 0 = identical, 1 = something changed, so cron or CI can react |
The core is set arithmetic on the keys:
old, new = index(load(a.old), a.key), index(load(a.new), a.key) added = sorted(new.keys() - old.keys(), key=str) removed = sorted(old.keys() - new.keys(), key=str) skip = set(a.ignore) | ({a.num} if a.num else set()) changed = [k for k in old.keys() & new.keys() if {f: v for f, v in old[k].items() if f not in skip} != {f: v for f, v in new[k].items() if f not in skip}] The numeric field is excluded from the "changed" check on purpose. Otherwise every record whose value moved by a cent shows up as "changed" and hides the structural changes you actually care about. Numbers get their own ranked section instead, with a --min-base filter so a record going from 1 to 3 doesn't top the relative list at +200%.
Example 1: a tender feed, 29 vs 30 November
$ python snapshot_diff.py bund_tenders_rss/2026-11-29.json.gz bund_tenders_rss/2026-11-30.json.gz --key guid old: 100 new: 100 added: 77 removed: 77 changed (other fields): 0 77 of 100 entries "removed" in one day looks dramatic. It isn't. Which brings me to the first trap.
Trap 1: "removed" from a window is not "deleted"
The feed always returns the newest 100 notices. I checked the publication dates inside both files: the 29 November snapshot covers about 29 hours (27 Nov 12:42 to 28 Nov 17:18), the 30 November one roughly 52 hours over a weekend. The 77 "removed" tenders were not withdrawn; they simply scrolled out of a fixed-size window.
Two consequences:
- For windowed sources (RSS, "latest N" endpoints), read removed as "no longer visible here", never as "cancelled".
- If more than N items appear between two snapshots, you lose some for good. A weekday with more than 100 notices would leave gaps that no diff can show. Either fetch more often or page further back.
Example 2: DeFiLlama chains, one day apart
$ python snapshot_diff.py defillama_chains/2026-11-29.json.gz defillama_chains/2026-11-30.json.gz \ --key name --num tvl --top 5 --min-base 100000000 old: 426 new: 426 added: 0 removed: 0 changed (other fields): 0 tvl: total old 67,433,861,449 new 68,384,164,183 biggest absolute changes in tvl: Base 4,601,370,584 -> 5,000,741,094 (+399,370,510) Ethereum 36,131,243,511 -> 36,463,428,687 (+332,185,177) Hyperliquid L1 2,236,031,188 -> 2,324,255,175 (+88,223,988) Clean: same 426 chains, no structural change, a handful of value moves.
Trap 2: a type change looks like a data change
Comparing 12 November with 29 November, the same command reported 6 changed chains. I looked at the actual differences:
| Chain | Field | 12 Nov | 29 Nov |
|---|---|---|---|
| SatoshiVM | chainId | "3109" (string) | 3109 (number) |
| Evmos | chainId | "9001" | 9001 |
| K2 | chainId | "2410" | 2410 |
| HAQQ | chainId | "11235" | 11235 |
| Elysium | chainId | null | 99901 |
| Polygon | tokenSymbol | POL | MATIC |
Four of six "changes" are the API switching an ID from string to integer. Same value, different JSON type. One is a missing value being filled, and only one (Polygon's token symbol) is a real content change. If you alert on "changed", decide up front whether "3109" and 3109 are equal for you. If they are, normalise types before comparing, or put the field in --ignore.
Trap 3: a value field can move without anything "happening"
Over the same 17 days, total TVL across the matched chains fell from about 78.0 to 67.4 billion USD. It's tempting to read that as money leaving. But DeFiLlama's TVL is denominated in USD, so it moves with token prices even if no one deposits or withdraws anything. A diff tells you that a number changed, not why. For anything priced in a currency, compare against a price series, or diff a quantity that doesn't depend on price, before you write a sentence about flows.
The same applies to the stablecoin endpoint: between 12 and 29 November the diff showed 18 newly listed stablecoins and 0 removed. That is a fact about the list, not about adoption. A new entry can be a new coin or just a newly tracked one.
Running it on a schedule
Because of the exit code, it slots into a daily job:
python snapshot_diff.py "archive/src/$YESTERDAY.json.gz" "archive/src/$TODAY.json.gz" \ --key id --ignore fetchedAt updatedAt > diff.txt \ || mail -s "src changed" me@example.com < diff.txt Pair it with a gap check (I use a small archive gap scanner). A diff between two days only means something if both days exist.
Takeaways
- Pick a stable key (
guid,id, URL). Names can change; if you must use them, watch the "added" plus "removed" pairs for renames. - Keep numeric movement separate from structural change.
- Windowed sources make "removed" meaningless; check the time span each snapshot covers.
- Normalise types before comparing, or accept that format changes will page you.
- A changed number is a question, not an answer.
The script (standard library only, Python 3.9+) is in data-recipes/snippets.
Question: when you diff API snapshots, do you normalise types and units first, or keep the raw diff and filter afterwards? I've gone back and forth, and I'm curious what held up for you.