I would like to extract data from a json column in pyspark dataframe by python3.
My dataframe:
year month p_name json_col
2010 05 rchsc [{"attri_name": "in_market", "value": "yes"}, {"attri_name": "weight", "value": "12.56"}, {"attri_name" : "color", "value" : "red"} ]
I need a dataframe like:
year month p_name in_market weight color
2010 05 rchsc yes 12.56 red
I have tried
from pyspark.sql.functions import from_json, col
from pyspark.sql.types import StructType, StructField, StringType
schema = StructType(
[
StructField('attri_name', StringType(), True),
StructField('value', StringType(), True)
]
)
df.withColumn("new_col", from_json("json_col", schema))
But, no new columns are created. I am not sure how to decompose the json column and pivot them to new columns.