How to count JSON tokens
- Paste or open your JSON. It can be an API response, a tool result, a few-shot example or a whole dataset. If it isn't strictly valid (trailing commas, single quotes, comments, a truncated end), the counter repairs it with the NeatJSON parser and tells you what it fixed. If it can't be parsed at all, the raw text is still counted.
- Read the table. You get characters, UTF-8 bytes, lines and estimated tokens for four tokenizer families, both as pasted and minified. The Savings column shows what minifying alone would save.
- Pick a context window. Choose 8K, 32K, 128K, 200K or 1M, or type any size. Check your model's documentation for its actual limit. The bars show how much of the window the JSON takes up, before and after minifying.
- Try the savings ideas. Each card shows the token estimate for a cheaper version of the same data. Apply replaces the input so you can keep stacking changes, Undo steps back, and Copy puts the result on your clipboard.
Why JSON is expensive in tokens
LLM tokenizers are trained mostly on prose and code, so ordinary English comes out at roughly four characters per token. JSON is noisier. Here is how OpenAI's o200k_base tokenizer splits the start of a pretty-printed record:
[\n | ␣ | ␣{\n | ␣␣␣ | ␣" | id | ": | ␣ | 0 | ,\n | ␣␣␣ | ␣" | uuid | ": | ␣" | 651 | 327 | 0 | e | - | 269 | e | ...
- Quotes and punctuation. Every key costs its own token plus a share of
␣"and":. Even a short key like"id"is spread over three tokens:␣",idand":. - Repeated keys. In an array of 1,000 records, every key name is written 1,000 times, and the model pays for every copy.
- Whitespace. Each indented line adds an indentation token and a line-break token. With o200k the indent width hardly matters, since 2 and 4 spaces both collapse into one token. Tabs cost more because each tab tends to become its own token.
- IDs, hashes and numbers. Digits are split into groups of at most three, and hex or base64 strings break into many short pieces. A UUID costs 20 to 25 tokens. A long float such as
0.20176824876850008costs about 8. - Non-English text. Accented, Cyrillic and CJK text uses more tokens per character, and older vocabularies such as cl100k are noticeably worse at it than o200k. Escaped text (
名) is far worse again, so send UTF-8 rather than\uescapes.
Token savings techniques, measured
We measured each technique on the same 20-record customer array: ids, UUIDs, names, emails, timestamps, a nested address, tags and some null notes. The figures are real o200k_base counts from tiktoken, with this page's estimate alongside.
| Format | Characters | o200k tokens (tiktoken) | This page's estimate | Saving vs pretty |
|---|---|---|---|---|
| Pretty-printed, 2-space indent | 8,851 | 3,124 | 3,142 | – |
| Pretty-printed, 4-space indent | 10,611 | 3,124 | 3,142 | 0% |
| Pretty-printed, tab indent | 7,971 | 3,423 | 3,342 | 10% more |
| Minified | 6,430 | 2,133 | 2,208 | 32% |
| Minified, null fields dropped | 6,326 | 2,101 | 2,168 | 33% |
| Minified, repeated keys aliased (with legend) | 5,519 | 2,149 | 2,226 | 31% |
Columnar JSON: {"columns":[…],"rows":[[…]]} | 4,836 | 1,743 | 1,762 | 44% |
| CSV (nested address flattened) | 4,129 | 1,644 | 1,566 | 47% |
What the numbers say:
- Minify first. It is lossless, every model reads it fine, and it saved about a third of the tokens here. Changing the indent width is not a substitute: 2-space and 4-space files counted the same.
- A table layout saves the most for arrays of records. Writing the keys once as
"columns"and each record as a positional array removes all the repeated key names. CSV goes further but loses types and nesting, so use it for flat data. The JSON to CSV converter gives you more control over the output. - Shortening keys helps less than you'd expect. Common English keys like
createdAtare already one or two tokens, so a one-letter alias plus its legend barely pays for itself. It does help with long or unusual key names such ascustomer_lifetime_value_usd. Single-letter keys also make it harder for the model to understand the data. - Dropping nulls is a small, safe win when your consumer treats a missing key and a null value the same way.
Exact token counts in code
The estimates here are for quick checks while you shape a prompt. To get exact counts for billing or hard limits, use each provider's own tokenizer or API.
OpenAI models: tiktoken (Python)
import tiktoken # pip install tiktoken
text = open("payload.json", encoding="utf-8").read()
enc = tiktoken.get_encoding("o200k_base") # GPT-4o-era models; "cl100k_base" for GPT-4 / GPT-3.5
print(len(enc.encode(text)))
# or let tiktoken pick the encoding for a model name it knows:
# enc = tiktoken.encoding_for_model("gpt-4o")
Chat requests add a few tokens per message for role and formatting, so a full request costs a little more than the raw JSON.
Anthropic Claude: the token counting endpoint
import anthropic # pip install anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
text = open("payload.json", encoding="utf-8").read()
resp = client.messages.count_tokens(
model="claude-opus-5-5", # use the model you will actually call
messages=[{"role": "user", "content": text}],
)
print(resp.input_tokens)
The endpoint is POST /v1/messages/count_tokens, and it accepts the same system prompt, tools and messages as a real request. Anthropic's tokenizer is not public, and counts vary between Claude model generations. Don't use tiktoken for Claude, because it undercounts.
Google Gemini: countTokens
from google import genai # pip install google-genai
client = genai.Client() # reads GEMINI_API_KEY
text = open("payload.json", encoding="utf-8").read()
resp = client.models.count_tokens(model="YOUR_GEMINI_MODEL", contents=text)
print(resp.total_tokens)
Context windows and output room
A context window is the total number of tokens a model can handle in one request. That covers the system prompt, tool definitions, conversation history, your JSON and the model's reply. If the JSON alone fills 90% of the window, there is little room left for the answer. Model limits change often and vary by tier and provider, so this page doesn't hard-code them. Pick the nearest common size or type the exact figure from your provider's documentation.
If the JSON is too big, cutting tokens isn't the only fix. You can send only the fields the task needs, for example with a JSONPath filter in the JSON viewer. You can split a large array into batches. You can describe the shape with a schema or TypeScript types instead of pasting thousands of sample rows. And when a model sends JSON back with a stray comma or a cut-off ending, the JSON validator pinpoints the error and the main formatter can repair it.
Frequently asked questions
How accurate is this JSON token counter?
It is an estimate. The page splits text the way GPT-style tokenizers do and then estimates the cost of each piece, without downloading the real vocabularies. Tested against OpenAI's o200k_base tokenizer on JSON, English and mixed samples, it is usually within about 10%, and closer on typical API JSON. Random strings such as base64 and hashes are the least accurate. For exact numbers use tiktoken, Anthropic's token counting endpoint or Gemini's countTokens.
Why are the Claude and Gemini numbers different from GPT?
Every model family uses its own tokenizer, so the same JSON produces a different token count in each. Anthropic and Google do not publish theirs as downloadable vocabularies, so this page applies rough ratios to the OpenAI-style estimate: about 1.2 times for Claude and 1.05 times for Gemini. Treat those two columns as ballpark figures and check with the provider's counting API before relying on them.
Does minifying JSON reduce tokens?
Yes, usually by a quarter to a third for pretty-printed JSON. In our 20-record test, minifying cut o200k tokens from 3,124 to 2,133. Changing 4-space indentation to 2 spaces saved nothing, because a run of spaces is a single token. Tab indentation cost more than spaces. Minified JSON is still valid JSON, and models read it without trouble.
Is CSV or JSON cheaper to send to an LLM?
For flat arrays of records, CSV or a columnar JSON layout is clearly cheaper, because the key names are written once instead of in every record. In our test, CSV used about 47% fewer tokens than pretty JSON and 23% fewer than minified JSON. CSV loses types, nulls and nesting, so for nested data a columnar JSON layout or plain minified JSON is often the better trade-off.
Should I shorten JSON keys to save tokens?
Only when the keys are long or unusual. Common English keys like name, email or createdAt are already one or two tokens, so a one-letter alias plus a legend saves little and makes the data harder for the model to interpret. Long snake_case keys repeated thousands of times are worth shortening, and the Shorten repeated keys card shows whether it pays off for your data.
How do I count tokens exactly?
For OpenAI models, use the tiktoken library with the o200k_base or cl100k_base encoding. For Claude, call Anthropic's token counting endpoint (messages.count_tokens in the SDK) with the model you plan to use. For Gemini, call countTokens (models.count_tokens in the Google Gen AI SDK). The code samples on this page show each one.
Is my JSON sent to OpenAI, Anthropic or Google?
No. Counting, minifying, the savings ideas and the token view all run in JavaScript in your browser. The page makes no API calls with your data, so it is safe for private payloads and works offline once loaded.