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.2.tar.gz (3.9 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.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minson-0.1.2.tar.gz
  • Upload date:
  • Size: 3.9 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.2.tar.gz
Algorithm Hash digest
SHA256 a00146c79ae7f8835f0ba306cfe4a39dbe4ad2dedb5ba60b3e87cd31da96a860
MD5 cd579843d5941c9232e80fa4e3ad88fb
BLAKE2b-256 4a08ff476b533237313e4a33699139f5e486d97b0da3e19932ae898f7ecceb1b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minson-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9c00ed775e97056c78f485a6f35a8783812b1571c57244be71c69f6ab28d74b0
MD5 8ef710f4f7e6c2516cb811fc79408bf6
BLAKE2b-256 08b4056f34ab834defeee5f848ac7d2cc761bb259eeee69be7b397605cbfb777

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