Jose Dzireh Chong
Jose Dzireh Chong

Reputation: 143

I'm getting a JSONDecodeError on line 357. I don't even have a line 357

My code. The stuff with filepath are the important bits in my opinion.

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 14:47:36 2017

@author: Jose Chong
"""
import json
import tkinter
import os

filepath = os.path.expanduser(r'~\Documents\joseDzirehChongToDoList\toDoListSaveFile.json')

master = tkinter.Tk()
master.title("To-Do List")
master.geometry("300x300")

masterFrame = tkinter.Frame(master)

masterFrame.pack(fill=tkinter.X)

checkboxArea = tkinter.Frame(masterFrame, height=26)

checkboxArea.pack(fill=tkinter.X)

inputStuff = tkinter.Frame(masterFrame)

checkboxList = []

with open(filepath) as infile:    
    checkboxList = json.load(infile)

def destroyCheckbox(checkbox, row):
    row.destroy()
    del checkboxList [checkboxList.index(str(checkbox))]

    with open(filepath, 'w') as outfile:
        json.dump(checkboxList, outfile)

for savedCheckbox in checkboxList:
    checkboxRow = tkinter.Frame(checkboxArea)
    checkboxRow.pack(fill=tkinter.X)
    checkbox1 = tkinter.Checkbutton(checkboxRow, text=savedCheckbox)
    checkbox1.pack(side=tkinter.LEFT)
    deleteItem = tkinter.Button(checkboxRow, text="x", bg="red", fg="white",
                                activebackground="white", activeforeground="red",
                                command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r))
    deleteItem.pack(side=tkinter.RIGHT)

    with open(filepath, 'w') as outfile:
        json.dump(checkboxList, outfile)

def drawCheckbox():
    newCheckboxInput = entry.get()
    def destroyCheckbox():
        checkboxRow.destroy()
        del checkboxList[checkboxList.index(newCheckboxInput)]
        with open(filepath, 'w') as outfile:
            json.dump(checkboxList, outfile)                 
    checkboxList.append(newCheckboxInput)
    entry.delete(0,tkinter.END)
    checkboxRow = tkinter.Frame(checkboxArea)
    checkboxRow.pack(fill=tkinter.X)
    checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1])
    checkbox1.pack(side=tkinter.LEFT)
    deleteItem = tkinter.Button(checkboxRow, text = "x", command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r), bg="red", fg="white", activebackground="white", activeforeground="red")
    deleteItem.pack(side=tkinter.RIGHT)

    with open(filepath, 'w') as outfile:
        json.dump(checkboxList, outfile)


def createInputStuff():
    paddingFrame = tkinter.Frame(inputStuff, height=5)
    paddingFrame.pack(fill=tkinter.X)
    buttonDone.pack()
    inputStuff.pack()
    buttonAdd.pack_forget()
    master.bind('<Return>', lambda event: drawCheckbox())

def removeInputStuff():
    inputStuff.pack_forget()
    buttonAdd.pack()
    buttonDone.pack_forget()
    master.unbind('<Return>')


buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff)


buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff)
buttonAdd.pack()


topInput = tkinter.Frame(inputStuff)
bottomInput = tkinter.Frame(inputStuff)

topInput.pack()
bottomInput.pack()

prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?")
prompt.pack()
entry = tkinter.Entry(bottomInput, bd=3)
entry.pack(side=tkinter.LEFT)
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox)
buttonConfirm.pack(side=tkinter.LEFT)

master.mainloop()

Using print(filepath) I can see that the filepath is valid, "C:\Users\Josalina\Documents\joseDzirehChongToDoList\toDoListSaveFile.json" is printed. However, I'm getting this error (go to the end of the error the rest is just for context if that may be needed)

Traceback (most recent call last):

  File "<ipython-input-33-f554d849ad97>", line 1, in <module>
    runfile('C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py', wdir='C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile')

  File "C:\Users\Josalina\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Users\Josalina\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Josalina/Desktop/Coding/Language - Python/to-do-list-to-compile/toDoList.py", line 30, in <module>
    checkboxList = json.load(infile)

  File "C:\Users\Josalina\Anaconda3\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)

  File "C:\Users\Josalina\Anaconda3\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)

  File "C:\Users\Josalina\Anaconda3\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\Josalina\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value

I don't even have a line 357, I only have 107 lines of code. How do I fix this? The idea is that it's supposed to add the items of the list to the JSON file, basically this but the JSON file is in a different location.

Upvotes: 0

Views: 643

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191963

It's an empty file... the program refuses to run and so it's an empty file

You can't json.load an empty file. That is why the program refuses to run.

Your code here...

checkboxList = []

with open(filepath) as infile:    
    checkboxList = json.load(infile)

You could simply put [] in the file. It just cannot be empty.

The list is supposed to be added to the file on the first run of the program

Then you need something like this to add "to the file", not read "from the file"

with open(filepath, 'w') as fp:
    json.dump(checkboxList, fp)

Upvotes: 1

Related Questions