[SOLVED] Reading multiple JSON files in python using Jupiter notebook


This Content is from Stack Overflow. Question asked by TH3CODEgUy

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" 

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:Anacondalibsite-packagespandasutil_decorators.py in wrapper(*args, **kwargs)
    197                 else:
    198                     kwargs[new_arg_name] = new_arg_value
--> 199             return func(*args, **kwargs)
    201         return cast(F, wrapper)

C:Anacondalibsite-packagespandasutil_decorators.py in wrapper(*args, **kwargs)
    297                 )
    298                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 299             return func(*args, **kwargs)
    301         return wrapper

C:Anacondalibsite-packagespandasiojson_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)
    562     with json_reader:
--> 563         return json_reader.read()

C:Anacondalibsite-packagespandasiojson_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:Anacondalibsite-packagespandasiojson_json.py in _get_object_parser(self, json)
    714         obj = None
    715         if typ == "frame":
--> 716             obj = FrameParser(json, **kwargs).parse()
    718         if typ == "series" or obj is None:

C:Anacondalibsite-packagespandasiojson_json.py in parse(self)
    830         else:
--> 831             self._parse_no_numpy()
    833         if self.obj is None:

C:Anacondalibsite-packagespandasiojson_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


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])

This Question was asked in StackOverflow by TH3CODEgUy and Answered by Michael M. It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?