A Python package and command-line tool that converts complex JSON into a highly token-efficient, flat format called "TOON" (Tab-Object-Oriented Notation).
Achieve a 30% to 60% reduction in tokens when sending structured data to Large Language Models (LLMs), leading to lower API costs and faster responses.
Working with LLMs costs money, and every token counts. Standard JSON is verbose, filled with brackets, braces, and quotes that consume valuable tokens without adding much semantic value. NJFT was built to solve this problem.
By intelligently flattening the data structure and converting arrays of objects into a compact tabular format, NJFT dramatically reduces the character count of your data.
- 💰 Lower API Costs: Fewer tokens mean smaller bills from services like OpenAI, Anthropic, and Google.
- ⚡ Faster Performance: Less data needs to be sent and processed, resulting in quicker API responses.
- 📦 Fit More Data: Squeeze larger and more complex data structures into the LLM's context window.
- Drastic Token Reduction: Achieves 30-60% character reduction compared to standard JSON.
- Flattens nested JSON objects.
- Converts arrays of objects into a special
key[count]{headers}:format. - Converts arrays of primitive values into indexed keys (e.g.,
key_0,key_1). - Provides proxy metrics (character and word counts) to estimate token reduction.
- Command-line interface to read from files and write to files or standard output.
- Python 3.6+
- No external libraries are needed.
You can install the package directly from GitHub using pip:
pip install njftOnce installed, the njft command will be available in your terminal.
# 1. Basic Conversion (prints to console)
njft input.json
# 2. Save to an Output File
njft input.json -o output.toon
# 3. Display Conversion Metrics and Comparison
njft input.json --metrics
# 4. Use with Pipes (stdin and stdout)
cat input.json | njft - > output.tooninput_file: Path to the input JSON file. Use-to read from standard input.-o, --output: (Optional) Path to the output TOON file. If omitted, the output is printed to standard output.-m, --metrics: (Optional) If specified, the tool will display a detailed comparison of the input JSON and output TOON, including character/word counts and compression percentage.
You can import and use the encode function in your own Python code.
import njft
# You can pass a file path
toon_from_file = njft.encode("path/to/your/input.json")
print(toon_from_file)
# Or you can pass a raw JSON string
json_string = '{"user": {"name": "John", "roles": ["admin", "editor"]}}'
toon_from_string = njft.encode(json_string)
print(toon_from_string)Here is a more realistic example showing how njft simplifies a complex nested object. You can run this code by saving it as example.py.
import njft
import json
# 1. Define a complex, nested JSON object
complex_data = {
"project_id": "P-487A",
"manager": {
"user_id": 101,
"full_name": "Elias Vance"
},
"milestones": [
{"name": "Phase 1", "status": "Completed", "tasks": [{"id": 1}, {"id": 2}]},
{"name": "Phase 2", "status": "In Progress", "tasks": [{"id": 3}]}
]
}
# 2. Convert to a JSON string
input_json_string = json.dumps(complex_data)
# 3. Use njft.encode() to get the token-efficient TOON format
toon_output = njft.encode(input_json_string)
print(toon_output)Given the following input.json:
{
"project": "TOON Converter",
"milestones": [
{ "id": 1, "name": "Design", "status": "Completed" },
{ "id": 2, "name": "Implement", "status": "In Progress" }
]
}Running njft input.json will produce:
project: TOON Converter
milestones{id,name,status}:
1,'Design','Completed'
2,'Implement','In Progress'