This simple package can be used to repair a broken json file. To know all cases in which this package will work, check out the unit test.
Inspired by https://github.com/josdejong/jsonrepair with contributions by GPT-4
Motivation
I was using GPT a lot and there is no sure fire way to get structured output out of it. You can ask for a JSON output or use the Functions paradigm, either way the documentation from OpenAI clearly states that it might not return a valid JSON. Luckily, the mistakes GPT makes are simple enough to be fixed without destroying the content. I searched for a lightweight python package but couldn't find any.
So I wrote this one.
You can look how I used it by checking out this demo: https://huggingface.co/spaces/mangiucugna/difficult-conversations-bot/
How to use
from json_repair import repair_json
good_json_string = repair_json(bad_json_string)
How it works
This module will parse the JSON file following the BNF definition:
<json> ::= <primitive> | <container>
<primitive> ::= <number> | <string> | <boolean>
; Where:
; <number> is a valid real number expressed in one of a number of given formats
; <string> is a string of valid characters enclosed in quotes
; <boolean> is one of the literal strings 'true', 'false', or 'null' (unquoted)
<container> ::= <object> | <array>
<array> ::= '[' [ <json> *(', ' <json>) ] ']' ; A sequence of JSON values separated by commas
<object> ::= '{' [ <member> *(', ' <member>) ] '}' ; A sequence of 'members'
<member> ::= <string> ': ' <json> ; A pair consisting of a name, and a JSON value
If something is wrong (a missing parantheses or quotes for example) it will use a few simple heuristics to fix the JSON string:
- Add the missing parentheses if the parser believes that the array or object should be closed
- Quote strings or add missing single quotes
- Adjust whitespaces and remove line breaks
I am sure some corner cases will be missing, if you have examples please open an issue or even better push a PR
How to develop
Just create a virtual environment with requirements.txt, the setup uses pre-commit to make sure all tests are run
How to release
You will need owner access to this repository
- Edit
pyproject.tomland update the version number appropriately usingsemvernotation - Run
python -m build - Commit and push all changes to the repository before continuing or the next steps will fail
- Create a new release in Github, making sure to tag all the issues solved and contributors. Create the new tag, same as the one in the build configuration
- Once the release is created, a new Github Actions workflow will start to publish on Pypi, make sure it didn't fail
Bonus Content
If you need some good Custom Instructions (System Message) to improve your chatbot responses try https://gist.github.com/mangiucugna/7ec015c4266df11be8aa510be0110fe4
Metadata
Release files for json-repair 0.1.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json_repair-0.1.10.tar.gz | 8.3 kB | Without provenance | Details for json_repair-0.1.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_repair-0.1.10-py3-none-any.whl | Python 3 | none | any | Details for json_repair-0.1.10-py3-none-any.whl |
Total release size: 15.7 kB
Release files / json_repair-0.1.10.tar.gz
| Download URL | json_repair-0.1.10.tar.gz |
|---|---|
| Size | 8.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7349cb6362eae781ecc1ff5ce09342e65bba7a13a85c1bad8d27e149491167ee
|
|
BLAKE2b-256 checksum How to use checksums |
b7ecef3c1cbe7bfacc6c78e24b8ea9c00287e0cb051727980bca57da5ca37b6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / json_repair-0.1.10-py3-none-any.whl
| Download URL | json_repair-0.1.10-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e3f8a1b7c89b37b64f2d522165f041f008c40fd5eee1fa42f599cfeb644cc67a
|
|
BLAKE2b-256 checksum How to use checksums |
139f591efda05176f9a68f25858a1d2f8bc9bdea02de62199077ace7196933fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for json-repair 0.1.10
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.