
Answer-first summary for fast verification
Answer: df.withColumn('event_timestamp', get_json_object(col('event_data'), '$.event.details.timestamp'))
The correct answer is A because it correctly uses the `get_json_object` function to extract the nested `timestamp` field from the JSON strings in the `event_data` column and adds it as a new column `event_timestamp`.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You have a DataFrame df with a column event_data containing JSON strings. How would you extract the nested field event.details.timestamp from these JSON strings into a new column named event_timestamp using Spark? Provide the code snippet.
A
df.withColumn('event_timestamp', get_json_object(col('event_data'), '$.event.details.timestamp'))
B
df.select(get_json_object(col('event_data'), '$.event.details.timestamp').alias('event_timestamp'))
C
df.withColumn('event_timestamp', from_json(col('event_data'), 'event.details.timestamp'))
D
df.select(from_json(col('event_data'), 'event.details.timestamp').alias('event_timestamp'))
No comments yet.