1

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

Community
  • 1
  • 1
  • What line gives you an error? It's unclear from the error message. Post actual code and full stack trace of error. – Reut Sharabani Mar 02 '15 at 22:27
  • Paste the full traceback plz – Hackaholic Mar 02 '15 at 22:29
  • Do you mean to have '...' at the beginning? That's probably the offending code. – Elias Benevedes Mar 02 '15 at 22:29
  • @EliasBenevedes That would be a SyntaxError, not a ValueError. – Two-Bit Alchemist Mar 02 '15 at 22:30
  • @EliasBenevedes I thought the same thing, but it passes in python3.4, not sure why :) This is a partial code obviously, as many variables have values already. try `type(...)` and see for yourself! Update: http://stackoverflow.com/questions/772124/what-does-the-python-ellipsis-object-do – Reut Sharabani Mar 02 '15 at 22:31
  • 1
    @ReutSharabani That was added to support NumPy's array slicing. It is a singleton like None. (Sorry wasn't sure if you were using 2 or 3 before.) https://stackoverflow.com/questions/772124/what-does-the-python-ellipsis-object-do – Two-Bit Alchemist Mar 02 '15 at 22:35

2 Answers2

2

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.

tenwest
  • 2,058
  • 1
  • 13
  • 16
  • 2
    In your case, at least, it is because you are using single quotes and [JSON requires double-quoted strings](http://json.org). See `json.loads('{"sean": "310"}')` vs `json.loads('{\'sean\': "310"}')`. Note some browsers are not particular about this -- Python, however, is. – Two-Bit Alchemist Mar 03 '15 at 01:30
  • yes, I realised that later on as I was trying his code (I do not work with json). When I used double quotes, I no longer got a value error, but an attribute error trying to call `write()` on a dict, hence the latter part of the answer. should have included this in my answer though, thanks – tenwest Mar 03 '15 at 20:18
0

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.

Bugs
  • 4,491
  • 9
  • 32
  • 41