0

I have dataframe which is product of left join. Now I want to create json structure.

I tried using different option but I was not able create it. Here is my dataframe :

col1    col2    col3    col4
1111    name    aaa     bbb
1111    name    ccc     ddd
1111    name    iii     kkk
1112    name1   abcd    def
1112    name1   DEFG    ABXC

The desired json structure is:

{col1: 1111, col2: name, details: [{col3: aaa, col4: bbb}, {col3: ccc, col4: ddd}, {col3: iii, col4: kkk}]},
{col1: 1112, col2: name1, details: [{col3: abcd, col4: def}, {col3: DEFG, col4: ABXC}]}

1 Answer 1

1

you can do it like this:

import pyspark.sql.functions as f

df = df.withColumn("details", f.to_json(f.struct("col3", "col4")))
df = df.groupBy(*["col1", "col2"]).agg(f.collect_list("details").alias("details"))

df.write.format('json').save('/path/file_name.json')

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.