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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-smart-repair

v1.0.5

Published

A smart tool to repair malformed JSON strings, making them valid and parseable.

Readme

🔧 Smart JSON Repair

npm version License: MIT

Smart JSON Repair is an intelligent JSON repair tool that automatically fixes broken, malformed, or incomplete JSON with advanced heuristics. It handles a wide range of JSON syntax errors and recovers valid JSON from messy input.

✨ Features

  • 🔍 Automatic Quote Fixing: Handles missing, mismatched, or broken quotes
  • 📝 Unquoted Keys & Values: Converts unquoted identifiers to proper strings
  • 🔗 Missing Commas: Intelligently inserts missing commas between elements
  • 🧩 Incomplete Structures: Closes unclosed brackets and braces
  • 🎯 Type Inference: Converts boolean/null variants (TRUE, None, nil, etc.)
  • 🌐 Unicode Handling: Fixes broken unicode escape sequences
  • 💬 Comment Removal: Strips JavaScript-style comments (// and /* */)
  • 🔢 Number Parsing: Handles various number formats and edge cases
  • 📦 Nested Structures: Properly repairs deeply nested objects and arrays
  • 🚀 CLI & Programmatic API: Use as a command-line tool or import as a library

📦 Installation

Global Installation (CLI)

npm install -g json-smart-repair

Local Installation (Library)

npm install json-smart-repair

🚀 Usage

Command Line Interface (CLI)

From File

json-repair input.json > output.json

From stdin

echo '{ id: 1, name: "John" age: 30 }' | json-repair

Using pipe

cat broken.json | json-repair > fixed.json

Programmatic API

JavaScript/Node.js

const { repairJson } = require('json-smart-repair');

const brokenJson = `{
  id: 1,
  name: "John Doe",
  age: 30,
  active: TRUE,
  tags: ["developer" "nodejs"],
}`;

const fixedJson = repairJson(brokenJson);
console.log(fixedJson);

TypeScript

import { repairJson } from 'json-smart-repair';

const brokenJson = `{ id: 1, name: 'Alice', city: Cairo }`;
const fixedJson = repairJson(brokenJson);

// Parse the repaired JSON
const data = JSON.parse(fixedJson);
console.log(data);

🎯 Examples

Example 1: Missing Commas & Unquoted Keys

Input:

{ id: 1 name: "Alice" city: "Cairo" }

Output:

{
  "id": 1,
  "name": "Alice",
  "city": "Cairo"
}

Example 2: Broken Quotes

Input:

{ "name": "John "The King"", "age": 25 }

Output:

{
  "name": "John \"The King\"",
  "age": 25
}

Example 3: Arrays with Missing Commas

Input:

["apple" "banana" "orange"]

Output:

[
  "apple",
  "banana",
  "orange"
]

Example 4: Boolean & Null Variants

Input:

{
  "active": TRUE,
  "verified": Yes,
  "deleted": None,
  "archived": nullish
}

Output:

{
  "active": true,
  "verified": true,
  "deleted": null,
  "archived": null
}

Example 5: Trailing Commas

Input:

{
  "items": [1, 2, 3,,,],
  "name": "test",
}

Output:

{
  "items": [
    1,
    2,
    3
  ],
  "name": "test"
}

Example 6: Comments

Input:

{
  // This is a comment
  "id": 1,
  /* Multi-line
     comment */
  "name": "John"
}

Output:

{
  "id": 1,
  "name": "John"
}

Example 7: Complex Nested Structure

Input:

[
  { id: 1, name: "Alice", tags: ["dev" "senior"], active: Yes },
  { id: 2, name: Bob, city: "NYC" age: 30 },
  { id: 3 skills: { js: true python: TRUE } }
]

Output:

[
  {
    "id": 1,
    "name": "Alice",
    "tags": [
      "dev",
      "senior"
    ],
    "active": true
  },
  {
    "id": 2,
    "name": "Bob",
    "city": "NYC",
    "age": 30
  },
  {
    "id": 3,
    "skills": {
      "js": true,
      "python": true
    }
  }
]

🛠️ API Reference

repairJson(text: string): string

Repairs broken JSON and returns a valid JSON string.

Parameters:

  • text (string): The broken/malformed JSON string

Returns:

  • (string): Valid, formatted JSON string

Example:

import { repairJson } from 'json-smart-repair';

const fixed = repairJson('{ name: John, age: 30 }');
// Returns: '{\n  "name": "John",\n  "age": 30\n}'

🎨 Supported Repairs

| Issue | Example Input | Repaired Output | |-------|---------------|-----------------| | Missing quotes | {name: John} | {"name": "John"} | | Single quotes | {'name': 'John'} | {"name": "John"} | | Missing commas | {a: 1 b: 2} | {"a": 1, "b": 2} | | Trailing commas | [1, 2, 3,] | [1, 2, 3] | | Unclosed brackets | {a: [1, 2} | {"a": [1, 2]} | | Line breaks in strings | "hello\nworld" | "hello\nworld" | | Boolean variants | TRUE, Yes | true | | Null variants | None, nil | null | | Comments | // comment | (removed) | | Broken unicode | \uD83D | (removed) |

⚙️ How It Works

Smart JSON Repair uses a multi-stage approach:

  1. Preprocessing: Fixes quote issues and detects unclosed strings
  2. Tokenization: Breaks input into tokens (strings, numbers, brackets, etc.)
  3. Parsing: Builds JSON structure with error recovery
  4. Post-processing: Cleans up unicode issues and validates output

The parser uses intelligent heuristics to guess the developer's intent, such as:

  • Detecting when a quote should close based on context
  • Inferring missing commas from structural patterns
  • Recognizing boolean/null value variants
  • Handling mixed quoted and unquoted values

📋 Requirements

  • Node.js >= 14.0.0

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by the need to handle malformed JSON from various sources
  • Built with TypeScript for type safety and better developer experience

📞 Support

If you have any questions or need help, please:

🔮 Roadmap

  • [ ] Add more test cases
  • [ ] Support for JSON5 features
  • [ ] Web-based playground
  • [ ] VS Code extension
  • [ ] Performance optimizations

Made with ❤️ by Mohamed Rashad