I was pasting 4KB of JSON into every prompt. So I built a key-folder that shrinks it — reversibly.
I was pasting 4KB of JSON into every prompt. So I built a key-folder that shrinks it — reversibly. Every time I debug against an API, the loop is the same: curl something, get a wall of JSON, paste it into a prompt, and watch a third of my context window evaporate into "repository_owner_login" repe

Posted on Oct 21 • Originally published at github.com
Every time I debug against an API, the loop is the same: curl something, get a wall of JSON, paste it into a prompt, and watch a third of my context window evaporate into "repository_owner_login" repeated forty times.
jq -c helps — it strips the whitespace. But whitespace was never the real problem. The real problem is the keys. In a typical API response, object keys are 20–40% of the bytes, and the same long names repeat on every single object in every array.
There are lossy "AI compressors" that will happily rewrite your JSON into something smaller. I don't trust them: if the tool can't show me exactly what changed — and put it back, byte for byte — I'm not pasting its output anywhere near production data.
So I built the boring version: jsonfold. It folds long JSON keys into short aliases before the prompt, and unfolds them back afterwards. Lossless, reversible, and every run prints a receipt you can verify.
Three commands, zero dependencies, plain Node:
# fold: long keys -> $0, $1,... (receipt goes to stderr, JSON to stdout) cat api.json | node bin/jsonfold.js fold > folded.json # 3,760 -> 2,940 bytes, saved 820 bytes (21.8%), ~205 est. tokens # 27 of 29 unique keys aliased (133 occurrences), round-trip OK # unfold: restore the original, byte-identical node bin/jsonfold.js unfold folded.json --map folded.json.map -o restored.json diff api.json restored.json && echo IDENTICAL # stats: see where the bytes actually go before you fold node bin/jsonfold.js stats api.json Enter fullscreen mode Exit fullscreen mode The aliasing is deliberate, not clever:
Token estimates use the rough ~4 chars/token heuristic and are labeled as such. I don't know your model's tokenizer, and the tool doesn't pretend to.
I made a 3,760-byte fixture shaped like a repo-list API response (invented data, 29 unique keys). Folding it:
3,760 -> 2,940 bytes, saved 820 bytes (21.8%), ~205 est. tokens 27 of 29 unique keys aliased (133 occurrences), round-trip OK Enter fullscreen mode Exit fullscreen mode 21.8% from key folding alone, on top of whatever jq -c already saved you. The folded JSON is still valid, readable-ish JSON — you can eyeball it, unlike dictionary-packed blobs.
Honest comparison, because "yet another JSON tool" deserves skepticism:
It won't drop fields, reorder anything, or summarize. That's jq 's job, or a conversation with the model. This tool does one thing: make the keys cheaper.
git clone https://github.com/techdevfun/jsonfold cd jsonfold node bin/jsonfold.js stats your-file.json Enter fullscreen mode Exit fullscreen mode MIT licensed, no dependencies, works on Node 18+. The repo has 42 tests, CI on three Node versions, and a demo script whose output is diffed in CI.
Roadmap, if people find it useful: an npx release, a --inline mode that embeds the map for single-file handoffs, and per-key savings in stats. Issues and "this broke on my payload" reports welcome — especially weird-key edge cases.
Repo: github.com/techdevfun/jsonfold — MIT, zero dependencies.