Repair broken JSON from LLM outputs
Project description
jsonfix-llm
Repair broken JSON from LLM outputs. Stop fighting malformed JSON.
Install
pip install jsonfix-llm
Quick Start
from jsonfix_llm import repair_json
fixed = repair_json("""{
'name': 'shashi'
'age': 25
}""")
# {"name": "shashi", "age": 25}
Features
- Markdown fence stripping — removes
```json ... ```wrappers - Comment stripping — removes
//and/* */comments - Quote fixing — single quotes → double quotes, unquoted keys → quoted
- Literal fixing —
True/False/None→true/false/null - Comma fixing — inserts missing commas, removes trailing commas
- Value fixing — fills missing values, fixes missing colons
- Control char escaping — escapes literal newlines/tabs in strings
- Bracket auto-close — appends missing
}or], handles truncation - Code extraction — extracts code from fenced, indented, inline, and XML blocks
- CLI tool — quick repair from the terminal
API
from jsonfix_llm import repair_json, extract_code, extract_json
# Simple repair
fixed = repair_json(text)
# Rich result
result = repair_json(text, rich=True)
print(result.fixed)
print(result.was_repaired)
print(result.fixes)
print(result.error_count)
print(result.errors)
# Extract code (default: first Python block)
code = extract_code(text)
# language-specific: extract_code(text, language="python")
# all blocks: extract_code(text, language="json", all=True)
# Extract JSON blocks
json_block = extract_json(text)
json_blocks = extract_json(text, all=True)
CLI
# Repair JSON file
jsonfix broken.json
# Read from stdin
echo '{"a":1' | jsonfix
# Write to file
jsonfix in.json -o fixed.json
# Show repair stats
jsonfix in.json --stats
# Extract code blocks
jsonfix extract file.md --language python
jsonfix extract file.md --language python -o output.py
jsonfix extract file.md --language json --all
Development
pip install -e ".[dev]"
pytest tests/ -v
Publishing to PyPI
First-time setup
pip install build twine
Create an account at pypi.org and generate an API token at https://pypi.org/manage/account/token/.
First-time publish
python -m build
twine upload dist/*
# Username: __token__
# Password: <your-pypi-token>
Bug fix release (patch)
After fixing a bug:
- Bump the patch version in
pyproject.toml:version = "0.1.0" → version = "0.1.1" - Build & upload:
python -m build twine upload dist/*
- Commit & push:
git add -A git commit -m "Bump version to 0.1.1" git push
New feature release (minor / major)
- Bump version in
pyproject.toml(semver:MAJOR.MINOR.PATCH):- New feature:
0.1.0→0.2.0 - Breaking change:
0.1.0→1.0.0
- New feature:
- Update CHANGELOG.md
- Build, upload, commit & push (same steps as above)
Note: PyPI does not allow re-uploading the same version. Always bump before uploading.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonfix_llm-0.1.1.tar.gz.
File metadata
- Download URL: jsonfix_llm-0.1.1.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8935a23ce91fbf1a5221885b7f82a2c1a2e251b3cc2c13ada23637c5392f31ce
|
|
| MD5 |
71d7b4e62a205f0712a7c834d220c75f
|
|
| BLAKE2b-256 |
beedbb666e76734e69d547fc543b8d781ba7b25e9ed95f1ba800da5f8df49480
|
File details
Details for the file jsonfix_llm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: jsonfix_llm-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6ea3eb16600294d0134b40ad12c6774e4adb4989ea474b560e08fbf92638b18
|
|
| MD5 |
d316306f439aa55b468d74dddb47445f
|
|
| BLAKE2b-256 |
670d62a1272741608f3202556038e4f43049383fc75d5e9d886b129b96279643
|