Introduction
What is TOON and why does it exist?
TOON stands for Typed Object Oriented Notation. It is a lightweight, human-readable data format built as a practical alternative to JSON for API communication — particularly suited for structured, multi-resource responses.
JSON is powerful but carries heavy syntactic overhead: repeated keys, nested braces, and no native type schema. TOON solves this by separating the schema definition from the data rows, like a miniature typed table format — resulting in smaller payloads and faster parsing.
sendToon, receiveToon, reqGetToon, and resSendToon — covering both client and server needs.The TOON Format
Understanding TOON syntax, anatomy, and parsing rules
Anatomy of a TOON string
A TOON document is made up of one or more resource blocks. Each block has:
employees[2]{id:n,name:s}:1,RiyaFull Example — TOON vs JSON
meta{page:n,limit:n,total:n}: 1,10,200 employees[2]{id:n,name:s,salary:n,active:b}: 1,Riya,90000,true 2,John,80000,false departments[1]{id:s,title:s}: 10,Engineering
{
"meta": {
"page": 1,
"limit": 10,
"total": 200
},
"employees": [
{ "id": 1, "name": "Riya",
"salary": 90000, "active": true },
{ "id": 2, "name": "John",
"salary": 80000, "active": false }
],
"departments": {
"id": "10",
"title": "Engineering"
}
}[1], the parsed output is a plain object. When the count is [2] or higher, the parsed output is an array. When no count is specified (like meta{…}), it also parses as an object.Data Types
Every supported type code, what it maps to, and how to use it
All-types example
sample{age:n,name:s,active:b,data:j,tags:a,value:nl}: 25,Manoj,true,{"x":1},["a","b"],null
{
"sample": {
"age": 25,
"name": "Manoj",
"active": true,
"data": { "x": 1 },
"tags": ["a", "b"],
"value": null
}
}Installation
Getting toonkit into your project
npm install toonkityarn add toonkitpnpm add toonkitImporting
import { sendToon, receiveToon, reqGetToon, resSendToon } from "toonkit";
Exported Functions
Frontend Usage
Sending and receiving TOON data from the browser
Use sendToon() to convert your JavaScript object into a TOON string before sending it to the server. Always set Content-Type: text/plain.
import { sendToon } from "toonkit"; const payload = sendToon({ employees: [ { id: 1, name: "Riya", salary: 90000, active: true }, { id: 2, name: "John", salary: 80000, active: false } ] }); // payload is now a compact TOON string: // employees[2]{id:n,name:s,salary:n,active:b}: // 1,Riya,90000,true // 2,John,80000,false
await fetch("/api/employees", { method: "POST", headers: { "Content-Type": "text/plain" // ← Required! }, body: payload // TOON string });
Backend Usage (Express)
Parsing TOON requests and sending TOON responses in Node.js
Before toonkit can parse the request body, Express must be configured to read raw text bodies using express.text(). Without this middleware, req.body will be undefined.
const express = require("express"); const { reqGetToon, resSendToon } = require("toonkit"); const app = express(); // ← Critical: enables plain text body parsing app.use(express.text()); app.listen(3000, () => console.log("Server running"));
app.use(express.text()) before your routes. toonkit reads from req.body which is only populated when Express's text parser is active.Testing with Postman
Manually sending TOON requests to your API
Since TOON is plain text, you can easily test your API endpoints using Postman or any HTTP client. Follow these four steps:
Content-Type: text/plainemployees[2]{id:n,name:s,salary:n}: 1,Riya,90000 2,John,80000
Advanced Examples
Pagination, multi-collection responses, and complex schemas
Pagination Response
A common API pattern is returning paginated data with a meta block alongside the collection. Because meta has no count (no []), it parses as a plain object.
import { sendToon } from "toonkit"; sendToon({ meta: { page: 1, limit: 10, total: 200 }, employees: [ { id: 1, name: "Riya", salary: 90000, active: true }, { id: 2, name: "John", salary: 80000, active: false }, // ... up to limit rows ] });
Multiple Collections
One of TOON's strengths is encoding multiple collections in a single response. This is useful for dashboard endpoints that return users, products, and orders together.
sendToon({ users: [ { id: 1, name: "Alice", role: "admin" }, { id: 2, name: "Bob", role: "user" } ], products: [ { sku: "A1", title: "Keyboard", price: 79.99 }, { sku: "B2", title: "Mouse", price: 39.99 } ], orders: [ { id: 101, userId: 1, total: 119.98, fulfilled: true } ] });
Performance Advantage
Why TOON is faster and lighter than JSON
JSON repeats every key name for every row. In a dataset with 100 employees, the field names id, name, salary, active each appear 100 times. TOON declares them once in the schema header.
Nested braces & quotes
Heavier for tabular data
No inline type schema
Clean CSV-style rows
Smaller payload size
Types baked in
// JSON: ~120 bytes for 2 rows {"employees":[{"id":1,"name":"Riya","salary":90000,"active":true},{"id":2,"name":"John","salary":80000,"active":false}]} // TOON: ~70 bytes for 2 rows employees[2]{id:n,name:s,salary:n,active:b}: 1,Riya,90000,true 2,John,80000,false
When to Use toonkit
Ideal scenarios and environments for TOON