Metadata-Version: 2.4
Name: llm_json_fixer
Version: 1.0.1
Summary: A simple utility to fix malformed JSON from LLM outputs
Home-page: https://github.com/mdhasnainali/llm_json_fixer
Author: Md. Hasnain Ali
Author-email: "Md. Hasnain Ali" <mdhasnainali.01@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/mdhasnainali/llm_json_fixer
Project-URL: Repository, https://github.com/mdhasnainali/llm_json_fixer
Project-URL: Issues, https://github.com/mdhasnainali/llm_json_fixer/issues
Project-URL: Documentation, https://github.com/mdhasnainali/llm_json_fixer#readme
Keywords: json,llm,fix,repair,malformed,ai,chatgpt,claude
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup :: HTML
Classifier: Topic :: Utilities
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# 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.
