Skip to content

Conversation

@zhengruifeng
Copy link
Contributor

@zhengruifeng zhengruifeng commented Jan 19, 2026

What changes were proposed in this pull request?

Implement DataFrame.toJSON in Python Client

Why are the changes needed?

to provide a similar way for creating json string

Does this PR introduce any user-facing change?

Yes, new API

In [2]: df = spark.range(10).withColumn("s", col("id").cast("string"))

In [3]: df.toJSON().show()
+----------------+
|           value|
+----------------+
|{"id":0,"s":"0"}|
|{"id":1,"s":"1"}|
|{"id":2,"s":"2"}|
|{"id":3,"s":"3"}|
|{"id":4,"s":"4"}|
|{"id":5,"s":"5"}|
|{"id":6,"s":"6"}|
|{"id":7,"s":"7"}|
|{"id":8,"s":"8"}|
|{"id":9,"s":"9"}|
+----------------+

How was this patch tested?

added tests

Was this patch authored or co-authored using generative AI tooling?

no

@github-actions
Copy link

github-actions bot commented Jan 19, 2026

JIRA Issue Information

=== New Feature SPARK-55090 ===
Summary: Implement DataFrame.toJSON in Python Client
Assignee: None
Status: Open
Affected: ["4.2.0"]


This comment was automatically generated by GitHub Actions

@zhengruifeng zhengruifeng changed the title [SPARK-55089][CONNECT] Correct the output schema of toJSON [SPARK-55090][PYTHON] Implement DataFrame.toJSON in Python Client Jan 19, 2026

if not is_remote_only():
def toJSON(self) -> ParentDataFrame:
return self.select(F.to_json(F.struct(F.col("*"))).alias("value"))
Copy link
Contributor Author

@zhengruifeng zhengruifeng Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow scala client

/** @inheritdoc */
def toJSON: Dataset[String] = {
select(to_json(struct(col("*")))).as(StringEncoder)
}

with additional fix #53850

@zhengruifeng zhengruifeng changed the title [SPARK-55090][PYTHON] Implement DataFrame.toJSON in Python Client [SPARK-55090][PYTHON][CONNECT] Implement DataFrame.toJSON in Python Client Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant