@tpmjs/tools-json-repair
Attempt to repair malformed JSON strings. Fixes common issues like unquoted keys, trailing commas, single quotes, missing commas, unclosed brackets, and comments. Returns the repaired JSON, whether it was modified, and a list of changes made.
Test @tpmjs/tools-json-repair (jsonRepairTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-json-repairpnpm add @tpmjs/tools-json-repairyarn add @tpmjs/tools-json-repairbun add @tpmjs/tools-json-repairdeno add npm:@tpmjs/tools-json-repairimport { jsonRepairTool } from '@tpmjs/tools-json-repair';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { jsonRepairTool } from '@tpmjs/tools-json-repair';
const result = await generateText({
model: openai('gpt-4o'),
tools: { jsonRepairTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
jsonstringThe malformed JSON string to repair
Schema extracted: 1/1/2026, 8:17:43 AM
Attempt to repair malformed JSON using jsonrepair.
npm install @tpmjs/tools-json-repair # or pnpm add @tpmjs/tools-json-repair # or yarn add @tpmjs/tools-json-repair
import { jsonRepairTool } from '@tpmjs/tools-json-repair'; import { generateText } from 'ai'; const result = await generateText({ model: yourModel, tools: { jsonRepair: jsonRepairTool, }, prompt: 'Fix this JSON: {name: "Alice", age: 25,}', });
import { jsonRepairTool } from '@tpmjs/tools-json-repair'; const result = await jsonRepairTool.execute({ json: "{name: 'Alice', age: 25,}", }); console.log(result.repaired); // {"name":"Alice","age":25} console.log(result); // { // repaired: '{"name":"Alice","age":25}', // wasModified: true, // changes: [ // 'Converted single quotes to double quotes', // 'Added quotes to unquoted keys', // 'Removed trailing commas' // ], // metadata: { // repairedAt: '2025-01-15T12:00:00.000Z', // originalLength: 27, // repairedLength: 26, // isValidJson: true // } // }
| Parameter | Type | Required | Description |
|---|---|---|---|
json | string | Yes | The malformed JSON string to repair |
{ repaired: string; wasModified: boolean; changes: string[]; metadata: { repairedAt: string; originalLength: number; repairedLength: number; isValidJson: boolean; }; }
const result = await jsonRepairTool.execute({ json: '{name: "Alice", age: 25}', }); console.log(result.repaired); // {"name":"Alice","age":25} console.log(result.changes); // ['Added quotes to unquoted keys']
const result = await jsonRepairTool.execute({ json: "{'name': 'Alice', 'age': 25}", }); console.log(result.repaired); // {"name":"Alice","age":25} console.log(result.changes); // ['Converted single quotes to double quotes', 'Added quotes to unquoted keys']
const result = await jsonRepairTool.execute({ json: '{"items": [1, 2, 3,], "count": 3,}', }); console.log(result.repaired); // {"items":[1,2,3],"count":3} console.log(result.changes); // ['Removed trailing commas']
const result = await jsonRepairTool.execute({ json: '{"users": [{"name": "Alice"}, {"name": "Bob"}', }); console.log(result.repaired); // {"users":[{"name":"Alice"},{"name":"Bob"}]} console.log(result.changes); // ['Added missing closing bracket']
const result = await jsonRepairTool.execute({ json: `{ // User data "name": "Alice", "age": 25 /* current age */ }`, }); console.log(result.repaired); // {"name":"Alice","age":25} console.log(result.changes); // ['Removed comments']
const result = await jsonRepairTool.execute({ json: '{"name":"Alice","age":25}', }); console.log(result.wasModified); // false console.log(result.changes); // []
try { const result = await jsonRepairTool.execute({ json: 'This is not even close to JSON', }); } catch (error) { console.error(error.message); // "Failed to repair JSON: ... The input may be too malformed to fix." }
const result = await jsonRepairTool.execute({ json: "{name: 'Alice', age: 25}", }); if (result.metadata.isValidJson) { const parsed = JSON.parse(result.repaired); console.log(parsed.name); // "Alice" console.log(parsed.age); // 25 }
| Issue | Before | After |
|---|---|---|
| Unquoted keys | {name: "Alice"} | {"name":"Alice"} |
| Single quotes | {'name': 'Alice'} | {"name":"Alice"} |
| Trailing commas | {"age": 25,} | {"age":25} |
| Missing commas | {"a": 1 "b": 2} | {"a":1,"b":2} |
| Unclosed brackets | {"items": [1, 2} | {"items":[1,2]} |
| Comments | {/* comment */ "a": 1} | {"a":1} |
MIT
Downloads/month
39
Quality Score