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.0.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.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: njft-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 71ee930362ceb387f62db7ab161befcc292756d741041fd2d84ead2e74193f33
MD5 1dc8de1d9e626521f1f2f022404b7be6
BLAKE2b-256 27ea05697641383835d95591654ffdf903859731b6e20fa73c7bb5d8f1c4e05f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: njft-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa890452c7ce4bddbeaaaff7f649b4619eefba1ed5ba50ca1b0a46ccccf508a5
MD5 1087d5c42d3508ccde311628430ff205
BLAKE2b-256 c515bea48d5da933fab9509340771e840a5d67198cf98bf3c361a8293607bb2b

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