I have a column in a DataFrame that contains a nested json in string format
val df=Seq(("""{"-1":{"-1":[ 7420,0,20,22,0,0]}}""" ), ("""{"-1":{"-1":[1006,2,18,10,0,0]}}"""), ("""{"-1":{"-1":[6414,0,17,11,0,0]}}""")).toDF("column1")
+-------------------------------------+
| column1|
+-------------------------------------+
|{"-1":{"-1":[7420, 0, 20, 22, 0, 0]}}|
|{"-1":{"-1":[1006, 2, 18, 10, 0, 0]}}|
|{"-1":{"-1":[6414, 0, 17, 11, 0, 0]}}|
+-----------------------+-------------+
I want to get a data frame that looks like this
+----+----+----+----+----+----+----+----+
|col1|col2|col3|col4|col5|col6|col7|col8|
+----+----+----+----+----+----+----+----+
| -1| -1|7420| 0| 20| 22| 0| 0|
| -1| -1|1006| 2| 18| 10| 0| 0|
| -1| -1|6414| 0| 17| 11| 0| 0|
+----+----+----+----+----+----+----+----+
I first applied get_json_object which gave me
val df1= df.select(get_json_object($"column1", "$.-1")
+------------------------------+
| column1|
+------------------------------+
|{"-1":[7420, 0, 20, 22, 0, 0]}|
|{"-1":[1006, 2, 18, 10, 0, 0]}|
|{"-1":[6414, 0, 17, 11, 0, 0]}|
+-----------------------+------+
so I lost the first element.
I tried to convert the existing elements to the format I wanted with this
val schema = new StructType()
.add("-1",
MapType(
StringType,
new StructType()
.add("a1", StringType)
.add("a2", StringType)
.add("a3", StringType)
.add("a4", StringType)
.add("a5", StringType)
.add("a6", StringType)
.add("a7", StringType)
.add("a8", StringType)
.add("a9", StringType)
.add("a10", StringType)
.add("a11", StringType)
.add("a11", StringType)))
df1.select(from_json($"new2", schema ))
but it returned a 1 column DataFrame of all nulls