polars.DataFrame.write_json#

DataFrame.write_json(
file: IOBase | str | Path | None = None,
*,
pretty: bool = False,
row_oriented: bool = False,
) str | None[source]#

Serialize to JSON representation.

Parameters:
file

File path or writable file-like object to which the result will be written. If set to None (default), the output is returned as a string instead.

pretty

Pretty serialize json.

row_oriented

Write to row oriented json. This is slower, but more common.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...     }
... )
>>> df.write_json()
'{"columns":[{"name":"foo","datatype":"Int64","bit_settings":"","values":[1,2,3]},{"name":"bar","datatype":"Int64","bit_settings":"","values":[6,7,8]}]}'
>>> df.write_json(row_oriented=True)
'[{"foo":1,"bar":6},{"foo":2,"bar":7},{"foo":3,"bar":8}]'