Skip to main content

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.

Metadata

Release files for llm-json-fixer 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-json-fixer 1.0.1
File Size Uploaded Provenance
llm_json_fixer-1.0.1.tar.gz 6.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-json-fixer 1.0.1
File Interpreter ABI Platform
llm_json_fixer-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / llm_json_fixer-1.0.1.tar.gz

Download URL llm_json_fixer-1.0.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4b0fd39dfd49ed555ae9d091ae62aa5d0faa519ee360482558dbc3ef0081c389
BLAKE2b-256 checksum
How to use checksums
7dfba0804787ff154c48cf9f644be12688bcb5e18d7851acf71e509106c08025
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / llm_json_fixer-1.0.1-py3-none-any.whl

Download URL llm_json_fixer-1.0.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
595990796876fefe096d8a4c648e5c62aa99824773842c4ee75b8986687ac1bf
BLAKE2b-256 checksum
How to use checksums
0b91ad791e3c5d7446e439811f9ed4c4b9a8de3910e3e07c0de2360b2971eb30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Security for llm-json-fixer 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page