Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
empty json file error
#1
I wrote this and since it creates an empty file it give me an error.
with open("food.json", "r+") as file:
    food = json.load(file)
Error:
Traceback (most recent call last): File "c:/Users/User/MyStuff/mltipls.py", line 4, in <module> food = json.load(file) File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load return loads(fp.read(), File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads return _default_decoder.decode(s) File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\User\AppData\Local\Programs\Python\Python38-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 fixed it with:
with open("food.json", "r+") as file:
    try:
        food = json.load(file)
    except:
        food = {}
        json.dump(food, file)
I know empty except statements aren't advisable but I don't know what exception to use.
I tried:
except None
except json.decoder.JSONDecodeError
except JSONDecodeError
These all get their own errors.
Reply
#2
(Jun-17-2020, 09:50 AM)mcmxl22 Wrote: I wrote this and since it creates an empty file it give me an error.
if the file does not exists, it will not create an empty file, but will raise FileNotFoundError.

If the file exists, but is empty or otherwise not valid JSON
import json
with open("food.json", "r+") as file:
    try:
        food = json.load(file)
    except json.decoder.JSONDecodeError:
        print('this is error')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Errors using json file RonR 5 957 Oct-28-2025, 03:00 PM
Last Post: buran
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,252 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  encrypt data in json file help jacksfrustration 1 3,570 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 8,070 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 3,006 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 6,406 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 7,591 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  json decoding error deneme2 10 11,461 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 20,701 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  python requests library .JSON() error mHosseinDS86 6 9,951 Dec-19-2022, 08:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020