json-whisperer
v1.0.0
Published
Zero-dependency utility to clean, fix, and aggressively parse dirty JSON strings returned by AI/LLMs.
Maintainers
Readme
🤫 json-whisperer
Stop dealing with broken LLM JSON outputs. json-whisperer is a zero-dependency TypeScript library that magically cleans, fixes, and parses dirty JSON strings generated by AI models (GPT-4, Claude, Ollama).
The Problem
LLMs rarely return perfect JSON.parse() ready strings. They add markdown, talk before the JSON, leave trailing commas, or get cut off due to max_tokens limits.
// A typical AI response:
const aiResponse = `Here is your data:
\`\`\`json
{
"users": [
{ "name": "John", "role": "admin", } // <-- Trailing comma!
]
}
\`\`\`
Have a great day!`;
JSON.parse(aiResponse); // 💥 SyntaxError: Unexpected token 'H'The Solution
Just whisper to it.
import { whisperJson } from 'json-whisperer';
const data = whisperJson(aiResponse);
console.log(data.users.name); // "John" ✅ It just works.Features
- 🧹 Strips Markdown & Chit-chat (Ignores "Here is your JSON: ```json ...")
- 🧲 Trailing Commas Fix (Automatically removes them before parsing)
- 🚑 Aggressive Recovery (Tries to auto-close cut-off JSONs from token limits)
- 🛡️ Type-Safe (Full TypeScript support with generics)
- 🪶 Zero Dependencies (Tiny bundle size, works in browser, Node, and Edge/Cloudflare)
Usage
Basic Parsing with Generics
interface User { name: string; age: number; }
const user = whisperJson<User>(dirtyString);Streaming / Cut-off JSON Recovery
If your LLM hit max_tokens and output looks like this: {"status": "ok", "data": [{"id": 123
const result = whisperJson(cutOffString, { aggressive: true });
// whisperJson will automatically close the string, array, and object!
// Result: { status: "ok", data: [{ id: 123 }] }Fallback (Don't throw errors)
// If completely broken, return a default object instead of crashing your app
const result = whisperJson(garbage, { fallback: { error: true } });