0

I have a dataframe like below

col1
------
[{"a":"1","b":"2"},{"a":"11,"b":"22"}]

now i want to include the new struct using the existing value {"cc": "1" } --> here 1 is coming from "a": "1"

col1
------
[{"a":"1","b":"2", {"cc": "1" }},{"a":"11,"b":"22",{"cc": "11" } }]  

please suggest me the udf in pyspark,

1
  • Please show us your dataframe with df.show(), you posted a dictionary in a list. Commented Aug 12, 2021 at 14:47

1 Answer 1

1

You can use transform function (from spark V2.4) to get desired result.

from pyspark.sql import *
from pyspark.sql.functions import *

spark = SparkSession.builder.master('local[*]').getOrCreate()

df = spark.createDataFrame([('[{"a":"1","b":"2"},{"a":"11","b":"22"}]',)],"col1 string")

df.withColumn("col1", from_json("col1", schema_of_json(df.select("col1").first()[0]))).\
    selectExpr("to_json(transform(col1, x-> "
               "struct(x.a as a, x.b as b, struct(x.a as cc) as cc))) as co1").\
    show(truncate=False)

    +------------------------------------------------------------------------+
    |co1                                                                     |
    +------------------------------------------------------------------------+
    |[{"a":"1","b":"2","cc":{"cc":"1"}},{"a":"11","b":"22","cc":{"cc":"11"}}]|
    +------------------------------------------------------------------------+
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.