Skip to main content

A tool to convert complex, nested JSON into a token-efficient, flat TOON format.

Project description

NJFT: Nested JSON to TOON Converter

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.

🚀 Why NJFT? The Token Efficiency Advantage

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.

Features

  • 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.

Requirements

  • Python 3.6+
  • No external libraries are needed.

Installation

You can install the package directly from GitHub using pip:

pip install njft

Usage

As a Command-Line Tool

Once 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.toon

Arguments

  • input_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.

As a Python Library

You can import and use the encode function in your own Python code.

Simple Example

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)

Detailed Example with Complex JSON

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)

Example Conversion

Given the following input.json:

{
  "project": "TOON Converter",
  "milestones": [
    { "id": 1, "name": "Design", "status": "Completed" },
    { "id": 2, "name": "Implement", "status": "In Progress" }
  ]
}

Running python3 toonFlat.py input.json will produce:

project: TOON Converter
milestones{id,name,status}:
1,'Design','Completed'
2,'Implement','In Progress'

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

njft-0.1.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

njft-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file njft-0.1.1.tar.gz.

File metadata

  • Download URL: njft-0.1.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for njft-0.1.1.tar.gz
Algorithm Hash digest
SHA256 83004ae0640444b191b4bb8d82d257ba1f7c9b88220c090c1dd79bae587a17e5
MD5 8ca6291d76cbdf7f2772c4163f0a1ce1
BLAKE2b-256 f9ed9cff88c8463b46dc1b7fbe0ee6af754d67630f7b60b04f29287f2829e210

See more details on using hashes here.

File details

Details for the file njft-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: njft-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for njft-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0735b1ced0cc21ed566ad21416d9f51d18992d8274ce7e85e0fda7badea8e5e3
MD5 fae9429f899e2af2a70b5acd43f06393
BLAKE2b-256 9534e046a8d8a241f86c039a94cadc6a6e732cac0e04a2a0f3a525deb60b0990

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