Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2024 03:35 AM
Hello Karene,
You can do the transformation in following manner from string to struct and refer to the example below:
data =[('001','{"name":"bhupendra","zipcode":"260100"}')]
schema = ['id','propertytype']
df = spark.createDataFrame(data,schema)
df.show(truncate=False)
df.printSchema()
from pyspark.sql.functions import from_json
from pyspark.sql.types import StructType, StructField,StringType
structTypeSchema = StructType([\
StructField('name',StringType()),\
StructField('zipcode',StringType())])
df1 = df.withColumn('propertystructtype', from_json(df.propertytype, structTypeSchema))
df1.show(truncate=False)
df1.printSchema()
BR