0

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
0

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

I'd like to add that listdir will list all files and directories. If you aren't certain that the folder only contains files, I'd recommend looking at this answer stackoverflow.com/a/3207973 on how to filter out the directories.
@MichaelM. i did what you said but l was still getting an error , however , l tried something else and it worked import os folder = "D:\FastCharge" files = [os.path.join(folder,file) for file in os.listdir(folder)]
@TH3CODEgUy I've updated my answer to reflect what worked for you. If you want to mark it correct, then it could help others with the same issue in the future.
@MichaelM. sure l have done that, thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.