Skip to main content

A forgiving JSON parser that recovers broken JSON from LLM outputs

Project description

sloppy-json

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

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


Download files

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

Source Distribution

sloppy_json-0.2.0.tar.gz (209.5 kB view details)

Uploaded Source

Built Distribution

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

sloppy_json-0.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

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

Hashes for sloppy_json-0.2.0.tar.gz
Algorithm Hash digest
SHA256 00b9c98f7a7a63a4c75cff59877807b050ae394bad83d345b9a1d664e6bbccc9
MD5 b6367f68fc9be2806f2efded1a6f6ede
BLAKE2b-256 1a7cfb28396552434a5fa0519d6fa9992fb59509df5b1e4f0685401b39de7cec

See more details on using hashes here.

Provenance

The following attestation bundles were made for sloppy_json-0.2.0.tar.gz:

Publisher: publish.yml on cemrehancavdar/sloppy-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for sloppy_json-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d07bc8a3be0dce9467c2c6a2b1a6163ae02105bc4c6e218cb69f257d23d754d5
MD5 4ba9da9b3eb421d1e26a022838975f9b
BLAKE2b-256 2eda6d90bb38ea139f460b5b095d640ee34a4b7488d1243eb6a7cc343bdee9c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sloppy_json-0.2.0-py3-none-any.whl:

Publisher: publish.yml on cemrehancavdar/sloppy-json

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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