1

I am trying to parse nested json with some sample json. Below is the print schema

 |-- batters: struct (nullable = true)
 |    |-- batter: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- id: string (nullable = true)
 |    |    |    |-- type: string (nullable = true)
 |-- id: string (nullable = true)
 |-- name: string (nullable = true)
 |-- ppu: double (nullable = true)
 |-- topping: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- id: string (nullable = true)
 |    |    |-- type: string (nullable = true)
 |-- type: string (nullable = true)

Trying to explode batters,topping separately and combine them.

df_batter = df_json.select("batters.*")
df_explode1= df_batter.withColumn("batter", explode("batter")).select("batter.*")

df_explode2= df_json.withColumn("topping", explode("topping")).select("id", 
"type","name","ppu","topping.*")

Unable to combine the two data frame.

Tried using single query

exploded1 = df_json.withColumn("batter", df_batter.withColumn("batter", 
explode("batter"))).withColumn("topping", explode("topping")).select("id", 
"type","name","ppu","topping.*","batter.*")

But getting error.Kindly help me to solve it. Thanks

1
  • u can't explode two arrays like that. you need to zip them using arrays_zip and then explode them together Commented Apr 1, 2020 at 15:25

1 Answer 1

1

You basically have to explode the arrays together using arrays_zip which returns a merged array of structs. Try this. I haven't tested but it should work.

from pyspark.sql import functions as F    
df_json.select("id","type","name","ppu","topping","batters.*")\
       .withColumn("zipped", F.explode(F.arrays_zip("batter","topping")))\
       .select("id","type","name","ppu","zipped.*").show()

You could also do it one by one:

from pyspark.sql import functions as F    
    df1=df_json.select("id","type","name","ppu","topping","batters.*")\
           .withColumn("batter", F.explode("batter"))\
           .select("id","type","name","ppu","topping","batter")
    df1.withColumn("topping", F.explode("topping")).select("id","type","name","ppu","topping.*","batter.*")
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.