0
 with open(LATEST_UPDATE_FULL_PATH) as JSON_FILE_UPDATE:
                    JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)
                    JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)

why does doing "load" twice causes the following below? Is there a handle on this file? I could not find anything to release JSON_FILE_UPDATE.

Traceback (most recent call last):
  File "PhthonScript", line 69, in <module>
    JSON_DATA_UPDATE = json.load(JSON_FILE_UPDATE)
  File "...\PythonByMiniconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "...\PythonByMiniconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "...\PythonByMiniconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "...\PythonByMiniconda3\lib\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)
Stefan
  • 372
  • 1
  • 16

1 Answers1

3

try this:

JSON_DATA_UPDATE = json.loads(JSON_FILE_UPDATE)

once it is loaded, then the file converted to json. cannot convert json to json.

another way, this may work on string to json:

import ast

file = """{
  "students": [
    {
      "name": "Millie Brown",
      "active": False,
      "rollno": 11
    },
    {
      "name": "Sadie Sink",
      "active": True,
      "rollno": 10
    }
  ]
}"""

print(type(file))
dict = ast.literal_eval(file)
print(type(dict))

dict['students'][0]['name']  would be "Millie Brown"
dict['students'][1]['active']  would be True
dimz
  • 179
  • 8
  • where does the located file exist (drive, memory...), what is its scope (function, program,...)? Wondering where the converted file lives. I – Stefan Jan 27 '22 at 15:39
  • what format is the LATEST_UPDATE_FULL_PATH content ? – dimz Jan 27 '22 at 15:50
  • it is a text file, JSON formated – Stefan Jan 27 '22 at 16:15
  • actually the result is dictionary type, you can easily extract the content using dict['keyname'] or dict['keyname'][index]['subkeyname']. index start from 0, 1, 2 depends on the lengths of records – dimz Jan 27 '22 at 16:33
  • alright, but JSON_FILE_UPDATE lives where, as it can only be converted "once"? (forgot "where" in my previous comment) – Stefan Jan 29 '22 at 15:35
  • 1
    I think I got it, if `json.load(file)` happens within `with open(path) as file`, the object `file` is converted into a JSON, so `JSON_DATA=json.load(file)` is not necessary, as `json.load(file)` is enough, isn't it? – Stefan Jan 30 '22 at 13:50