1

I have the following simple json in a file saved as notepad file t.json

[{"a": 1, "b": 2, "c": 3},
{"a": 4, "b": 5, "c": 6},
{"a": 7, "b": 8, "c": 9}]

I am trying to open it using

ValueError                                Traceback (most recent call last)
<ipython-input-236-487720f2328b> in <module>()
----> 1 data = pd.read_json('t.json')

~\Anaconda3\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)
    420         return json_reader
    421
--> 422     result = json_reader.read()
    423     if should_close:
    424         try:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in read(self)
    527             )
    528         else:
--> 529             obj = self._get_object_parser(self.data)
    530         self.close()
    531         return obj

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in _get_object_parser(self, json)
    544         obj = None
    545         if typ == 'frame':
--> 546             obj = FrameParser(json, **kwargs).parse()
    547
    548         if typ == 'series' or obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in parse(self)
    636
    637         else:
--> 638             self._parse_no_numpy()
    639
    640         if self.obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in _parse_no_numpy(self)
    851         if orient == "columns":
    852             self.obj = DataFrame(
--> 853                 loads(json, precise_float=self.precise_float), dtype=None)
    854         elif orient == "split":
    855             decoded = {str(k): v for k, v in compat.iteritems(

ValueError: Expected object or value

I have tried creating an absolute path as per the suggestion in this page but it also produces a value error. Can anyone help please?

taras
  • 5,216
  • 9
  • 32
  • 41
Owen
  • 157
  • 2
  • 11
  • Possible duplicate of [ValueError when using pandas.read\_json](https://stackoverflow.com/questions/27240982/valueerror-when-using-pandas-read-json) – typhon04 Aug 05 '19 at 22:55

1 Answers1

2

There is a possibility this is a bug of pandas module. What version are you using? Consider updating.

After that you can try the following:

data  = pd.read_json('t.json', lines=False)

Hope it helps

curi0uz_k0d3r
  • 445
  • 2
  • 9
  • Thanks for the suggestion. Strangely when I run 'conda update panda' i get an error ''conda' is not recognized as an internal or external command, operable program or batch file' Does this suggest an update of anaconda needed? – Owen Aug 11 '18 at 19:27
  • I think that windows cannot find the conda tool. You have to specify the path. Check https://stackoverflow.com/questions/44597662/conda-command-is-not-recognized-on-windows-10. However, consider installing pandas using pip (https://stackoverflow.com/questions/4750806/how-do-i-install-pip-on-windows) – curi0uz_k0d3r Aug 11 '18 at 20:36
  • Also using the parameter lines=False does solve the problem? – curi0uz_k0d3r Aug 12 '18 at 06:22
  • I've updated conda. created a an asbolute path as follows path = os.path.abspath("DESKTOP\python work jan") and then ran this data = pd.read_json(path + '\t.json', lines=False) to get an error read_json() got an unexpected keyword argument 'path' – Owen Aug 12 '18 at 11:36
  • Could you move the t.json file to your working folder, or specify the path directly in read_json without using the os,path module? if you need to specify the absolute path, consider using desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') to get the path to your Desktop and path.join in general to avoid mistakes when merging paths. – curi0uz_k0d3r Aug 12 '18 at 12:13