llm-json-fixer 1.0.1
A simple utility to fix malformed JSON from LLM outputs
Requires Python: >=3.7
Keywords: json, llm, fix, repair, malformed, ai, chatgpt, claude

# JSON Fixer

A simple, lightweight Python utility to fix malformed JSON strings commonly generated by Large Language Models (LLMs) like ChatGPT, Claude, and others.

## 🚀 Features

- **Zero Dependencies**: Uses only Python standard library
- **Simple API**: One main function to fix JSON
- **Handles Common Issues**: Fixes quotes, brackets, commas, Python booleans, and more
- **Extracts JSON**: Can extract JSON from surrounding text
- **Lightweight**: Less than 200 lines of code
- **Fast**: Optimized for performance

## 📦 Installation

```bash
pip install llm_json_fixer
```

## 🔧 Usage

### Basic Usage

```python
from llm_json_fixer import fix_json

# Fix single quotes
result = fix_json("{'name': 'John', 'age': 30}")
print(result)  # {'name': 'John', 'age': 30}

# Fix trailing commas
result = fix_json('{"name": "John", "age": 30,}')
print(result)  # {'name': 'John', 'age': 30}

# Fix Python booleans
result = fix_json('{"active": True, "data": None}')
print(result)  # {'active': True, 'data': None}

# Extract JSON from text
result = fix_json('Here is the JSON: {"name": "John"} and that\'s it.')
print(result)  # {'name': 'John'}
```

### Detailed Usage

```python
from llm_json_fixer import fix_json

# Get detailed information about fixes applied
result = fix_json('{"name": "John", "age": 30,}', return_dict=True)
print(result)
# {
#     'success': True,
#     'data': {'name': 'John', 'age': 30},
#     'error': None,
#     'fixes_applied': ['Applied common JSON fixes']
# }
```

### Validation

```python
from llm_json_fixer import is_valid_json

print(is_valid_json('{"name": "John"}'))  # True
print(is_valid_json("{'name': 'John'}"))  # False
```

## 🛠️ What It Fixes

- **Single quotes** → Double quotes
- **Trailing commas** → Removed
- **Missing commas** → Added
- **Unquoted keys** → Quoted
- **Python booleans** (`True`, `False`, `None`) → JSON equivalents
- **Unbalanced brackets** → Balanced
- **Markdown code blocks** → Removed
- **Multiple commas** → Single commas
- **Unclosed strings** → Closed
- **Extracts JSON** from surrounding text

## 📋 Examples

### Common LLM JSON Issues

```python
from llm_json_fixer import fix_json

# Single quotes (Python style)
fix_json("{'name': 'John', 'city': 'New York'}")
# → {'name': 'John', 'city': 'New York'}

# Trailing commas
fix_json('{"name": "John", "age": 30,}')
# → {'name': 'John', 'age': 30}

# Unquoted keys
fix_json('{name: "John", age: 30}')
# → {'name': 'John', 'age': 30}

# Python booleans
fix_json('{"active": True, "deleted": False, "data": None}')
# → {'active': True, 'deleted': False, 'data': None}

# Missing closing bracket
fix_json('{"name": "John", "age": 30')
# → {'name': 'John', 'age': 30}

# JSON in markdown
fix_json('```json\n{"name": "John"}\n```')
# → {'name': 'John'}

# JSON with surrounding text
fix_json('The result is: {"name": "John", "age": 30} as you can see.')
# → {'name': 'John', 'age': 30}
```

## 🔄 Return Values

### Simple Mode (default)
```python
result = fix_json(json_string)
# Returns: Parsed JSON object or None if fixing failed
```

### Detailed Mode
```python
result = fix_json(json_string, return_dict=True)
# Returns: {
#     'success': bool,      # Whether fixing was successful
#     'data': Any,          # Parsed JSON object (None if failed)
#     'error': str,         # Error message (None if successful)
#     'fixes_applied': list # List of fixes that were applied
# }
```

## 🎯 Use Cases

- **LLM Integration**: Clean up JSON responses from ChatGPT, Claude, etc.
- **API Development**: Handle malformed JSON from various sources
- **Data Processing**: Fix JSON in log files or data dumps
- **Web Scraping**: Clean up JSON extracted from web pages
- **Configuration Files**: Fix manually edited JSON configs

## 📄 License

MIT License - see LICENSE file for details.

## 🤝 Contributing

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

## 📝 Changelog

### 1.0.1
- Initial release
- Basic JSON fixing functionality
- Support for common LLM JSON issues
- Zero dependencies
- Simple API

## 🆘 Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.