A python wrapper for this amazing NodeJS application https://github.com/RyanMarcus/dirty-json
- Be sure to have node installed on your machine (otherwise go to https://nodejs.org/en/download/)
- Install the node requirements:
npm install
- Install the Python requirements:
pip install -r requirements.txt
import py_wrapper
import json
# ... have the broken string
string = '{something: ""very" broken"}'
fixed_string = py_wrapper.fix_json(string)
json.loads(fixed_string)
It would be nice to run as a REST webservice, to avoid language dependency.
This needs some few files / prebuilt docker image
docker build -t martinomensio/dirtyjson .
docker run -dit --name dirtyjson-rest -p 12345:12345 martinomensio/dirtyjson
Or from the docker hub (same command)
docker run -dit --name dirtyjson-rest -p 12345:12345 martinomensio/dirtyjson
Run a request to the microservice:
curl -X POST \
http://localhost:12345 \
-H 'Content-Type: text/plain' \
-d '{broken:json}'
or in python:
import requests
requests.post('http://localhost:12345', data='{broken:json}', headers={'content-type': 'text/plain'}).json()
This has a bit of latency, just run the requested when you are sure your JSON is broken (e.g. in the catch/except).