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

ai-json-fixer

v0.2.0

Published

A simple JSON parser designed to handle malformed JSON from Large Language Models

Readme

ai-json-fixer

A simple JSON parser specifically designed to handle malformed JSON output from Large Language Models (LLMs) like GPT, Claude, and others.

Features

Core

  • Markdown Block Extraction: Extracts JSON from ```json code blocks and inline code
  • Trailing Content Removal: Removes explanatory text after valid JSON structures
  • Quote Fixing: Fixes unescaped quotes inside JSON strings
  • Missing Comma Detection: Adds missing commas between array elements and object properties
    • Single-line JSON support for compact LLM outputs like {"a": 1 "b": 2} and [1 2 3]
    • Multi-line JSON support for formatted outputs

Key Benefits

  • Zero Dependencies: Pure TypeScript/JavaScript implementation
  • Comprehensive Testing: 86+ tests covering real LLM output patterns
  • TypeScript Support: Full type definitions included
  • Configurable: Multiple parsing modes and options
  • Error Recovery: Graceful handling of malformed input

Installation

npm install ai-json-fixer

Quick Start

import { LLMJSONParser } from 'ai-json-fixer';

const parser = new LLMJSONParser();

// Parse JSON from LLM output with markdown
const llmOutput = `Here's the data you requested:

\`\`\`json
{
  "users": [
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25}
  ],
  "total": 2
}
\`\`\`

This JSON contains the user information.`;

const result = parser.parse(llmOutput);
console.log(result);
// Output: { users: [...], total: 2 }

Common Use Cases

Extract from Markdown

const input = `\`\`\`json\n{"key": "value"}\n\`\`\``;
const result = parser.parse(input); // { key: "value" }

Fix Quote Issues

const input = '{"message": "He said "hello" to me"}';
const result = parser.parse(input); // { message: 'He said "hello" to me' }

Remove Trailing Content

const input = '{"status": "ok"} The request was successful.';
const result = parser.parse(input); // { status: "ok" }

Handle Missing Commas

Multi-line JSON:

const input = `{
  "name": "John"
  "age": 30
}`;
const result = parser.parse(input); // { name: "John", age: 30 }

Single-line JSON:

// Objects
const input1 = '{"a": 1 "b": 2 "c": 3}';
const result1 = parser.parse(input1); // { a: 1, b: 2, c: 3 }

// Arrays
const input2 = '[1 2 3 4 5]';
const result2 = parser.parse(input2); // [1, 2, 3, 4, 5]

// Complex structures
const input3 = '[{"id": 1} {"id": 2}]';
const result3 = parser.parse(input3); // [{ id: 1 }, { id: 2 }]

API Reference

LLMJSONParser

parse<T>(input: string, options?: ParseOptions): T | null

Parse JSON input with automatic fixing. Returns parsed object or null if parsing fails.

tryParse<T>(input: string, options?: ParseOptions): ParseResult<T>

Parse with detailed results including applied fixes and confidence score.

Options

interface ParseOptions {
  mode?: 'strict' | 'standard' | 'aggressive';
  stripMarkdown?: boolean;
  trimTrailing?: boolean;
  fixQuotes?: boolean;
  addMissingCommas?: boolean;
  trackFixes?: boolean;
  throwOnError?: boolean;
  maxFixAttempts?: number;
}

Parse Result

interface ParseResult<T> {
  data: T | null;
  fixes?: Fix[];
  confidence?: number;
  warnings?: string[];
}

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Run examples
npx tsx examples/basic-usage.ts

License

MIT