BR_DatabricksAI
Databricks Partner

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