maxframe.dataframe.DataFrame.to_json#
- DataFrame.to_json(path: str | None = None, orient: str | None = None, date_format: str | None = None, double_precision: int = 10, force_ascii: bool = True, date_unit: str | None = 'ms', default_handler: callable | None = None, lines: bool = False, compression: str | Dict[str, Any] | None = 'infer', index: bool | None = None, indent: int | None = None, storage_options: Dict[str, Any] | None = None, partition_cols: str | list | None = None, **kwargs)#
Convert the object to a JSON string.
Note NaN’s and None will be converted to null and datetime objects will be converted to UNIX timestamps.
- Parameters:
path (str, path object, file-like object, or None, default None) – String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function. If None, the result is returned as a string.
orient (str) –
Indication of expected JSON string format.
Series:
default is ‘index’
allowed values are: {‘split’, ‘records’, ‘index’, ‘table’}.
DataFrame:
default is ‘columns’
allowed values are: {‘split’, ‘records’, ‘index’, ‘columns’, ‘values’, ‘table’}.
The format of the JSON string:
’split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
’records’ : list like [{column -> value}, … , {column -> value}]
’index’ : dict like {index -> {column -> value}}
’columns’ : dict like {column -> {index -> value}}
’values’ : just the values array
’table’ : dict like {‘schema’: {schema}, ‘data’: {data}}
Describing the data, where data component is like
orient='records'.
date_format ({None, 'epoch', 'iso'}) – Type of date conversion. ‘epoch’ = epoch milliseconds, ‘iso’ = ISO8601. The default depends on the orient. For
orient='table', the default is ‘iso’. For all other orients, the default is ‘epoch’.double_precision (int, default 10) – The number of decimal places to use when encoding floating point numbers.
force_ascii (bool, default True) – Force encoded string to be ASCII.
date_unit (str, default 'ms' (milliseconds)) – The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’, ‘ns’ for second, millisecond, microsecond, and nanosecond respectively.
default_handler (callable, default None) – Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serializable object.
lines (bool, default False) – If ‘orient’ is ‘records’ write out line-delimited json format. Will throw ValueError if incorrect ‘orient’ is used.
compression (str or dict, default 'infer') – For on-the-fly compression of the output data. If str, represents compression mode. If dict, value at ‘method’ is the compression mode. Compression mode may be any of the following possible values: {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}. If compression mode is ‘infer’ and path_or_buf is path-like, then detect compression mode from the following extensions: ‘.gz’, ‘.bz2’, ‘.zip’ or ‘.xz’. (otherwise no compression). If dict given and mode is one of {‘zip’, ‘xz’}, other entries passed as additional compression options.
index (bool, default None) – Whether to include the index values in the JSON string. Not including the index (
index=False) is only supported when orient is ‘split’ or ‘table’.indent (int, optional) – Length of whitespace used to indent each record.
partition_cols (list, optional, default None) – Column names by which to partition the dataset. Columns are partitioned in the order they are given.
See also
read_jsonConvert a JSON string to pandas object.
Notes
The behavior of
indent=0varies from the stdlib, which does not indent the output but does insert newlines. Currently,indent=0and the defaultindent=Noneare equivalent in pandas, though this may change in a future release.orient='table'contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema.Examples
>>> import maxframe.dataframe as md >>> df = md.DataFrame([['a', 'b'], ['c', 'd']], ... index=['row 1', 'row 2'], ... columns=['col 1', 'col 2']) >>> df.to_json('data.json') >>> # Writing to a file with orient='records' >>> df.to_json('records.json', orient='records') >>> # Writing in line-delimited json format >>> df.to_json('ldjson.json', orient='records', lines=True) >>> # Write partitioned dataset >>> df.to_json('dataset', partition_cols=['col 1'])