1

I need to work with a lot of json strings as byte objects that look like this:

json_string = b'{"a":"1","b":"2","c":"abc=\"d\""}'.decode('utf8')

when I put them through json.loads()

json_object = json.loads(json_string)

it fails because the double quotes are not correctly escaped. How do I replace the \ with a \\ and is there another option than replacing all \ with \\? Because at other positions in the string it might be unwanted to replace them. The real json strings are longer and more complex than in the example here.

I use python 3.5

Thagor
  • 820
  • 2
  • 10
  • 33
  • 1
    That's not JSON. Insist that whoever is sending you strings like that fix their own code, rather than forcing you to write hacky workarounds for the mess they've made. – Zero Piraeus Nov 06 '17 at 17:26
  • How do these non-json strings come to exist? – juanpa.arrivillaga Nov 06 '17 at 17:30
  • They origin from WAT files and contain http(s) headers an example for one of the "real" key value pairs that make trouble look like this: `,"Connection":"close","p3p":"CP=\"CAO PSA OUR\"","cache-control":"no-store",` – Thagor Nov 06 '17 at 18:42
  • According to this it should work with single \ https://stackoverflow.com/a/15637481/3254405 – boateng Nov 06 '17 at 19:27
  • Sadly it does not it fails with the following exception `JSONDecodeError: Expecting ',' delimiter: line 1 column 1410 (char 1409)` unless i delete the key value pair. Which is not an option if have to parse millions of them – Thagor Nov 06 '17 at 20:40
  • What if you don't decode and just load the json string.. https://stackoverflow.com/a/20199213/3254405 – boateng Nov 06 '17 at 21:04
  • If I wrap the string into a file stream like this: `json_io = io.StringIO((json_string))` I still get the same exception when I then try to load it with `json.load(json_io)` . – Thagor Nov 06 '17 at 22:15
  • 1
    Try putting r inside loads again (not load): https://stackoverflow.com/a/9156466/3254405 – boateng Nov 07 '17 at 02:04
  • adding `r` in front of the string does the trick and I can load the json simply with `json.loads()` – Thagor Nov 07 '17 at 09:46

0 Answers0