The encoder writes Terset 1.2 by default. The decoder still reads formats 1.0
and 1.1. To write an older format, pass format="1.1" or format="1.0"
to encode.
JSON repeats every key in every record, and a model reads each repeat as input tokens. Terset writes a list of records as one table instead. The keys go in a header once, and each record becomes a row. decode turns the text back into the same JSON.
- Fewer tokens. Over the 11 benchmark cases, Terset 1.2 used 26.5% fewer tokens than compact JSON and 6.2% fewer than TOON's best form, counted with the
o200k_base tokenizer. The saved Claude Opus 5.5 counts for Terset 1.1 show 31.4% and 3.8% fewer tokens. See the benchmark.
- Lossless.
decode gives back the same JSON, key order included. The benchmark decodes every Terset output and checks it against its input before counting it.
- Uneven records keep their table. A record can skip a field or hold a list and still be one row.
- Same text from JavaScript and Python. Both packages pass the same format tests.
- Zero dependencies. Neither package installs anything else.
Contents
- See it
- Install
- Use it
- How it compares
- How it works
- Benchmark
- When not to use it
- FAQ
- Reference
- For agents
- Source
- License
See it
Try it with your JSON
The same data, as compact JSON and as Terset.
{"team":"core","users":[{"id":"u1","name":"Ada","role":"admin","active":true,"logins":41},{"id":"u2","name":"Ben","role":"ops","active":true,"logins":7},{"id":"u3","name":"Chen","role":"ops","active":false,"logins":0},{"id":"u4","name":"Dara","active":true,"logins":12,"note":"on leave"}]}
team: core
users[4]:
id name name=value
u1 Ada role=admin active=true logins=41
u2 Ben role=ops active=true logins=7
u3 Chen role=ops active=false logins=0
u4 Dara active=true logins=12 note=on leave
The gaps between cells are real tab characters.
The fitted rules line for this text:
Tables use tab-separated headers and rows. After the header columns, a row lists its other fields as name=value. A field a row does not list is absent. A leading bare id header marks string IDs.
Send the rules line once per prompt, right before the data. The data goes in the user message, before the question. The benchmark page shows the full prompt.
Counts use the o200k_base tokenizer. The rules line and the data are counted together with a newline between them.
| Format |
Bytes |
Tokens |
| Terset data alone |
199 |
70 |
| Terset data plus rules |
394 |
112 |
| Compact JSON |
289 |
99 |
Four records do not save enough tokens to pay for the rules line.
Repeat the same record shapes to make 10 users, each with a unique ID. With the rules included, Terset uses 35 fewer tokens.
| Format |
Bytes |
Tokens |
| Terset data alone |
438 |
156 |
| Terset data plus rules |
633 |
198 |
| Compact JSON |
683 |
233 |
decode turns the Terset text back into the same object, key order included.
Install
pip install terset
Zero dependencies. Python 3.10 and up. Type hints are included.
Use it
The API exports encode, decode, RULES and rules_for.
rules_for(text) gives only the rules sentences that text needs. RULES keeps the full rules.
Save this as example.py.
from terset import JsonRoot, decode, encode, rules_for
data: JsonRoot = {
"team": "core",
"users": [
{"id": "u1", "name": "Ada", "role": "admin"},
{"id": "u2", "name": "Ben", "role": "ops"},
{"id": "u3", "name": "Chen"},
],
}
text = encode(data)
# One text: send its fitted rules right before the data.
messages = [
{"role": "system", "content": "You read Terset data."},
{"role": "user", "content": f"Which users have no role?\n\n{rules_for(text)}\n{text}"},
]
print(text)
print(decode(text) == data) # True
python3 example.py
Output (the gaps are tabs):
team: core
users[3]:
id name name=value
u1 Ada role=admin
u2 Ben role=ops
u3 Chen
True
encode(value) turns a JSON object or array into Terset text.
- Send
rules_for(text) right before the data. A shared or cached system prompt for many texts takes RULES instead.
decode(text) gives back the same value. Ask the model to answer in JSON, since Terset is for reading.
Never write Terset by hand. Always use encode.
Python details
decode and rules_for raise ValueError on any text they reject, and TypeError when the input is not a string.
- Numbers follow JavaScript, so
1.0 writes as 1. Decoded whole numbers within [-2**53, 2**53] become int. Others become float.
- An
int that a binary64 float cannot hold exactly, such as 9007199254740993, raises TypeError instead of rounding.
- Numeric
-0, nan, infinities, cycles and types other than dict, list, str, numbers, bool and None raise TypeError.
- Dictionary order is kept, with array-index keys such as
"2" and "10" first, in numeric order, as in JavaScript.
encode takes the read-only JsonInputRoot type. decode returns JsonRoot. The package ships py.typed.
How it compares
- Compact JSON is the baseline here. Terset is smaller in 10 of 11 cases on both counters.
- TOON is another table format for prompts. Terset is smaller in 9 of 11 cases on o200k_base and 10 on Claude. TOON's best form is smaller in 2 on o200k_base and 1 on Claude, all listed in the benchmark below. When records skip a field or hold a list, TOON drops its table and writes one line per field. Terset keeps the table.
- CSV needs no rules line, so it is smaller than Terset in 3 of 11 cases on o200k_base and 5 on Claude. It has no types and cannot tell null, a missing field and empty text apart, so its text does not always read back to the same JSON. Nested values fit only as JSON inside a cell.
- YAML and pretty JSON are easy for people to read and cost the most in total here. Over all 11 cases on o200k_base, Terset is 39.0% smaller than YAML and 50.3% smaller than pretty JSON.
How it works
- Lists become counted tables. A list of two or more objects becomes a tab-separated header and one row per record. A named list starts with
name[N]:. A root table starts with [N] on its own line.
- Table size. Compare a list table's header and rows with compact JSON in UTF-8 bytes, then add the count. A tie keeps JSON.
- Maps can become keyed tables. An object entry can use
name{N}:. The first column, @key, holds each row's key. The complete named block must be shorter than the entry with compact JSON.
- Missing fields are absent. A field that only some rows have can appear as
name=value after the columns. In format 1.0, an empty cell means absent. Empty text is "". Null is null.
- Quotes keep types. Text that looks like a number, boolean or null is JSON-quoted, so
"42" stays text and 42 stays a number.
- The
id column. A bare id header first in a list table marks string ids, so 007 stays 007. An unflattened first id column in a list table that does not meet the string-id rules is written "id" and keeps typed cells. Uniform objects can expand into dotted columns. Keyed tables use ordinary typed cells for id.
- Dotted columns. When rows share the same ordered nested keys, those keys can become columns such as
address.city. A literal dotted field name is quoted. Nested values left inside a row cell use compact JSON.
- Path entries. A slash separates nested keys, as in
a/b: 1. Format 1.2 uses paths only when the whole document saves at least 256 UTF-8 bytes over format 1.1.
- Counts are checked. The decoder rejects missing or extra rows. It reads the canonical text for formats 1.0, 1.1 and 1.2.
- Where tables go. Tables can appear at the root, under a field or under a path such as
a/b[3]:. The encoder keeps key and column order.
Full rules: terset.dev/format.
Benchmark
Total tokens over all 11 cases. Lower is better. Terset counts include its fitted rules line. TOON best is the smaller of TOON's comma and tab forms.
| Format |
o200k_base |
Claude Opus 5.5 |
| Terset |
43,228 |
59,047 |
| TOON best |
46,068 |
61,360 |
| Compact JSON |
58,808 |
86,102 |
| YAML |
70,846 |
88,145 |
| Pretty JSON |
87,020 |
113,574 |
- o200k_base: TOON's best is smaller than Terset on 2 cases: Translation batch; Reviews, long text. The 2 file-list cases hold 57% of Terset's total. Per case, the median saving is 21.5% against compact JSON and 7.3% against TOON's best.
- Claude: TOON's best is smaller than Terset on 1 case: Deep nested config. The 2 file-list cases hold 56% of Terset's total. Per case, the median saving is 27.9% against compact JSON and 3.5% against TOON's best.
The o200k_base tokenizer counts Terset 1.2. The saved Claude Opus 5.5 token counts use Terset 1.1. Open the section below for every case, the input cost and the method.
o200k_base
| Case |
Terset |
Compact JSON |
YAML |
TOON best |
Terset vs compact JSON |
| UI text, uneven rows |
547 |
697 |
812 |
814 |
-21.5% |
| UI text, uniform rows |
480 |
737 |
910 |
518 |
-34.9% |
| Translation batch |
1,139 |
1,448 |
1,674 |
1,110 |
-21.3% |
| File list, short |
9,608 |
12,808 |
15,304 |
10,315 |
-25.0% |
| File list, long |
15,042 |
19,969 |
23,966 |
15,502 |
-24.7% |
| 200 users |
5,404 |
9,051 |
10,924 |
5,589 |
-40.3% |
| Metrics, numbers and booleans |
2,940 |
4,798 |
5,999 |
3,181 |
-38.7% |
| Deep nested config |
250 |
251 |
316 |
296 |
-0.4% |
| Mixed-type array |
63 |
63 |
89 |
99 |
0.0% |
| Reviews, long text |
3,700 |
4,308 |
5,006 |
3,662 |
-14.1% |
| Orders with line items |
4,055 |
4,678 |
5,846 |
4,982 |
-13.3% |
| Total |
43,228 |
58,808 |
70,846 |
46,068 |
-26.5% |
Terset, fitted rules line included, is under TOON's best in 9 of 11
cases and under compact JSON in 10 of 11.
Over all 11 cases, Terset totals 43,228 tokens: 26.5% fewer than compact
JSON (58,808), 50.3% fewer than pretty JSON (87,020), 39.0% fewer than
YAML (70,846), and 6.2% fewer than TOON's best (46,068).
Claude Opus 5.5
| Case |
Terset |
Compact JSON |
YAML |
TOON best |
Terset vs compact JSON |
| UI text, uneven rows |
831 |
1,083 |
1,062 |
1,065 |
-23.3% |
| UI text, uniform rows |
745 |
1,253 |
1,160 |
774 |
-40.5% |
| Translation batch |
1,813 |
2,514 |
2,413 |
1,833 |
-27.9% |
| File list, short |
11,972 |
18,018 |
18,012 |
12,671 |
-33.6% |
| File list, long |
21,259 |
30,405 |
31,199 |
22,019 |
-30.1% |
| 200 users |
7,665 |
12,819 |
13,214 |
7,836 |
-40.2% |
| Metrics, numbers and booleans |
3,310 |
6,535 |
6,960 |
3,467 |
-49.3% |
| Deep nested config |
402 |
405 |
412 |
394 |
-0.7% |
| Mixed-type array |
89 |
89 |
101 |
118 |
0.0% |
| Reviews, long text |
4,899 |
5,979 |
6,404 |
4,915 |
-18.1% |
| Orders with line items |
6,062 |
7,002 |
7,208 |
6,268 |
-13.4% |
| Total |
59,047 |
86,102 |
88,145 |
61,360 |
-31.4% |
Terset, fitted rules line included, is under TOON's best in 10 of 11
cases and under compact JSON in 10 of 11.
Over all 11 cases, Terset totals 59,047 tokens: 31.4% fewer than compact
JSON (86,102), 48.0% fewer than pretty JSON (113,574), 33.0% fewer than
YAML (88,145), and 3.8% fewer than TOON's best (61,360).
Input cost at list price
Input cost for 1,000 requests of each case, in US dollars. Input tokens
only, at the list price on the day it was read. It is not a bill.
Claude Opus 5.5 at $4 per million input tokens, on the Claude counts:
| Case |
Terset |
Compact JSON |
Terset vs compact JSON |
| UI text, uneven rows |
$3.32 |
$4.33 |
$1.01 less |
| UI text, uniform rows |
$2.98 |
$5.01 |
$2.03 less |
| Translation batch |
$7.25 |
$10.06 |
$2.81 less |
| File list, short |
$47.89 |
$72.07 |
$24.18 less |
| File list, long |
$85.04 |
$121.62 |
$36.58 less |
| 200 users |
$30.66 |
$51.28 |
$20.62 less |
| Metrics, numbers and booleans |
$13.24 |
$26.14 |
$12.90 less |
| Deep nested config |
$1.61 |
$1.62 |
$0.01 less |
| Mixed-type array |
$0.36 |
$0.36 |
the same |
| Reviews, long text |
$19.60 |
$23.92 |
$4.32 less |
| Orders with line items |
$24.25 |
$28.01 |
$3.76 less |
| Total |
$236.19 |
$344.41 |
$108.22 less |
GPT 6.1 Sol at $2 per million input tokens, context up to 272K, on the
o200k_base counts:
| Case |
Terset |
Compact JSON |
Terset vs compact JSON |
| UI text, uneven rows |
$1.09 |
$1.39 |
$0.30 less |
| UI text, uniform rows |
$0.96 |
$1.47 |
$0.51 less |
| Translation batch |
$2.28 |
$2.90 |
$0.62 less |
| File list, short |
$19.22 |
$25.62 |
$6.40 less |
| File list, long |
$30.08 |
$39.94 |
$9.86 less |
| 200 users |
$10.81 |
$18.10 |
$7.29 less |
| Metrics, numbers and booleans |
$5.88 |
$9.60 |
$3.72 less |
| Deep nested config |
$0.50 |
$0.50 |
the same |
| Mixed-type array |
$0.13 |
$0.13 |
the same |
| Reviews, long text |
$7.40 |
$8.62 |
$1.22 less |
| Orders with line items |
$8.11 |
$9.36 |
$1.25 less |
| Total |
$86.46 |
$117.62 |
$31.16 less |
How it was measured: