user4623845
user4623845

Reputation:

Python - ValueError: Expecting value: line 1 column 1 (char 0)

This produces and error:

ValueError: Expecting value: line 1 column 1 (char 0)

Here is my code:

...

print("Your phonebook contains the following entries:")
for name, number in phoneBook.items():
    print("%s - %s" % (name, number))

while not created:
    if not os.path.isfile('phonebook.json'):
        with open('phonebook.json', 'wb') as f:
            try:
                f.write('{}')
            except TypeError:
                {}
        created = True
        print('New phonebook created!')
    else:
        print('Phonebook found!')
        created = True

with open('phonebook.json', 'r') as f:
    try:
        phoneBook_Ori = json.load(f)
        phoneBook_Upd = dict(phoneBook_Ori.items() + phoneBook.items())
        phoneBook_Ori.write(phoneBook_Upd)
    except EOFError:
        {}
if EOFError:
    with open('phonebook.json', 'w') as f:
        json.dump(phoneBook, f)
else:
    with open('phonebook.json', 'w') as f:
        json.dump(phoneBook_Ori, f)

Has anyone got an idea of how to fix this?

I have also previously asked a question on this code here

Upvotes: 1

Views: 16670

Answers (2)

Sushant Sikka
Sushant Sikka

Reputation: 1

I was getting this error whilst using json.load(var) with var containing an empty JSON response from a REST API call.

In your case, the JSON response (phonebook.json) must have records. This will fix the error.

Upvotes: 0

tenwest
tenwest

Reputation: 2317

I copy pasted your code in the python 2.x interpreter.

I received a ValueError regarding the phonebook.json file. I created a dummy file with:

{'sean':'310'}

My error reads: ValueError: Expecting property name: line 1 column 2

This was the only way I was able to receive a ValueError. Therefore, I believe your issue lies in the way the json is written in phonebook.json. Can you post its contents or a subset?

Also, using phoneBook_Ori.write() seems very questionable, as the json module has no method called write(), and the return on json.load(), if used on json objects, is a dictionary, which also cannot write(). You would probably want to use json.dump().

read more at: https://docs.python.org/2/library/json.html

Anyway, I hope I was helpful.

Upvotes: 2

Related Questions