skip to Main Content

I have a JSON file and I want to access the "country" column

What I have done so far is resumed in this image:

enter image description here

2

Answers


  1. See if below steps are useful –

    1. Create a temp view on top of your dataframe.
    df_sql.createOrReplaceTempView("SampleView")
    
    1. Use spark sql to access ‘Country’ element like below –
    df_country = spark.sql("select place:`country` from SampleView")
    

    (This solution is as per your image)

    Login or Signup to reply.
  2. You could do this by creating a new column in your dataframe like this:

    df_sql.withColumn("country", F.col("place").getItem("country"))
    

    The column country should contain the value of the key country in the column place.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search