I would really love some help with parsing nested JSON data using PySpark-SQL. The data has the following schema (blank spaces are edits for confidentiality purposes...)
Schema
root
|-- location_info: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- restaurant_type: string (nullable = true)
| | |
| | |
| | |-- other_data: array (nullable = true)
| | | |-- element: struct (containsNull = true)
| | | | |-- other_data_1 string (nullable = true)
| | | | |-- other_data_2: string (nullable = true)
| | | | |-- other_data_3: string (nullable = true)
| | | | |-- other_data_4: string (nullable = true)
| | | | |-- other_data_5: string (nullable = true)
| | |
| | |-- latitude: string (nullable = true)
| | |
| | |
| | |
| | |
| | |
| | |-- longitude: string (nullable = true)
| | |
| | |
| | |
| | |-- timezone: string (nullable = true)
|-- restaurant_id: string (nullable = true)
My Goal I would essentially want to get the data into the following data frame
restaurant_id | latitude | longtitude | timezone
I have tried
The following code
dfj = spark.read.option("multiLine", False).json("/file/path")
result = dfj.select(col('restaurant_id'),
explode(col('location_info')).alias('location_info') )
# SQL operation
result.createOrReplaceTempView('result')
subset_data = spark.sql(
'''
SELECT restaurant_id, location_info.latitude,location_info.longitude,location_info.timestamp
FROM result
'''
).show()
# Also tried this to read in
source_df_1 = spark.read.json(sc.wholeTextFiles("/file/path")
.values()
.flatMap(lambda x: x
.replace("{", "#!#")
.split("#!#")))
But oddly enough it gives me the following only for the first object or restaurant id
+-------+-----------+------------+--------------------+
|restaurant_id|latitude|longitude|timestamp|
+-------+-----------+------------+--------------------+
| 25|2.0|-8.0|2020-03-06T03:00:...|
| 25|2.0|-8.0|2020-03-06T03:00:...|
| 25|2.0|-8.0|2020-03-06T03:00:...|
| 25|2.0|-8.0|2020-03-06T03:01:...|
| 25|2.0|-8.0|2020-03-06T03:01:...|
+-------+-----------+------------+--------------------+
My research indicated that this may have something to do with the way JSON files are structured at the source. For example:
{}{
}{
}
Thereby not being multi-Line or something. Wondering what to do about this as well?
Thank you very much for reading, any help would really be appreciated. I know I can always count on SO to be helpful