Python a badly formatted JSON with "extra" double quotes

96 views Asked by At

I need some help!!

I need to handle in Python a badly formatted JSON with "extra" double quotes. Unfortunately it is not possible to adjust at the origin. Any idea how I can fix this???

Bad JSON

json_input='{"value_ok" : "ok", "empth" : """", "value_2" : ""text"", "value_3" : ""2022-01-01""}'

Attempt 1

d = json.loads(json_input)
...
JSONDecodeError: Expecting ',' delimiter: line 1 column 33 (char 32)

Attempt 2

d = benedict.from_yaml(json_input)
...
expected ',' or '}', but got '<scalar>'
  in "<unicode string>", line 1, column 33:
    {"value_ok" : "ok", "empth" : """", "value_2" : ""text"", "value ... 

Expected result ...

json_adjusted='{"value_ok" : "ok", "empth" : "", "value_2" : "text", "value_3" : "2022-01-01"}'
d = json.loads(json_adjusted)
print(d)
{'value_ok': 'ok', 'empth': '', 'value_2': 'text', 'value_3': '2022-01-01'}
1

There are 1 answers

2
knittl On

You can try to substitute : "" and "",, but I would be careful as this could break the document in other places.

import re
json_input = re.sub(r'""([,}])', r'"\1', re.sub(r': ""', r': "', json_input))