pandas.api.typing.JsonReader#
- class pandas.api.typing.JsonReader(filepath_or_buffer, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, storage_options=None, encoding_errors='strict', dtype_backend=<no_default>, engine='ujson')[source]#
JsonReader provides an interface for reading in a JSON file.
If initialized with
lines=Trueandchunksize, can be iterated overchunksizelines at a time. Otherwise, callingreadreads in the whole document.A
JsonReaderis returned byread_json()whenchunksizeis passed; it is not usually instantiated directly.- Parameters:
- filepath_or_buffera str path, path object or file-like object
Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. pandas accepts any
os.PathLike, or file-like object with aread()method, such as a file handle orStringIO.- orientstr
Indication of expected JSON string format. Compatible JSON strings can be produced by
to_json()with a corresponding orient value. Seeread_json()for the set of possible orients.- typ{“frame”, “series”}
The type of object to recover.
- dtypebool or dict
If True, infer dtypes; if a dict of column to dtype, then use those; if False, then don’t infer dtypes at all, applies only to the data.
- convert_axesbool or None
Try to convert the axes to the proper dtypes.
- convert_datesbool or list of str
If True then default datelike columns may be converted (depending on
keep_default_dates). If False, no dates will be converted. If a list of column names, then those columns will be converted and default datelike columns may also be converted (depending onkeep_default_dates).- keep_default_datesbool
If parsing dates (
convert_datesis not False), then try to parse the default datelike columns. Seeread_json()for the criteria used to determine whether a column label is datelike.- precise_floatbool
Set to enable usage of higher precision (strtod) function when decoding string to double values.
- date_unitstr or None
The timestamp unit to detect if converting dates. The default behaviour is to try and detect the correct precision, but if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force parsing only seconds, milliseconds, microseconds or nanoseconds respectively.
- encodingstr or None
The encoding to use to decode py3 bytes.
- linesbool
Read the file as a json object per line.
- chunksizeint or None
Return a
JsonReaderfor iteration, yieldingchunksizelines at a time. Can only be passed iflines=True. If this is None, the file will be read into memory all at once.- compressionstr or dict
For on-the-fly decompression of on-disk data, see
read_json()for the accepted values.- nrowsint or None
The number of lines from the line-delimited json file that has to be read. Can only be passed if
lines=True. If this is None, all the rows will be returned.- storage_optionsdict, optional
Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. See
read_json()for more details.- encoding_errorsstr, optional, default “strict”
How encoding errors are treated. List of possible values .
- dtype_backend{“numpy_nullable”, “pyarrow”}
Back-end data type applied to the resultant
DataFrame(still experimental). Seeread_json()for more details.- engine{“ujson”, “pyarrow”}, default “ujson”
Parser engine to use. See
read_json()for the restrictions on the"pyarrow"engine.
See also
read_jsonConvert a JSON string to pandas object.
Examples
>>> from io import StringIO >>> df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) >>> reader = pd.read_json( ... StringIO(df.to_json(orient="records", lines=True)), ... lines=True, ... chunksize=1, ... ) >>> for chunk in reader: ... print(chunk) a b 0 1 3 a b 1 2 4
Methods
close()Close the underlying file handle, if one was opened by the reader.
read()Read the whole JSON input into a pandas object.