A Python library that tries to fix broken JSON strings. This is a Python port of the JavaScript library jsonrepair.
This Python package attempts to repair malformed JSON strings. It is designed to handle common cases of bad JSON, as follows:
- Add missing quotes around keys
- Add missing escape characters
- Add missing commas
- Add missing closing brackets
- Replace single quotes with double quotes
- Replace special quote characters like “...” with regular double quotes
- Replace special white space characters with regular spaces
- Replace Python constants None, True, and False with null, true, and false
- Strip trailing commas
- Strip comments like /* ... */ and // ...
- Strip JSONP notation like callback({ ... })
- Strip escape characters from an escaped string like {"stringified": "content"}
- Strip MongoDB data types like NumberLong(2) and ISODate("2012-12-19T06:01:17.171Z")
- Concatenate strings like "long text" + "more text on next line"
- Turn newline-delimited JSON into a valid JSON array, for example: { "id": 1, "name": "John" } { "id": 2, "name": "Sarah" }
git clone git@github.com:aleksandrphilippov/json-fixer.git
cd json-fixer
After cloning the repository, navigate to the project directory and run the example script:
python example.py
If you have Docker and Docker Compose installed, you can use the provided Dockerfile
and docker-compose.yml
to build
and run the application in a Docker container. This avoids the need to install Python and other dependencies on your
local machine.
Build the Docker image:
docker compose build
Run the application in a Docker container:
docker compose up -d
This will start the json_fixer
service as defined in your docker-compose.yml
file. The application will run using
the command specified in the CMD
directive of your Dockerfile
(in this case, python example.py
).
To stop the Docker container:
docker compose down
To remove the Docker image:
docker rmi json-fixer_json_fixer
Another option for managing the project is to use the Makefile
:
Build the Docker containers:
make build
Start the application:
make start
Stop the application:
make stop
Tail the logs for the application:
make logs
Run tests:
make test
To use json_fixer
in your Python script, you first need to import the module and then call the fix
function with
your malformed JSON string as the argument. Here is an example:
from json_fixer import fix_json
broken_json = '{`name`: "John", age: 30, "city": New York}'
fixed_json = fix_json(broken_json)
print(fixed_json)
This will output:
{"name": "John", "age": 30, "city": "New York"}
To run the tests for this project, navigate to the project directory in your terminal and run:
python -m unittest json_fixer.fixer_test
We welcome contributions! Please see CONTRIBUTING.md for details on how to contribute to this project.
- Fork the repository
- Create your feature branch (
git checkout -b feature/YourFeature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin feature/YourFeature
) - Create a new Pull Request
This project is a Python port of the jsonrepair JavaScript library by Jos de Jong. Huge thanks to him for the original implementation.
This project is licensed under the MIT License - see the LICENSE.md file for details.