Let's say I have a dataframe which looks like this:
+--------------------+--------------------+--------------------------------------------------------------+
| id | Name | Payment|
+--------------------+--------------------+--------------------------------------------------------------+
| 1 | James |[ {"@id": 1, "currency":"GBP"},{"@id": 2, "currency": "USD"} ]|
+--------------------+--------------------+--------------------------------------------------------------+
And the schema is:
root
|-- id: integer (nullable = true)
|-- Name: string (nullable = true)
|-- Payment: string (nullable = true)
How can I explode the above JSON array into below:
+--------------------+--------------------+-------------------------------+
| id | Name | Payment|
+--------------------+--------------------+-------------------------------+
| 1 | James | {"@id":1, "currency":"GBP"} |
+--------------------+--------------------+-------------------------------+
| 1 | James | {"@id":2, "currency":"USD"} |
+--------------------+--------------------+-------------------------------+
I've been trying to use the explode functionality like the below, but it's not working. It's giving an error about not being able to explode string types, and that it expects either a map or array. This makes sense given the schema denotes it's a string, rather than an array/map, but I'm not sure how to convert this into an appropriate format.
val newDF = dataframe.withColumn("nestedPayment", explode(dataframe.col("Payment")))
Any help is greatly appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…