@yarmu/json-repair
v1.0.2
Published
TypeScript implementation of json-repair - Fix invalid JSON strings
Maintainers
Readme
JSON Repair - TypeScript Implementation
A complete TypeScript port of the popular json-repair Python library.
This library can fix invalid JSON strings, which is especially useful when dealing with LLM outputs that may have malformed JSON.
✅ 100% Feature Parity | ✅ Fully Type-Safe | ✅ Production Ready
Features
- Fix missing quotes, brackets, and commas
- Handle incomplete arrays and objects
- Auto-complete missing values
- Remove comments from JSON
- Support for streaming/incomplete JSON
- Strict mode for validation
Installation
npm install @yarmu/json-repairUsage
import { repairJson, loads } from '@yarmu/json-repair';
// Repair and return as string
const fixed = repairJson('{"name": "John", "age": 30');
console.log(fixed); // '{"name": "John", "age": 30}'
// Repair and parse to object
const obj = loads('{"name": "John", "age": 30');
console.log(obj); // { name: 'John', age: 30 }API
repairJson(jsonStr, options?)
Repairs a JSON string and returns the fixed JSON string or parsed object.
Parameters:
jsonStr(string): The JSON string to repairoptions(object, optional):returnObjects(boolean): If true, return parsed object instead of string. Default: falseskipJsonParse(boolean): Skip initial JSON.parse validation. Default: falselogging(boolean): Return repair log along with result. Default: falsestreamStable(boolean): Keep repairs stable for streaming JSON. Default: falsestrict(boolean): Raise errors instead of repairing certain issues. Default: false
Returns: string | JSONReturnType | [JSONReturnType, RepairLog[]]
loads(jsonStr, options?)
Works like JSON.parse() but repairs the JSON first.
Parameters:
jsonStr(string): The JSON string to parseoptions(object, optional):skipJsonParse(boolean): Skip initial JSON.parse validation. Default: falselogging(boolean): Return repair log along with result. Default: falsestreamStable(boolean): Keep repairs stable for streaming JSON. Default: falsestrict(boolean): Raise errors instead of repairing. Default: false
Returns: JSONReturnType | [JSONReturnType, RepairLog[]]
load(fd, options?)
Read and repair JSON from a file descriptor.
Parameters:
fd(number): File descriptoroptions(object, optional): Same asloads
Returns: JSONReturnType | [JSONReturnType, RepairLog[]]
fromFile(filename, options?)
Read and repair JSON from a file.
Parameters:
filename(string): Path to JSON fileoptions(object, optional): Same asloads
Returns: JSONReturnType | [JSONReturnType, RepairLog[]]
Examples
import { repairJson, loads } from '@yarmu/json-repair';
// Missing closing brace
repairJson('{"name": "John"'); // '{"name": "John"}'
// Missing quotes
repairJson('{name: "John"}'); // '{"name": "John"}'
// Trailing comma
repairJson('{"name": "John",}'); // '{"name": "John"}'
// With comments
repairJson('{"name": "John" /* comment */}'); // '{"name": "John"}'
// Direct parsing
const obj = loads('{"name": "John", "age": 30');
// Returns: { name: 'John', age: 30 }
// With logging
const [result, log] = repairJson('{"name": "John"', {
returnObjects: true,
logging: true
});
console.log(log); // Array of repair operations
// Strict mode (throws on structural issues)
try {
repairJson('{"key": "value", "key": "duplicate"}', { strict: true });
} catch (e) {
console.error('Duplicate keys found!');
}License
MIT
Credits
This is a TypeScript port of the Python json-repair library by Stefano Baccianella.
