json-smart-repair
v1.0.5
Published
A smart tool to repair malformed JSON strings, making them valid and parseable.
Maintainers
Readme
🔧 Smart JSON Repair
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-repairLocal Installation (Library)
npm install json-smart-repair
🚀 Usage
Command Line Interface (CLI)
From File
json-repair input.json > output.jsonFrom stdin
echo '{ id: 1, name: "John" age: 30 }' | json-repairUsing pipe
cat broken.json | json-repair > fixed.jsonProgrammatic 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:
- Preprocessing: Fixes quote issues and detects unclosed strings
- Tokenization: Breaks input into tokens (strings, numbers, brackets, etc.)
- Parsing: Builds JSON structure with error recovery
- 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - 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:
- Open an issue on GitHub
- Check the documentation
🔮 Roadmap
- [ ] Add more test cases
- [ ] Support for JSON5 features
- [ ] Web-based playground
- [ ] VS Code extension
- [ ] Performance optimizations
Made with ❤️ by Mohamed Rashad
