0

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?

1 Answer 1

2

I would go with a library called spark-hats https://github.com/AbsaOSS/spark-hats

Then it is

import za.co.absa.spark.hats.Extensions._

val jsonDFwithAge = jsonDF.nestedWithColumn("info.drivers.age", lit("24"))

jsonDFwithAge.printSchema
root
 |-- info: struct (nullable = false)
 |    |-- drivers: struct (nullable = false)
 |    |    |-- carName: string (nullable = true)
 |    |    |-- carNumbers: string (nullable = true)
 |    |    |-- driver: string (nullable = true)
 |    |    |-- ag: string (nullable = false)
 |-- teamName: string (nullable = true)
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.