Skip to main content

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 fixingTrue/False/Nonetrue/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

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

jsonfix_llm-0.1.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

jsonfix_llm-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonfix_llm-0.1.0.tar.gz.

File metadata

  • Download URL: jsonfix_llm-0.1.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for jsonfix_llm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 889e001c6874b19f51741d7f2e5264939cbabe5f3de4005255c7ad2fd63c5961
MD5 e1aa2a1b073e582bb14c01e9cf47170b
BLAKE2b-256 2d1c70d1dda14d0f6cde5bea79d7e6aa13724d3ed3eb776e0d9697b7c9ce87d1

See more details on using hashes here.

File details

Details for the file jsonfix_llm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonfix_llm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for jsonfix_llm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf75c28b6a0d6360f826bfad87b2103e1ca852b6d1316ecfbe7a292a960051ad
MD5 89ce6993f275358de82207c1cf5b9364
BLAKE2b-256 05ce52c287d2a4e45ea29541e38a8d2be117b5f3740b4507ef29059c4c2366c0

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