I have a spark dataframe with one column having multiple dictionaries:
| id | result |
|---|---|
| 1 | {'key1':'a', 'key2':'b'}, {'key1':'d', 'key2':'e'}, {'key1':'m', 'key2':'n'} |
| 2 | {'key1':'r', 'key2':'s'}, {'key1':'t', 'key2':'u'} |
I need the final output as:
| id | key1 | key2 |
|---|---|---|
| 1 | a | b |
| 1 | d | e |
| 1 | m | n |
| 2 | r | s |
| 2 | t | u |
And planning to explode this twice to get the results.
Although, The column result is of StringType() and therefore I am unable to explode it using the explode function:
df.withColumn("output", explode(col("result")))
Error:
AnalysisException: cannot resolve 'explode(
result)' due to data type mismatch: input to function explode should be array or map type, not string; 'Project [result#9651, explode(result#9651) AS output#9660] +- Relation[result#9651] json
Please help on how to resolve this.