0

I originally only used ujson as follows. This code has been working for sometime and I'm not sure how I broke it.

import ujson as json

with open('performance_data.json', 'r') as f:
    data = json.load(f)

It just today started throwing a ValueError

ValueError: Expected object or value

I tried loading the .json file using python in terminal with ujson and I got the same error. Then I tried loading it using json package instead of ujson, and it worked fine, in python terminal. So I added in a try except to use json instead of ujson so now my code looks like this

import json
import ujson

with open('performance_data.json', 'r' as f:
    try:
        data = ujson.load(f)
    except ValueError:
        data = json.load(f)

However this is still giving me problems.

json Traceback:

  File "live_paper.py", line 141, in main
    data = json.load(f)
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I would normally assume that this means the file is empty. However I can run the following code from script and see the file content.

with open('performance_data.json', 'r') as f:
    print(f.readline())

I've checked os.getcwd() is correct from script.

So summarizing, json.load(f) works from terminal but not when the script is ran. In terminal I can sift through my data and everything looks as it should.

ujson.load() works neither in terminal or from script and json.load() doesnt work from script.

Gustavo Costa
  • 107
  • 1
  • 1
  • 8
  • 1
    is anything modifying the content of the file? – Anentropic Jun 10 '22 at 17:04
  • @Anentropic actually yes, the file itself is created daily. That didn't occur to me just because those processes haven't been changed and because json.load works fine through the terminal. I'll look into that as well. – Gustavo Costa Jun 10 '22 at 19:39
  • 1
    It seemed to me that the facts in your question couldn't all be true, either you were mistaken about one of them or something is happening outside of what is described. If not mistaken then the easiest explanation would be something is changing the content of the file and the behaviour you get is random rather than consistent - i.e. sometimes you can parse the file with `json`, sometimes it appears empty, and sometimes it has content but you can't parse it (maybe it's in middle of being written and incomplete?) - I doubt it is anything to do with json vs ujson or shell vs script. – Anentropic Jun 11 '22 at 14:55
  • @Anentropic turns out there was nan in my json. Apparently the json library handles nan's better than ujson does. Could have to do with why ujson is faster. – Gustavo Costa Jun 13 '22 at 22:40
  • NaN is not valid JSON https://stackoverflow.com/a/6602204/202168 – Anentropic Jun 14 '22 at 12:03

1 Answers1

1

The problem is there is no valid json in your file for the module to load. You can verify this by trying to print the contents of the file using f.read() inside of your with statement. I know you have said that you tried this but there is a difference between the file not being empty and having valid json. The function being called will fail if there is not a valid json object found.

pgrad
  • 109
  • 3
  • That makes sense. I'll look into that and report back. Though it still wouldnt explain why json.load works fine through the terminal would it? – Gustavo Costa Jun 10 '22 at 19:40