A simple JSON parser designed to handle malformed JSON from Large Language Models
npm install ai-json-fixerA simple JSON parser specifically designed to handle malformed JSON output from Large Language Models (LLMs) like GPT, Claude, and others.
- Markdown Block Extraction: Extracts JSON from ``json code blocks and inline code{"a": 1 "b": 2}
- 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 and [1 2 3]
- Multi-line JSON support for formatted outputs
- 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
`bash`
npm install ai-json-fixer
`typescript
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 }
`
`typescript\
const input = \\json\n{"key": "value"}\n\\\;`
const result = parser.parse(input); // { key: "value" }
`typescript`
const input = '{"message": "He said "hello" to me"}';
const result = parser.parse(input); // { message: 'He said "hello" to me' }
`typescript`
const input = '{"status": "ok"} The request was successful.';
const result = parser.parse(input); // { status: "ok" }
Multi-line JSON:
`typescript{
const input =
"name": "John"
"age": 30
};`
const result = parser.parse(input); // { name: "John", age: 30 }
Single-line JSON:
`typescript
// 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 }]
`
#### parse
Parse JSON input with automatic fixing. Returns parsed object or null if parsing fails.
#### tryParse
Parse with detailed results including applied fixes and confidence score.
`typescript`
interface ParseOptions {
mode?: 'strict' | 'standard' | 'aggressive';
stripMarkdown?: boolean;
trimTrailing?: boolean;
fixQuotes?: boolean;
addMissingCommas?: boolean;
trackFixes?: boolean;
throwOnError?: boolean;
maxFixAttempts?: number;
}
`typescript`
interface ParseResult
data: T | null;
fixes?: Fix[];
confidence?: number;
warnings?: string[];
}
`bashInstall dependencies
npm install
MIT