I'm trying to find a generic way(without using a concrete case class in Scala) to parse Spark DataFrame to JSON Object/Array using Spray JSON or any other library.
I have tried to approach this using spray-json and my current code looks something like this
import spray.json._
import spray.json.DefaultJsonProtocol._
val list = sc.parallelize(List(("a1","b1","c1","d1"),("a2","b2","c2","d2"))).toDF
list.show
+---+---+---+---+
| _1| _2| _3| _4|
+---+---+---+---+
| a1| b1| c1| d1|
| a2| b2| c2| d2|
+---+---+---+---+
val json = list.toJSON.collect.toJson.prettyPrint
println(json)
Current Output:
["{\"_1\":\"a1\",\"_2\":\"b1\",\"_3\":\"c1\",\"_4\":\"d1\"}", "{\"_1\":\"a2\",\"_2\":\"b2\",\"_3\":\"c2\",\"_4\":\"d2\"}"]
Expected Output:
[{
"_1": "a1",
"_2": "b1",
"_3": "c1",
"_4": "d1"
}, {
"_1": "a2",
"_2": "b2",
"_3": "c2",
"_4": "d2"
}]
Kindly suggest how to get the expected output in the required format without using a "concrete scala case class". Either using spray-json or any other library.