Del via


json (DataFrameWriter)

Saves the content of the DataFrame in JSON format (JSON Lines / newline-delimited JSON) at the specified path.

Syntax

json(path, mode=None, compression=None, dateFormat=None, timestampFormat=None,
     lineSep=None, encoding=None, ignoreNullFields=None)

Parameters

Parameter Type Description
path str The path in any Hadoop-supported file system.
mode str, optional The behavior when data already exists. Accepted values are 'append', 'overwrite', 'ignore', and 'error' or 'errorifexists' (default).

Returns

None

Examples

Write a DataFrame into a JSON file and read it back.

import tempfile
with tempfile.TemporaryDirectory(prefix="json") as d:
    spark.createDataFrame(
        [{"age": 100, "name": "Alice"}]
    ).write.json(d, mode="overwrite")

    spark.read.format("json").load(d).show()
    # +---+------------+
    # |age|        name|
    # +---+------------+
    # |100|Alice|
    # +---+------------+