pyspark.sql.DataFrame.toJSON#
- DataFrame.toJSON(use_unicode=True)[source]#
Converts a
DataFrameinto aRDDof string orDataFrame.Each row is turned into a JSON document as one element in the returned RDD or DataFrame.
New in version 1.3.0.
Changed in version 4.2.0: Supports Spark Connect.
- Parameters
- use_unicodebool, optional, default True
Whether to convert to unicode or not. Note that this argument is disallowed in Spark Connect mode.
- Returns
RDD(in Classic mode) orDataFrame(in connect mode)
Examples
>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"]) >>> df.toJSON().first() '{"age":2,"name":"Alice"}'