A forgiving JSON parser that recovers broken JSON from LLM outputs
Project description
sloppy-json
A forgiving JSON parser that recovers broken JSON from LLM outputs.
Installation
uv add sloppy-json
or with pip:
pip install sloppy-json
Quick Start
from sloppy_json import parse
# Handles almost any broken JSON (permissive by default)
result = parse("{'name': 'test', 'active': True,}")
# Returns: '{"name": "test", "active": true}'
Usage
from sloppy_json import parse, RecoveryOptions
# Default: permissive parsing (maximum recovery)
result = parse("Here is the JSON: {name: 'test'") # handles everything
# Strict parsing (standard JSON only)
result = parse('{"key": "value"}', RecoveryOptions(strict=True))
# Custom options
opts = RecoveryOptions(
allow_single_quotes=True,
allow_trailing_commas=True,
)
result = parse("{'key': 'value',}", opts)
Features
| Feature | Example Input | Output |
|---|---|---|
| Single quotes | {'key': 'value'} |
{"key": "value"} |
| Unquoted keys | {key: "value"} |
{"key": "value"} |
| Trailing commas | {"a": 1,} |
{"a": 1} |
| Missing commas | {"a": 1 "b": 2} |
{"a": 1, "b": 2} |
| Python literals | {"flag": True} |
{"flag": true} |
| Truncated JSON | {"key": "val |
{"key": "val"} |
| Code blocks | ```json {"a":1}``` |
{"a": 1} |
| Comments | {"a": 1} // comment |
{"a": 1} |
| JS undefined | {"a": undefined} |
{"a": null} |
| NaN/Infinity | {"a": NaN} |
{"a": "NaN"} |
Auto-detection
Automatically detect what options are needed from sample data:
from sloppy_json import parse, RecoveryOptions
samples = ["{'key': 'value',}", "{name: True}"]
options = RecoveryOptions.detect_from(samples)
# options.allow_single_quotes == True
# options.allow_trailing_commas == True
# options.allow_unquoted_keys == True
# options.convert_python_literals == True
result = parse(new_json, options)
Documentation
- Getting Started - Overview and quick start
- Options Reference - All
RecoveryOptionsexplained - Auto-Detection - Automatically detect required options
- Error Handling - Exceptions and partial recovery
- Examples - Common scenarios and recipes
Similar Projects
- sloppy-xml-py - A sloppy XML parser by Armin Ronacher for handling malformed XML. The naming of this project was inspired by sloppy-xml-py.
- json5 - A JSON extension that allows comments, trailing commas, and unquoted keys.
License
MIT
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 sloppy_json-0.2.0.tar.gz.
File metadata
- Download URL: sloppy_json-0.2.0.tar.gz
- Upload date:
- Size: 209.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00b9c98f7a7a63a4c75cff59877807b050ae394bad83d345b9a1d664e6bbccc9
|
|
| MD5 |
b6367f68fc9be2806f2efded1a6f6ede
|
|
| BLAKE2b-256 |
1a7cfb28396552434a5fa0519d6fa9992fb59509df5b1e4f0685401b39de7cec
|
Provenance
The following attestation bundles were made for sloppy_json-0.2.0.tar.gz:
Publisher:
publish.yml on cemrehancavdar/sloppy-json
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sloppy_json-0.2.0.tar.gz -
Subject digest:
00b9c98f7a7a63a4c75cff59877807b050ae394bad83d345b9a1d664e6bbccc9 - Sigstore transparency entry: 780738676
- Sigstore integration time:
-
Permalink:
cemrehancavdar/sloppy-json@63c03c2e58f37185d7071dee72e87cd01a61e9ad -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cemrehancavdar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63c03c2e58f37185d7071dee72e87cd01a61e9ad -
Trigger Event:
release
-
Statement type:
File details
Details for the file sloppy_json-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sloppy_json-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d07bc8a3be0dce9467c2c6a2b1a6163ae02105bc4c6e218cb69f257d23d754d5
|
|
| MD5 |
4ba9da9b3eb421d1e26a022838975f9b
|
|
| BLAKE2b-256 |
2eda6d90bb38ea139f460b5b095d640ee34a4b7488d1243eb6a7cc343bdee9c5
|
Provenance
The following attestation bundles were made for sloppy_json-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on cemrehancavdar/sloppy-json
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sloppy_json-0.2.0-py3-none-any.whl -
Subject digest:
d07bc8a3be0dce9467c2c6a2b1a6163ae02105bc4c6e218cb69f257d23d754d5 - Sigstore transparency entry: 780738677
- Sigstore integration time:
-
Permalink:
cemrehancavdar/sloppy-json@63c03c2e58f37185d7071dee72e87cd01a61e9ad -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cemrehancavdar
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@63c03c2e58f37185d7071dee72e87cd01a61e9ad -
Trigger Event:
release
-
Statement type: