Urgent.News

What's breaking now, across thousands of outlets.

Tech

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" repeated forty times. jq -c helps — it strips the whitespace. But whitespace was never the real problem.…

For years, the author has been pasting large JSON objects—around 4KB—into every prompt. This repetitive process led to a frustrating loop: retrieving data, receiving a wall of JSON, pasting it into a prompt, and watching a third of the context window vanish. The primary issue was not the whitespace, which could be stripped using tools like jq -c, but the size of the object keys. In a typical API response, object keys made up 20–40% of the bytes, and many long names repeated across multiple objects in every array.

Frustrated by the lack of reliable compression tools, the author developed a solution called jsonfold. jsonfold folds long JSON keys into short aliases before the prompt and unfolds them back afterward. This process is lossless, reversible, and produces a verifiable receipt that can be checked against the original data. The tool provides three simple commands: fold, unfold, and stats, all written in plain Node.js with no additional dependencies.

When the author tested jsonfold on a 3,760-byte fixture resembling a repo-list API response with 29 unique keys, the results were impressive. Folding the JSON reduced its size by 21.8%, from 3,760 bytes to 2,940 bytes, resulting in an estimated 820 bytes saved (about 205 estimated tokens). The tool successfully aliased 27 out of 29 unique keys, preserving the original data byte-for-byte upon unfolding.

This key folding alone contributed to the overall size reduction, even with the use of whitespace-stripping tools like jq -c.

The author emphasizes that jsonfold does not alter the data in any way other than shortening the keys. It does not drop fields, reorder anything, or summarize the information. Instead, it focuses solely on making the keys cheaper, potentially offering significant savings in large payloads. The tool is MIT licensed, works on Node 18+, and has undergone extensive testing with 42 tests, CI on three Node versions, and a demo script that has been diffed in CI.

In the future, the author plans to release jsonfold via npm as npx, introduce a --inline mode for single-file handoffs, and provide per-key savings in the stats output. The source code is available on GitHub (github.com/techdevfun/jsonfold) under the MIT license, with open invitations for issues, edge case reports, and contributions.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Wednesday 21 October →