npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@toonparse/core

v1.0.0

Published

ToonParse - Convert JSON, YAML, CSV to TOON format and reduce LLM token costs by 60%

Readme

@toonparse/core

Reduce LLM token costs by 60% with TOON format

Convert JSON, YAML, CSV to TOON (Token-Oriented Object Notation) format and save up to 60% on LLM tokens. Optimized for ChatGPT, Claude, GPT-4, and other LLMs.

npm version License: MIT

Why ToonParse?

  • 💸 Save 30-60% on tokens compared to JSON
  • 🎯 73.9% LLM accuracy vs JSON's 69.7%
  • 🚀 Zero config - works out of the box
  • 📦 Lightweight - minimal dependencies
  • 🔄 Lossless - perfect round-trip conversion

Installation

npm install @toonparse/core
pnpm add @toonparse/core
yarn add @toonparse/core

Quick Start

import { toToon, fromToon } from '@toonparse/core';

// Convert to TOON
const data = {
  users: [
    { id: 1, name: 'Alice', role: 'admin' },
    { id: 2, name: 'Bob', role: 'user' }
  ]
};

const toon = toToon(data);
console.log(toon);
// Output:
// users[2]{id,name,role}:
//   1,Alice,admin
//   2,Bob,user

// Convert back to JSON
const original = fromToon(toon);
console.log(original);
// { users: [...] }

API Reference

toToon(data: unknown): string

Convert JavaScript object/array to TOON format string.

Parameters:

  • data - Any JSON-serializable data

Returns: TOON format string

Example:

const toon = toToon({ name: 'Alice', age: 30 });
// name: Alice
// age: 30

fromToon(toon: string): unknown

Parse TOON format string back to JavaScript object.

Parameters:

  • toon - TOON format string

Returns: Parsed JavaScript object

Example:

const data = fromToon('name: Alice\nage: 30');
// { name: 'Alice', age: 30 }

compareFormats(data: unknown): ComparisonResult

Compare token usage between JSON and TOON formats.

Returns:

{
  jsonTokens: number;      // Token count for JSON
  toonTokens: number;      // Token count for TOON
  savingsTokens: number;   // Tokens saved
  savingsPercent: number;  // Percentage saved
}

Example:

import { compareFormats } from '@toonparse/core';

const comparison = compareFormats(myData);
console.log(`Save ${comparison.savingsPercent}% tokens with TOON!`);

countTokens(text: string): number

Calculate approximate token count for any text (uses ~4 chars/token).

Example:

import { countTokens } from '@toonparse/core';

const tokens = countTokens('Hello world');
// ~3 tokens

checkToonEligibility(data: unknown): EligibilityResult

Check if your data is suitable for TOON format.

Returns:

{
  score: number;          // 0-100 suitability score
  reason: string;         // Explanation
  recommended: boolean;   // Should you use TOON?
}

Example:

import { checkToonEligibility } from '@toonparse/core';

const check = checkToonEligibility(myData);
if (check.recommended) {
  console.log(`Score: ${check.score}/100 - ${check.reason}`);
}

Use Cases

Optimizing ChatGPT Prompts

import { toToon } from '@toonparse/core';

const products = [
  { id: 'p1', name: 'Widget', price: 19.99 },
  { id: 'p2', name: 'Gadget', price: 29.99 },
  // ... 100 more products
];

// Before: ~5000 tokens (JSON)
const jsonPrompt = `Analyze these products:\n${JSON.stringify(products)}`;

// After: ~2000 tokens (TOON) - 60% savings!
const toonPrompt = `Analyze these products:\n${toToon(products)}`;

Cost Savings Calculator

import { compareFormats } from '@toonparse/core';

const comparison = compareFormats(largeDataset);
const monthlyCalls = 100000;
const costPerToken = 0.00003; // GPT-4 pricing

const jsonCost = comparison.jsonTokens * monthlyCalls * costPerToken;
const toonCost = comparison.toonTokens * monthlyCalls * costPerToken;
const savings = jsonCost - toonCost;

console.log(`Monthly savings: $${savings.toFixed(2)}`);

Smart Format Selection

import { checkToonEligibility, toToon } from '@toonparse/core';

function optimizeForLLM(data: unknown): string {
  const eligibility = checkToonEligibility(data);
  
  if (eligibility.recommended) {
    console.log(`Using TOON (${eligibility.score}/100): ${eligibility.reason}`);
    return toToon(data);
  } else {
    console.log(`Using JSON: ${eligibility.reason}`);
    return JSON.stringify(data);
  }
}

Performance Benchmarks

Tested across 4 LLMs on 209 questions:

| Format | Accuracy | Tokens | Efficiency Score | |--------|----------|--------|------------------| | TOON | 73.9% | 2,744 | 26.9 🏆 | | JSON Compact | 70.7% | 3,081 | 22.9 | | YAML | 69.0% | 3,719 | 18.6 | | JSON | 69.7% | 4,545 | 15.3 |

Result: TOON achieves 73.9% accuracy while using 39.6% fewer tokens than JSON.

See full benchmarks →

When to Use TOON

Use TOON for:

  • LLM prompts (ChatGPT, Claude, GPT-4)
  • Uniform arrays of objects (database results, API responses)
  • Cost-sensitive applications
  • When token counts matter

Keep JSON for:

  • Deeply nested structures
  • Non-uniform data
  • Standard web APIs
  • Configuration files

Learn more →

Free Online Tools

Don't want to code? Try our free converters:

TypeScript Support

Full TypeScript support included with type definitions:

import { toToon, fromToon, type ConversionResult } from '@toonparse/core';

// Full type safety
const toon: string = toToon(data);
const parsed: unknown = fromToon(toon);

Advanced Usage

Custom Token Counting

import { countTokens, toToon } from '@toonparse/core';

const data = { /* large dataset */ };
const json = JSON.stringify(data);
const toon = toToon(data);

console.log(`JSON: ${countTokens(json)} tokens`);
console.log(`TOON: ${countTokens(toon)} tokens`);

Integration with LangChain

import { toToon } from '@toonparse/core';
import { ChatOpenAI } from 'langchain/chat_models/openai';

const chat = new ChatOpenAI();
const data = [ /* your data */ ];

// Use TOON to reduce costs
const response = await chat.call([
  { role: 'user', content: `Analyze:\n${toToon(data)}` }
]);

Contributing

Contributions welcome! Please visit:

License

MIT © ToonParse Team

Links


Built by ToonParse - The #1 TOON format toolkit