0

I have parquet file. I loaded using Spark.And one of the value is nested key,value pairs. How do I flatten?

df.printSchema
root
|-- location: string (nullable = true)
|-- properties: string (nullable = true)


texas,{"key":{"key1":"value1","key2":"value2"}}

thanks,

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
G G
  • 1,049
  • 4
  • 17
  • 26

1 Answers1

1

You can use explode on your dataframe and pass it a function that reads the JSON column using scala4s. Scala4s has easy parsing API, for your case it will look like:

val list = for {
  JArray(keys) <- parse(json) \\ "key"
  json @ JObject(key) <- keys
  JField("key1", JString(key1)) <- key
  JField("key2", JString(key2)) <- key
} yield {
  Seq(key1, key2)
}

This flattens your dataframe.

If you also want to add column for key, you can use withColumn after explode(keep the key also in the new column).

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
Ashish Awasthi
  • 1,302
  • 11
  • 23