Search code examples
pythonjsoncompiler-errorsjupyter-notebookdata-analysis

JSONDecodeError: Expecting value: line 1 column 1 (char 0) error in local machine environment but not in server based environment


I have got basic knowledge of Python programming and currently am learning data science. While trying to convert a json file content into database one of the code that I ran was:

with open('newyork_data.json') as json_data:
    newyork_data = json.load(json_data)

This command runs completely fine in a server based environment but for some reason is showing the error:

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

while running in local anaconda based environment. I can't find the reason why this is happening, can someone please help me? ENCLOSING:-

  1. Part of syntax in Google Collab:- https://colab.research.google.com/drive/1eAWDfMWg2SIkfka_N8pD5o-gV34w9Ew0?usp=sharing

  2. Complete error:-

    JSONDecodeError                           Traceback (most recent call last)
    <ipython-input-53-1adb23907f4f> in <module>
          1 with open('newyork_data.json') as json_data:
    ----> 2      newyork_data = json.load(json_data)
    
    ~\Anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
        294         cls=cls, object_hook=object_hook,
        295         parse_float=parse_float, parse_int=parse_int,
    --> 296         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
        297 
        298 
    
    ~\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
        346             parse_int is None and parse_float is None and
        347             parse_constant is None and object_pairs_hook is None and not kw):
    --> 348         return _default_decoder.decode(s)
        349     if cls is None:
        350         cls = JSONDecoder
    
    ~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
        335 
        336         """
    --> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
        338         end = _w(s, end).end()
        339         if end != len(s):
    
    ~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
        353             obj, end = self.scan_once(s, idx)
        354         except StopIteration as err:
    --> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
        356         return obj, end
    
    JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Solution

  • The error message suggests that the file is empty. Take a look at what's in the file: A JSON file will typically start with the character '{'. Make sure you are looking at the right copy of the file - if the current working directory is a different one from what you were expecting, you may get a different file by the same name.

    Another possible difference between your desktop and server environments is the default text encoding. If you specify an explicit encoding, your code will behave the same in both places, if the content of the file is the same. The normal encoding for JSON is UTF-8, and using that you get:

    with open('newyork_data.json', encoding='utf-8') as json_data:
         newyork_data = json.load(json_data)