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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tpmjs/tools-json-repair

v0.2.0

Published

Attempt to repair malformed JSON using jsonrepair

Readme

@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