I need to add an element inside an struct which is inside an struct itself.
File:
{"teamName":{"Redbull"},"info":{"drivers":{"driver":{"Max Verstappen","Alex Albon"},"carNumbers":{"33","23"},"carName":"RB7"}}}
base DF:
val jsonDF=spark.read.json("path")
jsonDF.printSchema
root
|-- info: struct (nullable = true)
| |-- drivers: struct (nullable = true)
| | |-- carName: string (nullable = true)
| | |-- carNumbers: string (nullable = true)
| | |-- driver: string (nullable = true)
|-- teamName: string (nullable = true)
I need to add age inside,info -> drivers ->
When i do this
jsonDF.withColumn("info",struct(col("info.drivers").alias("drivers"), lit("24").alias("age"))).printSchema
root
|-- info: struct (nullable = false)
| |-- drivers: struct (nullable = true)
| | |-- carName: string (nullable = true)
| | |-- carNumbers: string (nullable = true)
| | |-- driver: string (nullable = true)
| |-- age: string (nullable = false)
|-- teamName: string (nullable = true)
I'm getting it under info, i need to get it inside driver, how can i do that?