Skip to main content

Minify JSON to reduce LLM API token costs

Project description

minson

minify + json — strip

Install

pip install minson

Usage

In Python

from minson import minify

# From a dict/list
minify({"name": "Alice", "age": 30})
# → '{"name":"Alice","age":30}'

# From a JSON string
minify('{ "name": "Alice",  "age": 30 }')
# → '{"name":"Alice","age":30}'

# From a file
minify("/path/to/data.json")
# → '{"name":"Alice","age":30}'

Check your savings

from minson.core import token_savings

stats = token_savings({"name": "Alice", "age": 30, "city": "London"})
# {
#   'original_chars': 48,
#   'minified_chars': 38,
#   'chars_saved': 10,
#   'estimated_pct_saved': 20.8,
#   'estimated_tokens_saved': 2
# }

Minify a file (and write output)

from minson.core import minify_file

minify_file("data.json", "data.min.json")

From the command line

# Print minified JSON
minson data.json

# Write to file
minson data.json --output data.min.json

# Show token savings stats
minson data.json --stats

# Pipe from stdin
echo '{"a": 1, "b": 2}' | minson -

Why?

LLM APIs charge per token. Pretty-printed or verbose JSON wastes tokens on whitespace that carries no meaning. Minifying before sending data to the API is one of the simplest, zero-effort ways to cut costs.

Format Relative token cost
Pretty JSON 100% (baseline)
Minified JSON ~69–80%
YAML ~82%
XML ~114%

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minson-0.1.0.tar.gz (2.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minson-0.1.0-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

File details

Details for the file minson-0.1.0.tar.gz.

File metadata

  • Download URL: minson-0.1.0.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for minson-0.1.0.tar.gz
Algorithm Hash digest
SHA256 95fd31dea103a0bd789f9825f076c21e40da7130400538b566368a06aa625260
MD5 4db737b86c659479dbb9c7ad34a13498
BLAKE2b-256 099222c0eb189eec7fd8e1b1e4f590d92f3260eaedacc5a1a6d959a56376fa25

See more details on using hashes here.

File details

Details for the file minson-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: minson-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for minson-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2906544cecab1b3e13e13fb13e66f40f8853fa271f5944ade0706f19ab90f504
MD5 433ce9aa20beaa43442d9e1f36f6108f
BLAKE2b-256 90b68686466ccea801dcf52431ececa6fc0f33479080dca4070e7edaf22ae48d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page