0

I have written a python library that has become somewhat complex. Today I noticed that running my code multiple times in a row without changing a single line of code or anything else, results sometimes in an Exception and sometimes not. The error occurs when parsing a json string using the json.loads method. (The string in question is valid json format '["1", "6", "12", "14", "36", "44"]'). Due to the fact that the error appeared after working on the project for a long time and I am not sure what causes it I don't know how to provide a minimum work example. The code is simply of type

result = json.loads(string) # where string is the aforementioned list

When executing the script consecutive times in a row, most of the times the code finished without exception while sometimes it raises

  File "myfile.py", line 800, in get
    opt = json.loads(opt)
  File "/usr/lib64/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

(I have omitted the lines preceding the part which is causing the exception). Unfortunatley I am not sure how to investigate what is going on since the traceback of the error alone gives me no information on what happens differently to when the code runs fine. Are there any ways to compare what the interpreter does between two runs?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Torilla
  • 383
  • 5
  • 16
  • Is this related? https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes – Dennis Jan 06 '22 at 00:18
  • That could indeed be a reason I will check, however it still strikes me as odd that two consecutive runs with the exact same data produces different results. – Torilla Jan 06 '22 at 00:21
  • Can you do a `try: json.loads(...) except Exception: print(ascii (string))` to see what the string is you're trying to parse? – Dennis Jan 06 '22 at 00:25
  • Does this answer your question? [How can I diagnose common errors in JSON data?](https://stackoverflow.com/questions/75188362/how-can-i-diagnose-common-errors-in-json-data) – Karl Knechtel Apr 01 '23 at 07:42

0 Answers0