0

I tried running this code and get error:

Traceback (most recent call last):
  File "/Users/ccharest/Desktop/PCC/remember_me_2.py", line 7, in <module>
    username = json.load(f_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What is wrong with this block of code?

import json

filename = 'username2.json'

try:
    with open(filename) as f_obj:
        username = json.load(f_obj)
except FileNotFoundError:
    username = input("What is your name? ")
    username = username.title()
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
        print("We'll remember you when you come back {}!".format(username))

else:
    print("Welcome back {}!".format(username))

UPDATE 1 As suggested by some, I changed the filename from "usernam2.json" to "uname.json" and it worked... Why would using filename "username2.json" trigger the error???

UPDATE 2 As suggested by Alexander Huszagh, the file "username2.json" was created, but was empty.I deleted the file "username2.json" and ran the script again and it worked fine.

3
  • 3
    The JSON in username2.json is not valid. Commented Jul 16, 2016 at 14:40
  • 2
    The contents of username2.json might not be valid JSON. Can you post the contents of the file? Commented Jul 16, 2016 at 14:42
  • @carliecat, your file contents are not valid JSON. It clearly exists as a file. Open a text editor. Commented Jul 16, 2016 at 14:56

1 Answer 1

1

As the comments suggest above, your code is fine. The contents of your file, however, are almost certainly empty or not JSON. A quick proof of concept:

In [1]: import json

In [2]: json.loads("a")
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-2-98479bdf8b77> in <module>()
----> 1 json.loads("")

/usr/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    317             parse_int is None and parse_float is None and
    318             parse_constant is None and object_pairs_hook is None and not kw):
--> 319         return _default_decoder.decode(s)
    320     if cls is None:
    321         cls = JSONDecoder

/usr/lib/python3.5/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/usr/lib/python3.5/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

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

Are you sure you actually have JSON data in your file?

If you have any recognized JSON structure, but it is not entirely complete, such as the following, it will raise another decode error:

In [4]: json.loads('{"a": "b"')
---------------------------------------------------------------------------
...

JSONDecodeError: Expecting ',' delimiter: line 1 column 10 (char 9)

If you want to specifically handle the null file example, you can do:

import json
path = "path/to/file.json"
try:
    json.loads(path)
except json.JsonDecodeError as error:
    if (error.msg.startswith("Expecting") and error.pos == 0):
        # ignore the error, empty file, or non-JSON grammar, so it was expecting a JSON element but found none
        # if you would like to differentiate empty files from non-JSON files, use `os.stat` on the path, or pass in a file handle as `file` and use `file.tell()` to check if it returns 0
        pass
    else:
        # another grammar issue
        raise
Sign up to request clarification or add additional context in comments.

5 Comments

Anonymous downvoters, care to comment? This answer is correct.
The file was created, but empty. I deleted the file "username2.json" and ran the script again and it worked fine.
Yeah, the JSONDecodeError is actually expressive in what it tells you @carllecat. "Expecting '%s ', delimiter" (some delimiter) means that the JSON is malformed. "Extra data" means data is trailing the recognized JSON. And "Expecting value" means no JSON could be found.
Thanks Alexander. I appreciate the help.
@carllecat, I added in a working example so you can catch errors from empty files or those with null grammar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.