Home > Software engineering >  Reading multiple JSON files in python using Jupiter notebook
Reading multiple JSON files in python using Jupiter notebook

Time:09-20

I am trying to read a folder containing multiple JSON files, for example l am trying to analyze the first JSON file in the folder but getting errors.

This is code for the location and importing the first file

folder = "D:\FastCharge" 
df=pd.read_json(folder[0])
df.head()

However, l am getting the following error

ValueError                                Traceback (most recent call last)
<ipython-input-5-871efd5d92b5> in <module>
----> 1 df=pd.read_json(folder[0])
      2 df.head()

C:\Anaconda\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    197                 else:
    198                     kwargs[new_arg_name] = new_arg_value
--> 199             return func(*args, **kwargs)
    200 
    201         return cast(F, wrapper)

C:\Anaconda\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    297                 )
    298                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 299             return func(*args, **kwargs)
    300 
    301         return wrapper

C:\Anaconda\lib\site-packages\pandas\io\json\_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, storage_options)
    561 
    562     with json_reader:
--> 563         return json_reader.read()
    564 
    565 

C:\Anaconda\lib\site-packages\pandas\io\json\_json.py in read(self)
    692                 obj = self._get_object_parser(self._combine_lines(data_lines))
    693         else:
--> 694             obj = self._get_object_parser(self.data)
    695         self.close()
    696         return obj

C:\Anaconda\lib\site-packages\pandas\io\json\_json.py in _get_object_parser(self, json)
    714         obj = None
    715         if typ == "frame":
--> 716             obj = FrameParser(json, **kwargs).parse()
    717 
    718         if typ == "series" or obj is None:

C:\Anaconda\lib\site-packages\pandas\io\json\_json.py in parse(self)
    829 
    830         else:
--> 831             self._parse_no_numpy()
    832 
    833         if self.obj is None:

C:\Anaconda\lib\site-packages\pandas\io\json\_json.py in _parse_no_numpy(self)
   1077         if orient == "columns":
   1078             self.obj = DataFrame(
-> 1079                 loads(json, precise_float=self.precise_float), dtype=None
   1080             )
   1081         elif orient == "split":

ValueError: Expected object or value

CodePudding user response:

folder[0] is not the first filename, it is the first character of the string folder. To a list of files in the directory folder, use os.listdir(). Like this:

import os

folder = "D:\FastCharge"
files = [os.path.join(folder, file) for file in os.listdir(folder)]
df = pd.read_json(files[0])
df.head()
  • Related