Skip to content

pd.read_json cryptic error on missing file #5655

Closed
@ghost

Description

In [12]: pd.read_json("No Such File")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-f479cae2f65a> in <module>()
----> 1 pd.read_json("No Such File")

/home/user1/src/pandas/pandas/io/json.pyc in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit)
    188         obj = FrameParser(json, orient, dtype, convert_axes, convert_dates,
    189                           keep_default_dates, numpy, precise_float,
--> 190                           date_unit).parse()
    191 
    192     if typ == 'series' or obj is None:

/home/user1/src/pandas/pandas/io/json.pyc in parse(self)
    256 
    257         else:
--> 258             self._parse_no_numpy()
    259 
    260         if self.obj is None:

/home/user1/src/pandas/pandas/io/json.pyc in _parse_no_numpy(self)
    473         if orient == "columns":
    474             self.obj = DataFrame(
--> 475                 loads(json, precise_float=self.precise_float), dtype=None)
    476         elif orient == "split":
    477             decoded = dict((str(k), v)

ValueError: Expected object or value

read_json interprets strings which are not filenames as json data, then fails to parse them
if the filename names a path that doesn't exist (due to typo, or being in wrong directory for example).

That overloading makes it impossible nasty to distinguish two distinct error cases, e.g.
missing file and malformed json. Dubious API choice to my tastes.

in any case, catch both errors and return a saner message "missing file or malformed input" etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignError ReportingIncorrect or improved errors from pandasIO JSONread_json, to_json, json_normalize

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions