mangiucugna / json_repair

A python module to repair invalid JSON, commonly used to parse the output of LLMs
https://pypi.org/project/json-repair/
MIT License
826 stars 48 forks source link

[Bug]: Extra backslash when repairing JSON with escaped double quote #62

Closed didmar closed 2 months ago

didmar commented 2 months ago

Version of the library

0.26.0

Describe the bug

It looks like repairing the object leads to escaped double quotes getting an extra backslash.

How to reproduce

Run the following snippet:

import json_repair

a = '{"foo": "\\"bar\\""}'
print(json_repair.loads(a))
# {'foo': '"bar"'}
# => OK!

b = """{
  "items": [
    {
      "foo": "\\"bar\\""
    }
"""
print(json_repair.loads(b))
# {'items': [{'foo': '\\"bar"'}]}
# => KO, expected {'items': [{'foo': '"bar"'}]}

c = """{
  "items": [
    {
      "foo": "\\"bar\\""
    }
  ]
}"""
print(json_repair.loads(c))
# {'items': [{'foo': '"bar"'}]}
# => OK!

Expected behavior

No extra backslash in the parsed string