I have a JSON file in the below format that I am trying to read/query from spark job.
{
"dimensions": [
{"id1":"val1"},
{"id2":"val2"},
{"id3":"val"}
...
]
}
val schema = (new StructType).
add("dimensions",
new ArrayType(MapType(StringType,StringType),true))
val df = sparkContext.read.schema(schema).json(file)
'dimensions' is a JSON array and contains key value pair. I want to read it as just key-value pair (map) so that its easy to query in Spark SQL.
I tried above above schema, but it gives me array with each item being of MapType. Is there a way to read the above json array as MapType?
In the end, I want to be able to write spark sql something like below where I can select individual keys:
val result = spark.sql("SELECT dimensions.id1, dimensions.id3 FROM table")
Thanks!