Home > other >  How can I fix this error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How can I fix this error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Time:01-07

I am trying to create an REST API and a part of the project requires working with JSON files. I am trying to open a JSON file, to check whether certain content exists in the file. If it doesn't, it will be added. For that reason, I am trying to load the JSON file in order to use the dictionary methods. When I open the file and try to use the json.loads() function, I get an error. I am attaching the part of the code that doesn't work.

        file = open(self.storage_file_name, 'r')
        if file_is_empty:
            content_to_write = json.dumps(self.states_population_json, indent=4)
            file.write(content_to_write)
        else:
            current_date = str(date.today())
            print(file.read())
            json_content = json.loads(file.read()) # THIS IS WHERE THE ISSUE APPEARS <<<
            if current_date in self.states_population_json["features"].keys():
                pass
            else:
                json_content["features"][current_date] = self.states_population_json["features"][current_date]

Here's the error as well:

Traceback (most recent call last):
  File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\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)

I tried using json.load() instead of json.loads(), but then I get the following error:

Traceback (most recent call last):
  File "C:\Users\denis\Desktop\Programming\ESRI_Internship\test_file.py", line 89, in <module>
    data.save_data_json()
  File "C:\Users\denis\Desktop\Programming\ESRI_Internship\test_file.py", line 79, in save_data_json
    json_content = json.load(file)
  File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\denis\AppData\Local\Programs\Python\Python39-32\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)

Process finished with exit code 1

I made sure that the file isn't empty. Thanks in advance!

EDIT: Here is the content of the file (shortened a bit):

{
    "features": {
        "2022-01-06": [
            {
                "STATE_NAME": "Alabama",
                "POPULATION": 5028316
            },
            {
                "STATE_NAME": "North Carolina",
                "POPULATION": 10736879
            },
            {
                "STATE_NAME": "North Dakota",
                "POPULATION": 321419
            }
        ]
    }
}

CodePudding user response:

The line print(file.read()) moves the file cursor for reading to the end of the file (the command reads the whole file). A subsequent file.read() thus will not be able to read anything. Just remove the print statement, or if needed, move the file cursor back to the beginning of the file by executing file.seek(0) before the json.loads() statement.

  • Related