Unable to Access data from json object

后端 未结 2 1704
感动是毒
感动是毒 2021-01-24 13:29

I have stored json data structure in a dataframe with single column, named json_data in R so my json text is stored in this format

row 1)    { \         


        
相关标签:
2条回答
  • 2021-01-24 13:56

    Here is one possibility:

    library(rjson)
    
    json_data <- fromJSON(file = json_file)
    formatted_add <- lapply(json_data$results, function(x) x$formatted_add)
    lat <- lapply(json_data$results, function(x) x$geometry$location$lat)
    lng <- lapply(json_data$results, function(x) x$geometry$location$lng)
    
    data <- cbind(formatted_add, lat, lng)
    

    I hope this is what you need.

    0 讨论(0)
  • 2021-01-24 14:15

    OK, after the clarification, what you have is a JSON string per each row of the data.frame

    To recreate the data, I'm taking the single JSON string (row) you provided, and putting it in two rows of a data.frame

    Therefore, to access the data, you need to parse each row of the data.frame individually

    However, in your question, you ask for the "first occurrence of formatted_address under location". In the row of data you've given, there are seven different locations, which you can see by the number of place_id values, and there is only one formatted_address per place_id. So asking for the first occurrence doesn't make sense, as there is only one.

    library(jsonlite)
    
    js <- '{
                     "results": [{
                           "address_components": [{
                           "long_name": "Unnamed Road",
                           "short_name": "Unnamed Road",
                           "types": ["route"]
                           }, {
                           .... <i've omitted the rest of the text> ...
                           },
                           "place_id": "ChIJkbeSa_BfYzARphNChaFPjNc",
                            "types": ["country", "political"]
                           }],
                            "status": "OK"
                           }'
    
    json_data_df <- data.frame(json_data = c(js, js))
    
    ## if parsing a row individually you would do
    # json_result <- fromJSON(as.character(json_data_df[1, "json_data"]))
    
    ## as you're parsing each row of the data.frame, you can use whatever looping 
    ## method you like
    
    ## you can get all the data out of the JSON and create a list obect of everything
    lst <- lapply(1:nrow(json_data_df), function(x){
      fromJSON(as.character(json_data_df[x, 'json_data']))
    })
    
    
    ## or you can subset it within the loop to just get the bits you want
    lst <- lapply(1:nrow(json_data_df), function(x){
      js <- fromJSON(as.character(json_data_df[x, 'json_data']))
      data.frame(
        place_id = js$results$place_id,
        formatted_address = js$results$formatted_address,
        lat = js$results$geometry$location$lat,
        lng = js$results$geometry$location$lng
        )
    })
    
    lst
    # [[1]]
    #                      place_id                          formatted_address      lat      lng
    # 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
    # 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
    # 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
    # 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
    # 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
    # 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
    # 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288
    # 
    # [[2]]
    #                      place_id                          formatted_address      lat      lng
    # 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
    # 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
    # 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
    # 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
    # 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
    # 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
    # 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288
    

    As per the comments - to access just the first entry per each JSON object, you can just subset the first element in the lapply

    ## or you can subset it within the loop to just get the bits you want
    lst <- lapply(1:nrow(json_data_df), function(x){
      js <- fromJSON(as.character(json_data_df[x, 'json_data']))
      data.frame(
        place_id = js$results$place_id[1],
        formatted_address = js$results$formatted_address[1],
        lat = js$results$geometry$location$lat[1],
        lng = js$results$geometry$location$lng[1]
      )
    })
    lst
    
    # [[1]]
    #                      place_id                  formatted_address      lat      lng
    # 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439
    
    # [[2]]
    #                      place_id                  formatted_address      lat      lng
    # 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439
    
    0 讨论(0)
提交回复
热议问题