Databricks Data Engineer Associate Question 167
Single answerYou are working with a DataFrame in Databricks that contains a nested JSON column named user_info with the structure: { "name": { "first": "John", "last": "Doe" }, "age": 30 }. How can you extract the first name of the user?
- A
df.select('user_info.name.first')
- B
df.select(col('user_info.name.first'))
- C
df.select('user_info.first.name')
- D
df.select(col('user_info.first.name'))
Show answer and explanation
Correct answer: A
Explanation
In Databricks, to extract a nested field from a column, you can use the dot syntax to specify the full path to the field. The correct field path in this scenario is user_info.name.first, as the first field is nested under name, which is in turn nested under user_info.
- A. Correct.
Correct. The dot syntax is used to access nested fields in a column, and the path
user_info.name.firstcorrectly points to the first name. - B. Incorrect.
Incorrect. While
col()is valid for referencing columns, the syntax in this case is incorrect because it does not properly use the dot syntax for accessing nested fields. - C. Incorrect.
Incorrect. The path
user_info.first.nameis invalid because it does not match the structure of the JSON data; thefirstfield is nested undername. - D. Incorrect.
Incorrect. Similar to Option 3, the path
user_info.first.nameis invalid, and wrapping it incol()does not fix the incorrect field reference.