Laravel TOON
The TOON format for Laravel. The same data as JSON, in far fewer tokens.
composer require mischasigtermans/laravel-toon
JSON is an expensive format to pay for per token. Every record repeats every key, and the model reads the braces and quotes as carefully as the data. TOON, Token-Oriented Object Notation, writes the field names once and the records as rows.
I built this one because nested Stagent bookings were eating my MCP server's context, and the TOON packages for Laravel at the time kept the nesting intact. That post has the numbers on real Stagent data, and Stagent uses it in production. The package follows the TOON spec and its official test suite, and toonformat.dev lists it as the Laravel implementation.
TOON beats JSON on uniform arrays: database rows, paginated API responses, MCP tool results. On deeply nested, non-uniform data the saving shrinks, and a single object can be as small in JSON. Toon::diff() tells you which case you're in.
It doesn't replace JSON in your API. Serve TOON to the clients that ask for it, like a model or a token-conscious consumer, and keep JSON for everyone else.
What it does
-
Tables instead of repeated keys
An array of uniform objects becomes a table: field names once in the header, one row per record. The keys are written once, so the saving over JSON grows with every row.
-
Nested objects stay in the table
Nested objects that share one shape become nested field groups, like {id,author{name,email}}, at any depth. Objects of uniform objects collapse into keyed tables. Arrays whose objects differ in shape fall back to list form.
-
Built to the spec
Implements the TOON specification and runs every fixture of its official test suite. Decoding gives back the original structure, with ints, floats, booleans and nulls intact.
-
Laravel-shaped
Toon::encode() and Toon::decode(), toon_encode() and toon_decode() helpers, a toToon macro on collections and a toToon method on Eloquent models.
-
Knobs for fewer tokens
Omit nulls, empty strings or false values, drop keys like timestamps, alias long key names, truncate strings and cap float precision. Toon::diff() measures the saving in characters before you commit.
Hi, I'm Mischa. I've been shipping products and building ventures for over a decade. First exit at 25, second at 30. Now Partner & CPO at Ryde Ventures, an AI venture studio in Amsterdam. Currently shipping Stagent, TAP and Steddle. I write about what I learn along the way. More about me.
You might also enjoy: How working out 5 times a week changed my life.