I'm looking to convert a nested JSON and explode it to new rows.
The current data:
+-----------+-----------+-----------------------------+
| id| site | sale|
+-----------------------------------------------------+
| abc| 6|{"2019-05-08T00:00:00": null, "2019-05-09T00:00:00": [{"key1": 2, "key2": 0}]} |
| def| 5|{"2019-05-08T00:00:00": [{"key1": 22, "key2": 10}], "2019-05-09T00:00:00": null} |
| ghi| 4|{"2019-05-08T00:00:00": null, "2019-05-09T00:00:00": null} |
Desired result
+-----------+-----------+------------------------+-----------+-----------+
| id| site | saledate| key1| key2|
+-------------------------------------------------------------------------
| abc| 6| 2019-05-08T00:00:00| null| null|
| abc| 6| 2019-05-09T00:00:00| 2| 0|
| def| 5| 2019-05-08T00:00:00| 22| 10|
| def| 5| 2019-05-09T00:00:00| null| null|
| ghi| 4| 2019-05-08T00:00:00| null| null|
| ghi| 4| 2019-05-09T00:00:00| null| null|
What I have tried:
explodeworks only forArraytype.
new_df = old_df.withColumn('saledate', explode('sale'))
- I'm able to get properties of each date by doing the following. However, I would have to specify each date separately, which is not ideally.
new_df = old_df
.withColumn('sale_collection', explode('sale.2019-05-08T00:00:00'))
.withColumn('key1', col('sale_collection').getItem('key1')
.withColumn('key2', col('sale_collection').getItem('key2')