Code python

PySpark DataFrame - Convert JSON Column to Row using json_tuple

Kontext Kontext visibility 1,317 comment 0 access_time 2 years ago language English

descriptionCode description

PySpark SQL functions json_tuple can be used to convert DataFrame JSON string columns to tuples (new rows in the DataFrame). 

Syntax of this function looks like the following:

pyspark.sql.functions.json_tuple(col, *fields)

The first parameter is the JSON string column name in the DataFrame and the second is the filed name list to extract.

If you need to extract complex JSON documents like JSON arrays, you can follow this article - PySpark: Convert JSON String Column to Array of Object (StructType) in DataFrame.

Output

StructType([StructField('id', LongType(), True), StructField('c0', StringType(), True), StructField('c1', StringType(), True), StructField('c2', StringType(), True)])

+---+---+------+----------+
| id| c0|    c1|        c2|
+---+---+------+----------+
|  1|  1|10.201|2021-01-01|
|  2|  2|20.201|2022-01-01|
+---+---+------+----------+
fork_rightFork
more_vert
copyright This page is subject to Site terms.
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts