tpmjs/packages/tools/official/json-repair
Ajax Davis 5926373be6 chore: update ai package to 6.0.49 across all packages
- Updated all packages from ai@6.0.23 to ai@6.0.49
- Added pnpm override to ensure consistent version
- Created changeset for publishing affected packages
2026-01-25 11:33:03 +10:00
..
src feat: add 100+ official TPMJS tools 2025-12-31 22:55:56 +10:00
CHANGELOG.md chore: version packages 2025-12-31 23:54:47 +10:00
package.json chore: update ai package to 6.0.49 across all packages 2026-01-25 11:33:03 +10:00
README.md feat: add 100+ official TPMJS tools 2025-12-31 22:55:56 +10:00
tsconfig.json feat: add 100+ official TPMJS tools 2025-12-31 22:55:56 +10:00
tsup.config.ts feat: add 100+ official TPMJS tools 2025-12-31 22:55:56 +10:00

@tpmjs/tools-json-repair

Attempt to repair malformed JSON using jsonrepair.

Installation

npm install @tpmjs/tools-json-repair
# or
pnpm add @tpmjs/tools-json-repair
# or
yarn add @tpmjs/tools-json-repair

Usage

With Vercel AI SDK

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,}',
});

Direct Usage

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
//   }
// }

Features

  • Unquoted Keys - Adds quotes to object keys
  • Single Quotes - Converts single quotes to double quotes
  • Trailing Commas - Removes trailing commas
  • Missing Commas - Adds missing commas between elements
  • Unclosed Brackets - Adds missing closing brackets/braces
  • Comments - Removes single-line and multi-line comments
  • Escape Sequences - Fixes invalid escape sequences
  • Change Tracking - Reports what modifications were made

Parameters

Parameter Type Required Description
json string Yes The malformed JSON string to repair

Returns

{
  repaired: string;
  wasModified: boolean;
  changes: string[];
  metadata: {
    repairedAt: string;
    originalLength: number;
    repairedLength: number;
    isValidJson: boolean;
  };
}

Examples

Fix Unquoted Keys

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']

Fix Single Quotes

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']

Fix Trailing Commas

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']

Fix Missing Closing Brackets

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']

Fix Comments

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']

Already Valid JSON

const result = await jsonRepairTool.execute({
  json: '{"name":"Alice","age":25}',
});

console.log(result.wasModified);
// false

console.log(result.changes);
// []

Error Handling

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."
}

Using Repaired JSON

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
}

Common Issues Fixed

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}

Limitations

  • Cannot repair fundamentally invalid structures
  • May not preserve original formatting (whitespace, indentation)
  • Very corrupted JSON may still fail to repair
  • Does not validate semantic correctness of the JSON content

License

MIT