Skip to main content

A simple utility to fix malformed JSON from LLM outputs

Project description

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

pip install llm_json_fixer

🔧 Usage

Basic Usage

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

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

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

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)

result = fix_json(json_string)
# Returns: Parsed JSON object or None if fixing failed

Detailed Mode

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_json_fixer-1.0.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_json_fixer-1.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file llm_json_fixer-1.0.1.tar.gz.

File metadata

  • Download URL: llm_json_fixer-1.0.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for llm_json_fixer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4b0fd39dfd49ed555ae9d091ae62aa5d0faa519ee360482558dbc3ef0081c389
MD5 6747dad41bac70c0c7a2bb5350eabde5
BLAKE2b-256 7dfba0804787ff154c48cf9f644be12688bcb5e18d7851acf71e509106c08025

See more details on using hashes here.

File details

Details for the file llm_json_fixer-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: llm_json_fixer-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for llm_json_fixer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 595990796876fefe096d8a4c648e5c62aa99824773842c4ee75b8986687ac1bf
MD5 93ac867e2049e0595c52f4d4ca82768e
BLAKE2b-256 0b91ad791e3c5d7446e439811f9ed4c4b9a8de3910e3e07c0de2360b2971eb30

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page