I have a json string as :
{"user_rating": {"rating_text": "Excellent", "rating_color": "3F7E00", "votes": "778", "aggregate_rating": "4.5"}}
I wish to create a DataFrame out of it with DataFrame columns as:
rating_text | rating_color | votes | aggregate_rating
When I code it as :
val pdf = json.select("user_rating")
I get only one column user_rating
I approached this most voted solution but still getting only user_rating column : pdf.show()
Not sure how the Solution1 works exactly.
Unable to access columns by index as provided in Solution2. Getting NoSuchColumn column error.
What is the best possible approach to extract keys(rating_text,rating_color,..) and use as columns in a DataFrame?
Language I am using : Scala
Tried the below way to Iterate over each Row in DataFrame and parse by getting columns:
val pdf = json.select("restaurants.restaurant.user_rating")
pdf.map{Rrowow => (row.getStruct(0).getString(0))}.show()
Getting below exception at map function:
java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to org.apache.spark.sql.Row
json.select("user_rating.*")