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
  encrypt data in json file help jacksfrustration 1 192 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  json loads throwing error mpsameer 8 679 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 729 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,498 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,098 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  json decoding error deneme2 10 3,627 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,407 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  python requests library .JSON() error mHosseinDS86 6 3,422 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  validate large json file with millions of records in batches herobpv 3 1,264 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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